Skip to content

Commit

Permalink
revert to 1.19
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 4097d17 commit 5fea91e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-go-cover.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: "1.20"
go-version: "1.19"
- name: Checkout code
uses: actions/checkout@v2
- name: Go Coverage
Expand Down
42 changes: 23 additions & 19 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,41 @@ func (o Collection) MarshalCBOR() ([]byte, error) {

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

if err := cbor.Unmarshal(b, &tmp); err != nil {
return fmt.Errorf("unmarshaling CBOR collection: %w", err)
}

m := make(map[any]CMW)

for k, v := range tmp {
var c CMW
if err := c.Deserialize(v); err != nil {
return fmt.Errorf("unmarshaling CBOR collection item %v: %w", k, err)
}
m[k] = c
}

o.m = m

return nil
}

// UnmarshalCBOR the supplied JSON to a CMW collection
func (o *Collection) UnmarshalJSON(b []byte) error {
return decode[string, json.RawMessage]("JSON", json.Unmarshal, b, o)
}

type (
collectionDecoder func([]byte, any) error
)
var tmp map[string]json.RawMessage

// decode the supplied JSON or CBOR to a CMW collection
func decode[K comparable, V cbor.RawMessage | json.RawMessage](
kind string,
unmarshal collectionDecoder,
b []byte,
o *Collection,
) error {
var tmp map[K]V

if err := unmarshal(b, &tmp); err != nil {
return fmt.Errorf("unmarshaling %s collection: %w", kind, err)
if err := json.Unmarshal(b, &tmp); err != nil {
return fmt.Errorf("unmarshaling JSON collection: %w", err)
}

m := make(map[any]CMW)

for k, v := range tmp {
var c CMW
if err := c.Deserialize(v); err != nil {
return fmt.Errorf("unmarshaling %s collection item %v: %w", kind, k, err)
return fmt.Errorf("unmarshaling JSON collection item %v: %w", k, err)
}
m[k] = c
}
Expand Down
5 changes: 0 additions & 5 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,8 @@ func Test_Collection_CBOR_Serialize_ok(t *testing.T) {
item1.SetMediaType("application/vnd.1")
item1.SetValue([]byte{0xde, 0xad, 0xbe, 0xef})

var item2 CMW
item2.SetContentFormat(0)
item2.SetValue([]byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a})

var tv Collection
tv.SetItem(uint64(1), item1)
tv.SetItem(uint64(2), item2)

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

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/veraison/cmw

go 1.20
go 1.19

require (
github.com/fxamacker/cbor/v2 v2.4.0
Expand Down
Binary file modified testdata/collection-cbor-ok-2.cbor
Binary file not shown.
4 changes: 0 additions & 4 deletions testdata/collection-cbor-ok-2.diag
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
1: [
"application/vnd.1",
h'DEADBEEF'
],
2: [
0,
h'68656C6C6F0A'
]
}

0 comments on commit 5fea91e

Please sign in to comment.