Skip to content

Commit b84add1

Browse files
feat: add a Decode function
This is just an improvement to the API ergonomics, to avoid having to declare a CMW object before invoking the deserialisation logic. Consequently, the existing (*CMW)Deserialize method has been deprecated and will be retired at some point in the future. Signed-off-by: Thomas Fossati <[email protected]>
1 parent d7f7ea6 commit b84add1

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

cmw.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,19 @@ func (o *CMW) UnmarshalCBOR(b []byte) error {
273273
return nil
274274
}
275275

276+
// Decode decodes a JSON- or CBOR-encoded CMW to a CMW object.
277+
// On success, a pointer to the newly allocated CMW structure is returned.
278+
func Decode(b []byte) (*CMW, error) {
279+
var o CMW
280+
if err := o.Deserialize(b); err != nil {
281+
return nil, err
282+
}
283+
return &o, nil
284+
}
285+
286+
// Deserialize deserializes a JSON- or CBOR-encoded CMW into the target CMW object.
287+
//
288+
// Deprecated: use Decode instead.
276289
func (o *CMW) Deserialize(b []byte) error {
277290
if len(b) == 0 {
278291
return errors.New("empty buffer")

collection_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,19 @@ func Test_Collection_CBOR_Deserialize_and_iterate(t *testing.T) {
166166
}
167167
}
168168

169-
func Test_Collection_Deserialize_JSON_ok(t *testing.T) {
169+
func Test_Collection_Decode_JSON_ok(t *testing.T) {
170170
tv := mustReadFile(t, "testdata/collection-ok.json")
171171

172-
var c CMW
173-
err := c.Deserialize(tv)
172+
c, err := Decode(tv)
174173
assert.NoError(t, err)
175174
assert.Equal(t, KindCollection, c.GetKind())
176175
assert.Equal(t, FormatJSONCollection, c.GetFormat())
177176
}
178177

179-
func Test_Collection_Deserialize_CBOR_ok(t *testing.T) {
178+
func Test_Collection_Decode_CBOR_ok(t *testing.T) {
180179
tv := mustReadFile(t, "testdata/collection-cbor-ok.cbor")
181180

182-
var c CMW
183-
err := c.Deserialize(tv)
181+
c, err := Decode(tv)
184182
assert.NoError(t, err)
185183
assert.Equal(t, KindCollection, c.GetKind())
186184
assert.Equal(t, FormatCBORCollection, c.GetFormat())

example_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ func Example_decode_JSON_record() {
116116
}
117117

118118
func Example_roundtrip_JSON_collection() {
119-
var o CMW
120-
121119
ex := []byte(`{
122120
"bretwaldadom": [
123121
"application/eat-ucs+cbor",
@@ -143,7 +141,7 @@ func Example_roundtrip_JSON_collection() {
143141
]
144142
}`)
145143

146-
err := o.Deserialize(ex)
144+
o, err := Decode(ex)
147145
if err != nil {
148146
log.Fatalf("unmarshal JSON collection failed: %v", err)
149147
}

monad_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ func Test_Deserialize_monad_ok(t *testing.T) {
8282

8383
for _, tt := range tests {
8484
t.Run(tt.name, func(t *testing.T) {
85-
var actual CMW
86-
87-
err := actual.Deserialize(tt.tv)
85+
actual, err := Decode(tt.tv)
8886
assert.NoError(t, err)
8987
err = actual.monad.validate()
9088
assert.NoError(t, err)
@@ -410,8 +408,7 @@ func Test_UnmarshalCBOR_tag_ko(t *testing.T) {
410408

411409
for _, tt := range tests {
412410
t.Run(tt.name, func(t *testing.T) {
413-
var cmw CMW
414-
err := cmw.Deserialize(tt.tv)
411+
_, err := Decode(tt.tv)
415412
assert.EqualError(t, err, tt.expectedErr)
416413
})
417414
}

0 commit comments

Comments
 (0)