-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection.go
200 lines (163 loc) · 4.31 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package cmw
import (
"encoding/json"
"errors"
"fmt"
"github.com/fxamacker/cbor/v2"
)
type Collection struct {
m map[any]CMW
}
type CollectionSerialization uint
const (
UnknownCollectionSerialization = CollectionSerialization(iota)
CollectionSerializationJSON
CollectionSerializationCBOR
)
// Deserialize a JSON or CBOR collection
func (o *Collection) Deserialize(b []byte) error {
switch b[0] {
case 0x7b: // '{'
return o.UnmarshalJSON(b)
default:
return o.UnmarshalCBOR(b)
}
}
// 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 {
return nil, err
}
switch s {
case CollectionSerializationCBOR:
return o.MarshalCBOR()
case CollectionSerializationJSON:
return o.MarshalJSON()
default:
return nil, errors.New("unsupported serialization")
}
}
// GetMap returns a pointer to the internal map
func (o *Collection) GetMap() map[any]CMW {
return o.m
}
// GetItem returns the CMW associated with label k
func (o *Collection) GetItem(k any) (CMW, error) {
v, ok := o.m[k]
if !ok {
return CMW{}, fmt.Errorf("item not found for key %v", k)
}
return v, nil
}
// 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)
for i, v := range o.m {
c, err := v.Serialize()
if err != nil {
return nil, fmt.Errorf("marshaling JSON collection item %v: %w", i, err)
}
switch t := i.(type) {
case string:
m[t] = c
default:
return nil, fmt.Errorf("JSON collection, key error: want string, got %T", t)
}
}
b, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("marshaling JSON collection: %w", err)
}
return b, nil
}
// MarshalCBOR serializes the collection to CBOR
func (o Collection) MarshalCBOR() ([]byte, error) {
m := make(map[any]cbor.RawMessage)
for i, v := range o.m {
c, err := v.Serialize()
if err != nil {
return nil, fmt.Errorf("marshaling CBOR collection item %v: %w", i, err)
}
switch t := i.(type) {
case string, uint64:
m[t] = c
default:
return nil, fmt.Errorf("CBOR collection, key error: want string or int64, got %T", t)
}
}
b, err := cbor.Marshal(m)
if err != nil {
return nil, fmt.Errorf("marshaling CBOR collection: %w", err)
}
return b, nil
}
// UnmarshalCBOR unmarshal the supplied CBOR buffer to a CMW collection
func (o *Collection) UnmarshalCBOR(b []byte) error {
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
}
// UnmarshalJSON unmarshals the supplied JSON buffer to a CMW collection
func (o *Collection) UnmarshalJSON(b []byte) error {
var tmp map[string]json.RawMessage
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 JSON collection item %v: %w", k, err)
}
m[k] = c
}
o.m = m
return nil
}
func (o Collection) detectSerialization() (CollectionSerialization, error) {
rec := make(map[CollectionSerialization]bool)
s := UnknownCollectionSerialization
for k, v := range o.m {
switch v.serialization {
case CBORArray, CBORTag:
s = CollectionSerializationCBOR
rec[s] = true
case JSONArray:
s = CollectionSerializationJSON
rec[s] = true
default:
return UnknownCollectionSerialization,
fmt.Errorf(
"serialization not defined for collection item with k %v", k,
)
}
}
if len(rec) != 1 {
return UnknownCollectionSerialization,
errors.New("CMW collection has items with incompatible serializations")
}
return s, nil
}