Skip to content

Commit 3f9643d

Browse files
Introducing Metadata.UnmarshalJSON
Let the metadata package own the json unmarshaling. This way we do not need to maintain a public wrapper and it simply's the code using metadata a little as well.
1 parent 723b3c8 commit 3f9643d

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

metadata/metadata.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metadata
33
import (
44
"encoding/json"
55

6+
"github.com/mailru/easyjson"
67
"github.com/mailru/easyjson/jlexer"
78
)
89

@@ -98,25 +99,24 @@ func (v *valueData) MarshalJSON() ([]byte, error) {
9899
return json.Marshal(v.AsMap())
99100
}
100101

101-
// JSONMetadata is a special struct to UnmarshalJSON metadata
102-
type JSONMetadata struct {
103-
Metadata Metadata
104-
}
102+
func UnmarshalJSON(json []byte) (Metadata, error) {
103+
wrapper := jsonMetadata{
104+
Metadata: New(),
105+
}
105106

106-
var (
107-
// Ensure JSONMetadata implements the json.Unmarshaler interface
108-
_ json.Unmarshaler = &JSONMetadata{}
109-
)
107+
if err := easyjson.Unmarshal(json, &wrapper); err != nil {
108+
return nil, err
109+
}
110+
return wrapper.Metadata, nil
111+
}
110112

111-
// UnmarshalJSON unmarshal the json into Metdadata
112-
func (j *JSONMetadata) UnmarshalJSON(data []byte) error {
113-
r := jlexer.Lexer{Data: data}
114-
j.UnmarshalEasyJSON(&r)
115-
return r.Error()
113+
// jsonMetadata is a special struct to UnmarshalJSON metadata
114+
type jsonMetadata struct {
115+
Metadata Metadata
116116
}
117117

118118
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
119-
func (j *JSONMetadata) UnmarshalEasyJSON(in *jlexer.Lexer) {
119+
func (j *jsonMetadata) UnmarshalEasyJSON(in *jlexer.Lexer) {
120120
metadata := New()
121121

122122
isTopLevel := in.IsStart()

metadata/metadata_test.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/hellofresh/goengine/metadata"
11-
"github.com/mailru/easyjson"
1211
"github.com/stretchr/testify/assert"
1312
)
1413

@@ -215,12 +214,11 @@ func TestMetadata_MarshalJSON(t *testing.T) {
215214
func TestJSONMetadata_UnmarshalJSON(t *testing.T) {
216215
for _, testCase := range jsonTestCases {
217216
t.Run(testCase.title, func(t *testing.T) {
218-
var m metadata.JSONMetadata
219-
err := json.Unmarshal([]byte(testCase.json), &m)
217+
m, err := metadata.UnmarshalJSON([]byte(testCase.json))
220218

221219
// Need to use AsMap otherwise we can have inconsistent tests results.
222220
if assert.NoError(t, err) {
223-
assert.Equal(t, testCase.metadata().AsMap(), m.Metadata.AsMap())
221+
assert.Equal(t, testCase.metadata().AsMap(), m.AsMap())
224222
}
225223
})
226224
}
@@ -231,21 +229,7 @@ func BenchmarkJSONMetadata_UnmarshalJSON(b *testing.B) {
231229

232230
b.ResetTimer()
233231
for i := 0; i < b.N; i++ {
234-
var m metadata.JSONMetadata
235-
err := json.Unmarshal(payload, &m)
236-
if err != nil {
237-
b.Fail()
238-
}
239-
}
240-
}
241-
242-
func BenchmarkJSONMetadata_UnmarshalEasyJSON(b *testing.B) {
243-
payload := []byte(`{"_aggregate_id": "b9ebca7a-c1eb-40dd-94a4-fac7c5e84fb5", "_aggregate_type": "bank_account", "_aggregate_version": 1}`)
244-
245-
b.ResetTimer()
246-
for i := 0; i < b.N; i++ {
247-
var m metadata.JSONMetadata
248-
err := easyjson.Unmarshal(payload, &m)
232+
_, err := metadata.UnmarshalJSON(payload)
249233
if err != nil {
250234
b.Fail()
251235
}

strategy/json/sql/message_factory_aggregate.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hellofresh/goengine/aggregate"
1010
driverSQL "github.com/hellofresh/goengine/driver/sql"
1111
"github.com/hellofresh/goengine/metadata"
12-
"github.com/mailru/easyjson"
1312
)
1413

1514
// Ensure that AggregateChangedFactory satisfies the MessageFactory interface
@@ -78,11 +77,10 @@ func (a *aggregateChangedEventStream) Message() (goengine.Message, int64, error)
7877
return nil, 0, err
7978
}
8079

81-
metadataWrapper := metadata.JSONMetadata{Metadata: metadata.New()}
82-
if err := easyjson.Unmarshal(jsonMetadata, &metadataWrapper); err != nil {
80+
meta, err := metadata.UnmarshalJSON(jsonMetadata)
81+
if err != nil {
8382
return nil, 0, err
8483
}
85-
meta := metadataWrapper.Metadata
8684

8785
payload, err := a.payloadFactory.CreatePayload(eventName, jsonPayload)
8886
if err != nil {

0 commit comments

Comments
 (0)