Skip to content

Commit

Permalink
Add more marshaling tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Sep 29, 2024
1 parent a3bb58e commit c0ceeda
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 6 additions & 9 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
32 changes: 29 additions & 3 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,13 +17,38 @@ var _ = Describe("func ToProto()", func() {

BeforeEach(func() {
app = &unmarshaledApplication{
ident: MustNewIdentity("<name>", "28c19ec0-a32f-4479-bb1d-02887e90077c"),
ident: MustNewIdentity("<app>", "28c19ec0-a32f-4479-bb1d-02887e90077c"),
typeName: "<app type>",
names: EntityMessageNames{},
handlers: HandlerSet{},
handlers: HandlerSet{
MustNewIdentity("<handler>", "3c73fa07-1073-4cf3-a208-644e26b747d7"): &unmarshaledHandler{
ident: MustNewIdentity("<handler>", "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: "<handler type>",
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)
Expand Down

0 comments on commit c0ceeda

Please sign in to comment.