-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgroup.go
197 lines (158 loc) · 4.39 KB
/
group.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"errors"
"fmt"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)
// Group stores a group identity. The supported formats are UUID and variable-length opaque bytes.
type Group struct {
Value IGroupValue
}
// NewGroup instantiates an empty group
func NewGroup(val any, typ string) (*Group, error) {
factory, ok := groupValueRegister[typ]
if !ok {
return nil, fmt.Errorf("unknown group type: %s", typ)
}
return factory(val)
}
// Valid checks for the validity of given group
func (o Group) Valid() error {
if o.Value == nil {
return errors.New("no value set")
}
return o.Value.Valid()
}
// String returns a printable string of the Group value. UUIDs use the
// canonical 8-4-4-4-12 format, UEIDs are hex encoded.
func (o Group) String() string {
return o.Value.String()
}
// Type returns the type of the Group
func (o Group) Type() string {
if o.Value == nil {
return ""
}
return o.Value.Type()
}
// Bytes returns a []byte containing the raw bytes of the group value
func (o Group) Bytes() []byte {
if o.Value == nil {
return []byte{}
}
return o.Value.Bytes()
}
// MarshalCBOR serializes the target group to CBOR
func (o Group) MarshalCBOR() ([]byte, error) {
return em.Marshal(o.Value)
}
// UnmarshalCBOR deserializes the supplied CBOR into the target group
func (o *Group) UnmarshalCBOR(data []byte) error {
return dm.Unmarshal(data, &o.Value)
}
// UnmarshalJSON deserializes the supplied JSON type/value object into the Group
// target. The following formats are supported:
//
// (a) UUID, e.g.:
// {
// "type": "uuid",
// "value": "69E027B2-7157-4758-BCB4-D9F167FE49EA"
// }
//
// (b) Tagged bytes, e.g. :
//
// {
// "type": "bytes",
// "value": "MTIzNDU2Nzg5"
// }
//nolint:dupl
func (o *Group) UnmarshalJSON(data []byte) error {
var tnv encoding.TypeAndValue
if err := json.Unmarshal(data, &tnv); err != nil {
return fmt.Errorf("group decoding failure: %w", err)
}
decoded, err := NewGroup(nil, tnv.Type)
if err != nil {
return err
}
if err := json.Unmarshal(tnv.Value, &decoded.Value); err != nil {
return fmt.Errorf(
"cannot unmarshal group: %w",
err,
)
}
if err := decoded.Value.Valid(); err != nil {
return fmt.Errorf("invalid %s: %w", tnv.Type, err)
}
o.Value = decoded.Value
return nil
}
func (o Group) MarshalJSON() ([]byte, error) {
return extensions.TypeChoiceValueMarshalJSON(o.Value)
}
type IGroupValue interface {
extensions.ITypeChoiceValue
Bytes() []byte
}
func NewUUIDGroup(val any) (*Group, error) {
if val == nil {
return &Group{&TaggedUUID{}}, nil
}
u, err := NewTaggedUUID(val)
if err != nil {
return nil, err
}
return &Group{u}, nil
}
func MustNewUUIDGroup(val any) *Group {
ret, err := NewUUIDGroup(val)
if err != nil {
panic(err)
}
return ret
}
// NewBytesGroup creates a New Group of type bytes
// The supplied interface parameter could be
// a byte slice, a pointer to a byte slice or a string
func NewBytesGroup(val any) (*Group, error) {
ret, err := NewBytes(val)
if err != nil {
return nil, err
}
return &Group{ret}, nil
}
// IGroupFactory defines the signature for the factory functions that may be
// registered using RegisterGroupType to provide a new implementation of the
// corresponding type choice. The factory function should create a new *Group
// 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 IGroupFactory func(any) (*Group, error)
var groupValueRegister = map[string]IGroupFactory{
UUIDType: NewUUIDGroup,
BytesType: NewBytesGroup,
}
// RegisterGroupType registers a new IGroupValue implementation
// (created by the provided IGroupFactory) under the specified type name
// and CBOR tag.
func RegisterGroupType(tag uint64, factory IGroupFactory) error {
nilVal, err := factory(nil)
if err != nil {
return err
}
typ := nilVal.Value.Type()
if _, exists := groupValueRegister[typ]; exists {
return fmt.Errorf("Group type with name %q already exists", typ)
}
if err := registerCOMIDTag(tag, nilVal.Value); err != nil {
return err
}
groupValueRegister[typ] = factory
return nil
}