Repository navigation
Expand file tree
/
Copy pathclass.go
More file actions
168 lines (142 loc) · 4.05 KB
/
Copy pathclass.go
File metadata and controls
168 lines (142 loc) · 4.05 KB
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
// Copyright 2021-2026 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
)
// Class represents the class of the (target / attesting) environment. The only
// required field is the class unique identifier (see ClassID). Optionally,
// information about the specific brand & product as well as its topological
// coordinates within the wider device can be recorded.
type Class struct {
ClassID *ClassID `cbor:"0,keyasint,omitempty" json:"id,omitempty"`
Vendor *string `cbor:"1,keyasint,omitempty" json:"vendor,omitempty"`
Model *string `cbor:"2,keyasint,omitempty" json:"model,omitempty"`
Layer *uint64 `cbor:"3,keyasint,omitempty" json:"layer,omitempty"`
Index *uint64 `cbor:"4,keyasint,omitempty" json:"index,omitempty"`
}
// NewClassUUID instantiates a new Class object with the specified UUID as
// identifier
func NewClassUUID(uuid UUID) *Class {
classID, err := NewUUIDClassID(uuid)
if err != nil {
return nil
}
return &Class{ClassID: classID}
}
// NewClassOID instantiates a new Class object that identifies the OID
func NewClassOID(oid string) *Class {
classID, err := NewOIDClassID(oid)
if err != nil {
return nil
}
return &Class{ClassID: classID}
}
// NewClassBytes instantiates a new Class object with tagged-bytes ID
func NewClassBytes(b any) *Class {
classID, err := NewBytesClassID(b)
if err != nil {
return nil
}
return &Class{ClassID: classID}
}
// SetVendor sets the vendor metadata to the supplied string
func (o *Class) SetVendor(vendor string) *Class {
if o != nil {
o.Vendor = &vendor
}
return o
}
// GetVendor returns the vendor string if it set in the target Class.
// Otherwise, an empty string is returned.
func (o Class) GetVendor() string {
if o.Vendor == nil {
return ""
}
return *o.Vendor
}
// GetModel returns the model string if it set in the target Class.
// Otherwise, an empty string is returned.
func (o Class) GetModel() string {
if o.Model == nil {
return ""
}
return *o.Model
}
// GetLayer returns the layer number if it set in the target Class.
// Otherwise, uint64_max is returned.
func (o Class) GetLayer() uint64 {
if o.Layer == nil {
return ^uint64(0)
}
return *o.Layer
}
// GetIndex returns the index number if it set in the target Class.
// Otherwise, uint64_max is returned.
func (o Class) GetIndex() uint64 {
if o.Layer == nil {
return ^uint64(0)
}
return *o.Index
}
// SetModel sets the model metadata to the supplied string
func (o *Class) SetModel(model string) *Class {
if o != nil {
o.Model = &model
}
return o
}
// SetLayer sets the "layer" (i.e., the logical/topological location of the
// environment in the device) as indicated
func (o *Class) SetLayer(layer uint64) *Class {
if o != nil {
o.Layer = &layer
}
return o
}
// SetIndex sets the "index" (i.e., the identifier of the environment instance
// in a specific layer) as indicated
func (o *Class) SetIndex(index uint64) *Class {
if o != nil {
o.Index = &index
}
return o
}
// Valid checks the non-empty<> constraint on the map
func (o Class) Valid() error {
// check non-empty<{ ... }>
if (o.ClassID == nil || !o.ClassID.IsSet()) &&
o.Vendor == nil && o.Model == nil && o.Layer == nil && o.Index == nil {
return fmt.Errorf("class must not be empty")
}
return nil
}
// ToCBOR serializes the target Class to CBOR (if the Class is "valid")
func (o Class) ToCBOR() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
return em.Marshal(&o)
}
// FromCBOR deserializes the supplied CBOR data into the target Class
func (o *Class) FromCBOR(data []byte) error {
if err := dm.Unmarshal(data, o); err != nil {
return err
}
return o.Valid()
}
// FromJSON deserializes the supplied JSON string into the target Class
func (o *Class) FromJSON(data []byte) error {
if err := json.Unmarshal(data, o); err != nil {
return err
}
return o.Valid()
}
// ToJSON serializes the target Class to JSON (if the Class is "valid")
func (o Class) ToJSON() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
return json.Marshal(&o)
}