From c0ceeda03d308915e7c16d349984cb799a6459bb Mon Sep 17 00:00:00 2001 From: James Harris Date: Thu, 26 Sep 2024 11:30:48 +1000 Subject: [PATCH] Add more marshaling tests. --- go.mod | 2 +- go.sum | 4 ++-- identity.go | 2 +- marshal.go | 15 ++++++--------- marshal_test.go | 32 +++++++++++++++++++++++++++++--- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 2a818fa..4c633df 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.23.1 require ( github.com/dogmatiq/aureus v0.1.0 github.com/dogmatiq/dogma v0.14.3-0.20240923090950-ee623b679849 - github.com/dogmatiq/enginekit v0.11.1-0.20240925034306-c9f562c00a8c + github.com/dogmatiq/enginekit v0.11.1-0.20240926011551-78b4182956fb github.com/dogmatiq/iago v0.4.0 github.com/emicklei/dot v1.6.2 github.com/google/uuid v1.6.0 diff --git a/go.sum b/go.sum index 0446041..d9f8bea 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/dogmatiq/dapper v0.6.0 h1:hnWUsjnt3nUiC9hmkPvuxrnMd7fYNz1i+/GS3gOx0Xs github.com/dogmatiq/dapper v0.6.0/go.mod h1:ubRHWzt73s0MsPpGhWvnfW/Z/1YPnrkCsQv6CUOZVEw= github.com/dogmatiq/dogma v0.14.3-0.20240923090950-ee623b679849 h1:iAfnsLzgfSJBkseLyvotVLHCbTWDh90o2fZml+al3fg= github.com/dogmatiq/dogma v0.14.3-0.20240923090950-ee623b679849/go.mod h1:9lyVA+6V2+E/exV0IrBOrkUiyFwIATEhv+b0vnB2umQ= -github.com/dogmatiq/enginekit v0.11.1-0.20240925034306-c9f562c00a8c h1:NjaderfK+z0sjDv5VauniuBuczJ47MevOG7FlkWJXJE= -github.com/dogmatiq/enginekit v0.11.1-0.20240925034306-c9f562c00a8c/go.mod h1:XZcFZ9U5bMIu05rUmDtUHEhL49kCoSxcuZdr6o8Fih4= +github.com/dogmatiq/enginekit v0.11.1-0.20240926011551-78b4182956fb h1:mYmYU9WC3CYhXwWx78BUj1WQPta28VM43PftS7FE1Co= +github.com/dogmatiq/enginekit v0.11.1-0.20240926011551-78b4182956fb/go.mod h1:XZcFZ9U5bMIu05rUmDtUHEhL49kCoSxcuZdr6o8Fih4= github.com/dogmatiq/iago v0.4.0 h1:57nZqVT34IZxtCZEW/RFif7DNUEjMXgevfr/Mmd0N8I= github.com/dogmatiq/iago v0.4.0/go.mod h1:fishMWBtzYcjgis6d873VTv9kFm/wHYLOzOyO9ECBDc= github.com/dogmatiq/jumble v0.1.0 h1:Cb3ExfxY+AoUP4G9/sOwoOdYX8o+kOLK8+dhXAry+QA= diff --git a/identity.go b/identity.go index 235e65f..294661b 100644 --- a/identity.go +++ b/identity.go @@ -134,7 +134,7 @@ func ValidateIdentityName(n string) error { // ValidateIdentityKey returns nil if n is a valid application or handler key; // otherwise, it returns an error. func ValidateIdentityKey(k string) error { - if _, err := uuidpb.FromString(k); err != nil { + if _, err := uuidpb.Parse(k); err != nil { return validation.Errorf( "invalid key %#v, keys must be RFC 4122 UUIDs", k, diff --git a/marshal.go b/marshal.go index 5172bfd..3bc6515 100644 --- a/marshal.go +++ b/marshal.go @@ -97,7 +97,6 @@ func FromProto(app *configpb.Application) (Application, error) { out.handlers = HandlerSet{} } out.handlers.Add(handlerOut) - out.names.union(handlerOut.MessageNames()) } return out, nil @@ -239,14 +238,9 @@ func marshalIdentity(in Identity) (*identitypb.Identity, error) { return nil, err } - key, err := uuidpb.FromString(in.Key) - if err != nil { - return nil, err - } - return &identitypb.Identity{ Name: in.Name, - Key: key, + Key: uuidpb.MustParse(in.Key), }, nil } @@ -333,7 +327,6 @@ func unmarshalMessageKind(r configpb.MessageKind) (message.Kind, error) { // produced by unmarshaling a configuration. type unmarshaledApplication struct { ident Identity - names EntityMessageNames typeName string handlers HandlerSet } @@ -343,7 +336,11 @@ func (a *unmarshaledApplication) Identity() Identity { } func (a *unmarshaledApplication) MessageNames() EntityMessageNames { - return a.names + var names EntityMessageNames + for _, h := range a.handlers { + names.union(h.MessageNames()) + } + return names } func (a *unmarshaledApplication) TypeName() string { diff --git a/marshal_test.go b/marshal_test.go index 9793922..103258d 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -3,6 +3,7 @@ package configkit import ( //revive:disable:dot-imports "github.com/dogmatiq/configkit/message" + . "github.com/dogmatiq/enginekit/enginetest/stubs" "github.com/dogmatiq/enginekit/protobuf/configpb" "github.com/dogmatiq/enginekit/protobuf/identitypb" "github.com/dogmatiq/enginekit/protobuf/uuidpb" @@ -16,13 +17,38 @@ var _ = Describe("func ToProto()", func() { BeforeEach(func() { app = &unmarshaledApplication{ - ident: MustNewIdentity("", "28c19ec0-a32f-4479-bb1d-02887e90077c"), + ident: MustNewIdentity("", "28c19ec0-a32f-4479-bb1d-02887e90077c"), typeName: "", - names: EntityMessageNames{}, - handlers: HandlerSet{}, + handlers: HandlerSet{ + MustNewIdentity("", "3c73fa07-1073-4cf3-a208-644e26b747d7"): &unmarshaledHandler{ + ident: MustNewIdentity("", "3c73fa07-1073-4cf3-a208-644e26b747d7"), + names: EntityMessageNames{ + Kinds: map[message.Name]message.Kind{ + message.NameOf(CommandA1): message.CommandKind, + message.NameOf(EventA1): message.EventKind, + }, + Produced: message.NamesOf(CommandA1), + Consumed: message.NamesOf(EventA1), + }, + typeName: "", + handlerType: IntegrationHandlerType, + }, + }, } }) + It("produces a value that can be unmarshaled to an equivalent application", func() { + marshaled, err := ToProto(app) + Expect(err).ShouldNot(HaveOccurred()) + + unmarshaled, err := FromProto(marshaled) + Expect(err).ShouldNot(HaveOccurred()) + + Expect(ToString(unmarshaled)).To(Equal(ToString(app))) + Expect(unmarshaled).To(Equal(app)) + Expect(IsApplicationEqual(unmarshaled, app)).To(BeTrue()) + }) + It("returns an error if the identity is invalid", func() { app.ident.Name = "" _, err := ToProto(app)