Skip to content

Commit

Permalink
Serialize the eventv1 from the eventsv1 API response (#2)
Browse files Browse the repository at this point in the history
* Serialize the eventv1 from the eventsv1 API response

re: AB#10260

---------

Co-authored-by: jgough <[email protected]>
  • Loading branch information
honourfish and jgough authored Dec 12, 2024
1 parent 20a4ba0 commit 13e1edb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 103 deletions.
5 changes: 2 additions & 3 deletions eventsv1/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
24 changes: 13 additions & 11 deletions eventsv1/go.sum
Original file line number Diff line number Diff line change
@@ -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=
33 changes: 21 additions & 12 deletions eventsv1/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package eventsv1
import (
"encoding/json"

"github.com/datatrails/go-datatrails-common-api-gen/attribute/v2/attribute"
"github.com/zeebo/bencode"
)

Expand All @@ -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)
Expand All @@ -48,4 +56,5 @@ func SerializeEvent(attributes map[string]*attribute.Attribute, trails []string)
}

return bencodeSerializedEvent, nil

}
Loading

0 comments on commit 13e1edb

Please sign in to comment.