From 13e1edbd822159dab9b7eab7f2919307d1b56700 Mon Sep 17 00:00:00 2001 From: Joe Gough <36932486+honourfish@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:19:45 +0000 Subject: [PATCH] Serialize the eventv1 from the eventsv1 API response (#2) * Serialize the eventv1 from the eventsv1 API response re: AB#10260 --------- Co-authored-by: jgough --- eventsv1/go.mod | 5 +- eventsv1/go.sum | 24 +++--- eventsv1/serializer.go | 33 ++++--- eventsv1/serializer_test.go | 168 +++++++++++++++++++----------------- 4 files changed, 127 insertions(+), 103 deletions(-) diff --git a/eventsv1/go.mod b/eventsv1/go.mod index 805ab87..2ed8060 100644 --- a/eventsv1/go.mod +++ b/eventsv1/go.mod @@ -3,15 +3,14 @@ module github.com/datatrails/go-datatrails-serialization/eventsv1 go 1.23.0 require ( - github.com/datatrails/go-datatrails-common-api-gen v0.6.1 github.com/stretchr/testify v1.10.0 github.com/zeebo/bencode v1.0.0 ) require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/eventsv1/go.sum b/eventsv1/go.sum index 34889ca..fd27e05 100644 --- a/eventsv1/go.sum +++ b/eventsv1/go.sum @@ -1,22 +1,24 @@ -github.com/datatrails/go-datatrails-common-api-gen v0.6.1 h1:rkzx2FBdTTNirLNLWHpXRme3GrOssPP27reQUwdFAJc= -github.com/datatrails/go-datatrails-common-api-gen v0.6.1/go.mod h1:rTMGdMdu5M6mGpbXZy1D84cBTGE8JwsDH6BYh9LJlmA= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A= -github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/zeebo/bencode v1.0.0 h1:zgop0Wu1nu4IexAZeCZ5qbsjU4O1vMrfCrVgUjbHVuA= github.com/zeebo/bencode v1.0.0/go.mod h1:Ct7CkrWIQuLWAy9M3atFHYq4kG9Ao/SsY5cdtCXmp9Y= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/eventsv1/serializer.go b/eventsv1/serializer.go index ea046b3..14069b1 100644 --- a/eventsv1/serializer.go +++ b/eventsv1/serializer.go @@ -3,7 +3,6 @@ package eventsv1 import ( "encoding/json" - "github.com/datatrails/go-datatrails-common-api-gen/attribute/v2/attribute" "github.com/zeebo/bencode" ) @@ -20,26 +19,35 @@ import ( // SerializableEvent is the event // containing only fields that will be serialized. type SerializableEvent struct { - Attributes map[string]*attribute.Attribute `json:"attributes"` - Trails []string `json:"trails"` + Attributes map[string]any `json:"attributes"` + Trails []string `json:"trails"` } -// SerializeEvent serializes a v1 event -func SerializeEvent(attributes map[string]*attribute.Attribute, trails []string) ([]byte, error) { +// SerializeEventFromJson serializes a v1 event from datatrails eventv1 api respone +func SerializeEventFromJson(eventJson []byte) ([]byte, error) { - // 1. take attributes and trails from v1 event - serializableEvent := SerializableEvent{ - Attributes: attributes, - Trails: trails, + serializableEvent := SerializableEvent{} + + // json marshal the attributes and trails + err := json.Unmarshal(eventJson, &serializableEvent) + if err != nil { + return nil, err } - // 2. json marshal the attributes and trails - jsonSerializedEvent, err := json.Marshal(&serializableEvent) + return serializableEvent.Serialize() + +} + +// Serialize the event +func (se *SerializableEvent) Serialize() ([]byte, error) { + + // json marshal the attributes and trails + jsonSerializedEvent, err := json.Marshal(&se) if err != nil { return nil, err } - // 3. bencode the json marshaled attributes and trails + // bencode the json marshaled attributes and trails // // NOTE: this gives a consistent ordering to the attributes that json doesn't give. bencodeSerializedEvent, err := bencode.EncodeBytes(jsonSerializedEvent) @@ -48,4 +56,5 @@ func SerializeEvent(attributes map[string]*attribute.Attribute, trails []string) } return bencodeSerializedEvent, nil + } diff --git a/eventsv1/serializer_test.go b/eventsv1/serializer_test.go index ba31482..8c2ae2d 100644 --- a/eventsv1/serializer_test.go +++ b/eventsv1/serializer_test.go @@ -3,7 +3,6 @@ package eventsv1 import ( "testing" - "github.com/datatrails/go-datatrails-common-api-gen/attribute/v2/attribute" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -15,9 +14,9 @@ import ( // 3. an event with the same attributes and same trails in a different order, serializes to different bytes. // 4. an event with the same trails and different attributes serializes to different bytes. // 5. an event with the same attributes and different trails, serializes to different bytes. -func TestSerializeEventConsistency(t *testing.T) { +func TestSerializeEventFromProtoConsistency(t *testing.T) { type event struct { - attributes map[string]*attribute.Attribute + attributes map[string]any trails []string } tests := []struct { @@ -29,12 +28,12 @@ func TestSerializeEventConsistency(t *testing.T) { { name: "same attributes and trails in same order", event1: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -42,12 +41,12 @@ func TestSerializeEventConsistency(t *testing.T) { }, }, event2: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -59,12 +58,12 @@ func TestSerializeEventConsistency(t *testing.T) { { name: "same attributes and trails, attributes in different order", event1: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -72,12 +71,12 @@ func TestSerializeEventConsistency(t *testing.T) { }, }, event2: event{ - attributes: map[string]*attribute.Attribute{ - "vanilla extract": attribute.NewStringAttribute("1 tsp"), - "flour": attribute.NewStringAttribute("500g"), - "milk": attribute.NewStringAttribute("300ml"), - "eggs": attribute.NewStringAttribute("2"), - "sugar": attribute.NewStringAttribute("250g"), + attributes: map[string]any{ + "vanilla extract": "1 tsp", + "flour": "500g", + "milk": "300ml", + "eggs": "2", + "sugar": "250g", }, trails: []string{ "cake", @@ -89,12 +88,12 @@ func TestSerializeEventConsistency(t *testing.T) { { name: "same attributes and trails, trails in different order", event1: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -102,12 +101,12 @@ func TestSerializeEventConsistency(t *testing.T) { }, }, event2: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "viccy sponge", @@ -119,12 +118,12 @@ func TestSerializeEventConsistency(t *testing.T) { { name: "same trails and different attributes in same order", event1: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -132,13 +131,13 @@ func TestSerializeEventConsistency(t *testing.T) { }, }, event2: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), - "bicarbonate of soda": attribute.NewStringAttribute("2 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", + "bicarbonate of soda": "2 tsp", }, trails: []string{ "cake", @@ -150,12 +149,12 @@ func TestSerializeEventConsistency(t *testing.T) { { name: "same attributes and different trails in same order", event1: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -163,12 +162,12 @@ func TestSerializeEventConsistency(t *testing.T) { }, }, event2: event{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "sugar": attribute.NewStringAttribute("250g"), - "eggs": attribute.NewStringAttribute("2"), - "milk": attribute.NewStringAttribute("300ml"), - "vanilla extract": attribute.NewStringAttribute("1 tsp"), + attributes: map[string]any{ + "flour": "500g", + "sugar": "250g", + "eggs": "2", + "milk": "300ml", + "vanilla extract": "1 tsp", }, trails: []string{ "cake", @@ -181,10 +180,20 @@ func TestSerializeEventConsistency(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - serializedEvent1, err := SerializeEvent(test.event1.attributes, test.event1.trails) + serializableEvent1 := SerializableEvent{ + Attributes: test.event1.attributes, + Trails: test.event1.trails, + } + + serializableEvent2 := SerializableEvent{ + Attributes: test.event2.attributes, + Trails: test.event2.trails, + } + + serializedEvent1, err := serializableEvent1.Serialize() require.Nil(t, err) - serializedEvent2, err := SerializeEvent(test.event2.attributes, test.event2.trails) + serializedEvent2, err := serializableEvent2.Serialize() require.Nil(t, err) if test.sameSerialization { @@ -197,12 +206,12 @@ func TestSerializeEventConsistency(t *testing.T) { } } -// TestSerializeEvent tests: +// TestSerializableEvent_Serialize tests: // // 1. an event with all types of attributes [string|list|dict] can be serialized without error. -func TestSerializeEvent(t *testing.T) { +func TestSerializableEvent_Serialize(t *testing.T) { type args struct { - attributes map[string]*attribute.Attribute + attributes map[string]any trails []string } tests := []struct { @@ -213,16 +222,16 @@ func TestSerializeEvent(t *testing.T) { { name: "all attribute types no error", args: args{ - attributes: map[string]*attribute.Attribute{ - "flour": attribute.NewStringAttribute("500g"), - "method": attribute.NewListAttribute([]map[string]string{ + attributes: map[string]any{ + "flour": "500g", + "method": []map[string]string{ {"1": "put flour sugar into mixing bowl"}, {"2": "put in eggs and mix"}, {"3": "put in milk and mix"}, - }), - "baking time": attribute.NewDictAttribute(map[string]string{ + }, + "baking time": map[string]string{ "oven time": "30 mins", - }), + }, }, trails: []string{ "cake", @@ -235,9 +244,14 @@ func TestSerializeEvent(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - actual, err := SerializeEvent(test.args.attributes, test.args.trails) - require.NotNil(t, actual) + serializableEvent := SerializableEvent{ + Attributes: test.args.attributes, + Trails: test.args.trails, + } + actual, err := serializableEvent.Serialize() + + require.NotNil(t, actual) assert.Equal(t, test.err, err) })