Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: veraison/cmw
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a29f60a962173eafc88e3d9c3a7fc00bf4f58b65
Choose a base ref
..
head repository: veraison/cmw
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1e721a531c92e20e28ea30dbb497793d7682d11d
Choose a head ref
Showing with 15 additions and 9 deletions.
  1. +10 −4 collection.go
  2. +5 −5 collection_test.go
14 changes: 10 additions & 4 deletions collection.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ const (
CollectionSerializationCBOR
)

// Deserialize a JSON or CBOR collection
func (o *Collection) Deserialize(b []byte) error {
switch b[0] {
case 0x7b: // '{'
@@ -32,6 +33,9 @@ func (o *Collection) Deserialize(b []byte) error {
}
}

// Serialize the collection. The type of serialization depends on the
// serialization specified for each item. Items must have compatible
// serializations: CBORArray/CBORTag or JSON.
func (o *Collection) Serialize() ([]byte, error) {
s, err := o.detectSerialization()
if err != nil {
@@ -62,14 +66,15 @@ func (o *Collection) GetItem(k any) (CMW, error) {
return v, nil
}

// SetItem adds a new item with label k to the collection
func (o *Collection) SetItem(k any, c CMW) {
// AddItem adds a new item with label k to the collection
func (o *Collection) AddItem(k any, c CMW) {
if o.m == nil {
o.m = make(map[any]CMW)
}
o.m[k] = c
}

// MarshalJSON serializes the collection to JSON
func (o Collection) MarshalJSON() ([]byte, error) {
m := make(map[string]json.RawMessage)

@@ -94,6 +99,7 @@ func (o Collection) MarshalJSON() ([]byte, error) {
return b, nil
}

// MarshalCBOR serializes the collection to CBOR
func (o Collection) MarshalCBOR() ([]byte, error) {
m := make(map[any]cbor.RawMessage)

@@ -118,7 +124,7 @@ func (o Collection) MarshalCBOR() ([]byte, error) {
return b, nil
}

// UnmarshalCBOR the supplied CBOR to a CMW collection
// UnmarshalCBOR unmarshal the supplied CBOR buffer to a CMW collection
func (o *Collection) UnmarshalCBOR(b []byte) error {
var tmp map[any]cbor.RawMessage

@@ -141,7 +147,7 @@ func (o *Collection) UnmarshalCBOR(b []byte) error {
return nil
}

// UnmarshalCBOR the supplied JSON to a CMW collection
// UnmarshalJSON unmarshals the supplied JSON buffer to a CMW collection
func (o *Collection) UnmarshalJSON(b []byte) error {
var tmp map[string]json.RawMessage

10 changes: 5 additions & 5 deletions collection_test.go
Original file line number Diff line number Diff line change
@@ -53,14 +53,14 @@ func Test_Collection_JSON_Serialize_ok(t *testing.T) {
a.SetValue([]byte{0x61})
a.SetSerialization(JSONArray)

tv.SetItem("a", a)
tv.AddItem("a", a)

var b CMW
b.SetMediaType("application/vnd.b")
b.SetValue([]byte{0x62})
b.SetSerialization(JSONArray)

tv.SetItem("b", b)
tv.AddItem("b", b)

actual, err := tv.Serialize()
assert.NoError(t, err)
@@ -120,7 +120,7 @@ func Test_Collection_CBOR_Serialize_ok(t *testing.T) {
item1.SetSerialization(CBORArray)

var tv Collection
tv.SetItem(uint64(1), item1)
tv.AddItem(uint64(1), item1)

expected := mustReadFile(t, "testdata/collection-cbor-ok-2.cbor")

@@ -156,14 +156,14 @@ func Test_Collection_detectSerialization_fail(t *testing.T) {
a.SetValue([]byte{0x61})
a.SetSerialization(JSONArray)

tv.SetItem("a", a)
tv.AddItem("a", a)

var b CMW
b.SetMediaType("application/vnd.b")
b.SetValue([]byte{0x62})
b.SetSerialization(CBORArray)

tv.SetItem("b", b)
tv.AddItem("b", b)

s, err := tv.detectSerialization()
assert.EqualError(t, err, "CMW collection has items with incompatible serializations")