Skip to content

Commit

Permalink
more work on serialization fmt
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Fossati <[email protected]>
  • Loading branch information
thomas-fossati committed Dec 27, 2023
1 parent d15d726 commit 76a075a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
45 changes: 32 additions & 13 deletions cmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
type Serialization uint

const (
JSONArray = Serialization(iota)
UnknownSerialization = Serialization(iota)
JSONArray
CBORArray
CBORTag
UnknownSerialization
)

// a CMW object holds the internal representation of a RATS conceptual message
Expand Down Expand Up @@ -58,10 +58,8 @@ func (o *CMW) Deserialize(b []byte) error {
switch s {
case JSONArray:
return o.UnmarshalJSON(b)
case CBORArray:
case CBORArray, CBORTag:
return o.UnmarshalCBOR(b)
case CBORTag:
return o.UnmarshalCBORTag(b)
}

return errors.New("unknown CMW format")
Expand All @@ -73,18 +71,26 @@ func (o CMW) Serialize() ([]byte, error) {
switch s {
case JSONArray:
return o.MarshalJSON()
case CBORArray:
case CBORArray, CBORTag:
return o.MarshalCBOR()
case CBORTag:
return o.MarshalCBORTag()
}
return nil, fmt.Errorf("invalid serialization format %d", s)
}

func (o CMW) MarshalJSON() ([]byte, error) { return arrayEncode(json.Marshal, &o) }
func (o CMW) MarshalCBOR() ([]byte, error) { return arrayEncode(cbor.Marshal, &o) }

func (o CMW) MarshalCBORTag() ([]byte, error) {
func (o CMW) MarshalCBOR() ([]byte, error) {
s := o.serialization
switch s {
case CBORArray:
return arrayEncode(cbor.Marshal, &o)
case CBORTag:
return o.encodeCBORTag()
}
return nil, fmt.Errorf("invalid serialization format: want CBORArray or CBORTag, got %d", s)
}

func (o CMW) encodeCBORTag() ([]byte, error) {
var (
tag cbor.RawTag
err error
Expand All @@ -108,14 +114,26 @@ func (o CMW) MarshalCBORTag() ([]byte, error) {
}

func (o *CMW) UnmarshalCBOR(b []byte) error {
return arrayDecode[cbor.RawMessage](cbor.Unmarshal, b, o)
if arrayDecode[cbor.RawMessage](cbor.Unmarshal, b, o) == nil {
o.serialization = CBORArray
return nil
}

if o.decodeCBORTag(b) == nil {
// the serialization attribute is set by decodeCBORTag
return nil
}

return errors.New("invalid CBOR-encoded CMW")
}

func (o *CMW) UnmarshalJSON(b []byte) error {
return arrayDecode[json.RawMessage](json.Unmarshal, b, o)
err := arrayDecode[json.RawMessage](json.Unmarshal, b, o)
o.serialization = JSONArray
return err
}

func (o *CMW) UnmarshalCBORTag(b []byte) error {
func (o *CMW) decodeCBORTag(b []byte) error {
var (
v cbor.RawTag
m []byte
Expand All @@ -132,6 +150,7 @@ func (o *CMW) UnmarshalCBORTag(b []byte) error {

_ = o.typ.Set(v.Number)
_ = o.val.Set(m)
o.serialization = CBORTag

return nil
}
Expand Down
20 changes: 15 additions & 5 deletions cmw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func Test_Deserialize_CBORArray_ko(t *testing.T) {
0x82, 0xfb, 0x40, 0x8f, 0x41, 0xd7, 0x0a, 0x3d, 0x70, 0xa4,
0x44, 0xde, 0xad, 0xbe, 0xef,
},
`unmarshaling type: cannot unmarshal 1000.230000 into uint16`,
`invalid CBOR-encoded CMW`,
},
{
"overflow for type",
Expand All @@ -459,7 +459,7 @@ func Test_Deserialize_CBORArray_ko(t *testing.T) {
0x82, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x44, 0xde, 0xad, 0xbe,
0xef,
},
`unmarshaling type: cannot unmarshal 65536 into uint16`,
`invalid CBOR-encoded CMW`,
},
{
"bad type (float) for value",
Expand All @@ -468,7 +468,7 @@ func Test_Deserialize_CBORArray_ko(t *testing.T) {
0x82, 0x19, 0xff, 0xff, 0xfb, 0x3f, 0xf3, 0x33, 0x33, 0x33,
0x33, 0x33, 0x33,
},
`unmarshaling value: cannot decode value: cbor: cannot unmarshal primitives into Go value of type []uint8`,
`invalid CBOR-encoded CMW`,
},
}

Expand All @@ -490,13 +490,13 @@ func Test_Deserialize_CBORTag(t *testing.T) {
{
"empty CBOR Tag",
[]byte{0xda, 0x63, 0x74, 0x01, 0x01},
`unmarshal CMW CBOR Tag bstr-wrapped value: EOF`,
`invalid CBOR-encoded CMW`,
},
{
"bad type (uint) for value",
// echo "1668546817(1)" | diag2cbor.rb | xxd -i
[]byte{0xda, 0x63, 0x74, 0x01, 0x01, 0x01},
`unmarshal CMW CBOR Tag bstr-wrapped value: cbor: cannot unmarshal positive integer into Go value of type []uint8`,
`invalid CBOR-encoded CMW`,
},
}

Expand Down Expand Up @@ -542,3 +542,13 @@ func Test_EncodeArray_sanitize_input(t *testing.T) {
assert.NoError(t, err)
}
}

func Test_Serialize_invalid_serialization(t *testing.T) {
var tv CMW

tv.SetMediaType("application/vnd.x")
tv.SetValue([]byte{0x00})

_, err := tv.Serialize()
assert.Error(t, err, "TPDP")
}
26 changes: 5 additions & 21 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ func mustReadFile(t *testing.T, fname string) []byte {
}

func Test_Collection_JSON_Deserialize_ok(t *testing.T) {
tv := []byte(`{
"a": [
"application/vnd.a",
"YQ"
],
"b": [
"application/vnd.b",
"Yg"
]
}`)
tv := mustReadFile(t, "testdata/collection-ok.json")

var expectedA CMW
expectedA.SetMediaType("application/vnd.a")
expectedA.SetValue([]byte{0x61})
Expand All @@ -36,7 +28,7 @@ func Test_Collection_JSON_Deserialize_ok(t *testing.T) {
var expectedB CMW
expectedB.SetMediaType("application/vnd.b")
expectedB.SetValue([]byte{0x62})
expectedA.SetSerialization(JSONArray)
expectedB.SetSerialization(JSONArray)

var actual Collection
err := actual.UnmarshalJSON(tv)
Expand All @@ -52,16 +44,8 @@ func Test_Collection_JSON_Deserialize_ok(t *testing.T) {
}

func Test_Collection_JSON_Serialize_ok(t *testing.T) {
expected := []byte(`{
"a": [
"application/vnd.a",
"YQ"
],
"b": [
"application/vnd.b",
"Yg"
]
}`)
expected := mustReadFile(t, "testdata/collection-ok.json")

var tv Collection

var a CMW
Expand Down
10 changes: 10 additions & 0 deletions testdata/collection-ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"a": [
"application/vnd.a",
"YQ"
],
"b": [
"application/vnd.b",
"Yg"
]
}

0 comments on commit 76a075a

Please sign in to comment.