forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.go
347 lines (274 loc) · 7.89 KB
/
entity.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2021 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"errors"
"fmt"
"unicode/utf8"
"github.com/jraman567/corim/encoding"
"github.com/jraman567/corim/extensions"
)
// Entity stores an entity-map capable of CBOR and JSON serializations.
type Entity struct {
EntityName *EntityName `cbor:"0,keyasint" json:"name"`
RegID *TaggedURI `cbor:"1,keyasint,omitempty" json:"regid,omitempty"`
Roles Roles `cbor:"2,keyasint" json:"roles"`
Extensions
}
// RegisterExtensions registers a struct as a collections of extensions
func (o *Entity) RegisterExtensions(exts extensions.IExtensionsValue) {
o.Extensions.Register(exts)
}
// GetExtensions returns pervisouosly registered extension
func (o *Entity) GetExtensions() extensions.IExtensionsValue {
return o.Extensions.IExtensionsValue
}
// SetEntityName is used to set the EntityName field of Entity using supplied name
func (o *Entity) SetEntityName(name string) *Entity {
if o != nil {
if name == "" {
return nil
}
o.EntityName = MustNewStringEntityName(name)
}
return o
}
// SetRegID is used to set the RegID field of Entity using supplied uri
func (o *Entity) SetRegID(uri string) *Entity {
if o != nil {
if uri == "" {
return nil
}
taggedURI := TaggedURI(uri)
o.RegID = &taggedURI
}
return o
}
// SetRoles appends the supplied roles to the target entity.
func (o *Entity) SetRoles(roles ...Role) *Entity {
if o != nil {
o.Roles.Add(roles...)
}
return o
}
// Valid checks for validity of the fields within each Entity
func (o Entity) Valid() error {
if o.EntityName == nil {
return fmt.Errorf("invalid entity: empty entity-name")
}
if err := o.EntityName.Valid(); err != nil {
return fmt.Errorf("invalid entity: %w", err)
}
if o.RegID != nil && o.RegID.Empty() {
return fmt.Errorf("invalid entity: empty reg-id")
}
if err := o.Roles.Valid(); err != nil {
return fmt.Errorf("invalid entity: %w", err)
}
return o.Extensions.validEntity(&o)
}
// UnmarshalCBOR deserializes from CBOR
func (o *Entity) UnmarshalCBOR(data []byte) error {
return encoding.PopulateStructFromCBOR(dm, data, o)
}
// MarshalCBOR serializes to CBOR
func (o *Entity) MarshalCBOR() ([]byte, error) {
return encoding.SerializeStructToCBOR(em, o)
}
// UnmarshalJSON deserializes from JSON
func (o *Entity) UnmarshalJSON(data []byte) error {
return encoding.PopulateStructFromJSON(data, o)
}
// MarshalJSON serializes to JSON
func (o *Entity) MarshalJSON() ([]byte, error) {
return encoding.SerializeStructToJSON(o)
}
// Entities is an array of entity-map's
type Entities []Entity
// NewEntities instantiates an empty entity-map array
func NewEntities() *Entities {
return new(Entities)
}
// AddEntity adds the supplied entity-map to the target Entities
func (o *Entities) AddEntity(e Entity) *Entities {
if o != nil {
*o = append(*o, e)
}
return o
}
// Valid iterates over the range of individual entities to check for validity
func (o Entities) Valid() error {
for i, m := range o {
if err := m.Valid(); err != nil {
return fmt.Errorf("entity at index %d: %w", i, err)
}
}
return nil
}
// EntityName encapsulates the name of the associated Entity. The CoRIM
// specification only allows for text (string) name, but this may be extended
// by other specifications.
type EntityName struct {
Value IEntityNameValue
}
// NewEntityName creates a new EntityName of the specified type using the
// provided value.
func NewEntityName(val any, typ string) (*EntityName, error) {
factory, ok := entityNameValueRegister[typ]
if !ok {
return nil, fmt.Errorf("unexpected entity name type: %s", typ)
}
return factory(val)
}
// MustNewEntityName is like NewEntityName, except it doesn't return an error,
// assuming that the provided value is valid. It panics if that isn't the case.
func MustNewEntityName(val any, typ string) *EntityName {
ret, err := NewEntityName(val, typ)
if err != nil {
panic(err)
}
return ret
}
func (o EntityName) String() string {
return o.Value.String()
}
func (o EntityName) Valid() error {
if o.Value == nil {
return errors.New("empty entity name")
}
return o.Value.Valid()
}
func (o EntityName) MarshalCBOR() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
return em.Marshal(o.Value)
}
func (o *EntityName) UnmarshalCBOR(data []byte) error {
if len(data) == 0 {
return errors.New("empty")
}
majorType := (data[0] & 0xe0) >> 5
if majorType == 3 { // text string
var text string
if err := dm.Unmarshal(data, &text); err != nil {
return err
}
name := StringEntityName(text)
o.Value = &name
return nil
}
return dm.Unmarshal(data, &o.Value)
}
func (o EntityName) MarshalJSON() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
if o.Value.Type() == extensions.StringType {
return json.Marshal(o.Value.String())
}
return extensions.TypeChoiceValueMarshalJSON(o.Value)
}
func (o *EntityName) UnmarshalJSON(data []byte) error {
var text string
if err := json.Unmarshal(data, &text); err == nil {
*o = *MustNewStringEntityName(text)
return nil
}
var tnv encoding.TypeAndValue
if err := json.Unmarshal(data, &tnv); err != nil {
return fmt.Errorf("entity name decoding failure: %w", err)
}
decoded, err := NewEntityName(nil, tnv.Type)
if err != nil {
return err
}
if err := json.Unmarshal(tnv.Value, &decoded.Value); err != nil {
return fmt.Errorf(
"cannot unmarshal entity name: %w",
err,
)
}
if err := decoded.Value.Valid(); err != nil {
return fmt.Errorf("invalid %s: %w", tnv.Type, err)
}
o.Value = decoded.Value
return nil
}
type IEntityNameValue interface {
extensions.ITypeChoiceValue
}
type StringEntityName string
func NewStringEntityName(val any) (*EntityName, error) {
var ret StringEntityName
if val == nil {
ret = StringEntityName("")
return &EntityName{&ret}, nil
}
switch t := val.(type) {
case string:
ret = StringEntityName(t)
case []byte:
if !utf8.Valid(t) {
return nil, errors.New("bytes do not form a valid UTF-8 string")
}
ret = StringEntityName(t)
default:
return nil, fmt.Errorf("unexpected type for string entity name: %T", t)
}
return &EntityName{&ret}, nil
}
func MustNewStringEntityName(val any) *EntityName {
ret, err := NewStringEntityName(val)
if err != nil {
panic(err)
}
return ret
}
func (o StringEntityName) String() string {
return string(o)
}
func (o StringEntityName) Type() string {
return extensions.StringType
}
func (o StringEntityName) Valid() error {
if o == "" {
return errors.New("empty entity-name")
}
return nil
}
// IEntityNameFactory defines the signature for the factory functions that may
// be registred using RegisterEntityNameType to provide a new implementation of
// the corresponding type choice. The factory function should create a new
// *EntityName with the underlying value created based on the provided input.
// The range of valid inputs is up to the specific type choice implementation,
// however it _must_ accept nil as one of the inputs, and return the Zero value
// for implemented type.
// See also https://go.dev/ref/spec#The_zero_value
type IEntityNameFactory func(any) (*EntityName, error)
var entityNameValueRegister = map[string]IEntityNameFactory{
extensions.StringType: NewStringEntityName,
}
// RegisterEntityNameType registers a new IEntityNameValue implementation
// (created by the provided IEntityNameFactory) under the specified type name
// and CBOR tag.
func RegisterEntityNameType(tag uint64, factory IEntityNameFactory) error {
nilVal, err := factory(nil)
if err != nil {
return err
}
typ := nilVal.Value.Type()
if _, exists := entityNameValueRegister[typ]; exists {
return fmt.Errorf("entity name type with name %q already exists", typ)
}
if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}
entityNameValueRegister[typ] = factory
return nil
}
type TaggedURI string
func (o TaggedURI) Empty() bool {
return o == ""
}