-
Notifications
You must be signed in to change notification settings - Fork 3
/
use.go
88 lines (73 loc) · 2.07 KB
/
use.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
// Copyright 2020 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package swid
import "encoding/xml"
// Use models the use type.
type Use struct {
val interface{}
}
/*
$use /= optional
$use /= required
$use /= recommended
$use /= int / text
optional=1
required=2
recommended=3
*/
// Use constants
const (
UseOptional = int64(iota + 1)
UseRequired
UseRecommended
UseUnknown = ^int64(0)
)
var (
useToString = map[int64]string{
UseOptional: "optional",
UseRequired: "required",
UseRecommended: "recommended",
}
stringToUse = map[string]int64{
"optional": UseOptional,
"required": UseRequired,
"recommended": UseRecommended,
}
)
// String returns the value of the Use receiver as a string
func (u Use) String() string {
return codeStringer(u.val, useToString, "use")
}
// Check returns nil if the Use receiver is of type string or code-point
// (i.e., uint)
func (u Use) Check() error {
return isStringOrCode(u.val, "use")
}
// MarshalCBOR encodes the Use receiver as code-point if possible,
// otherwise as string
func (u Use) MarshalCBOR() ([]byte, error) {
return codeToCBOR(u.val, stringToUse)
}
// UnmarshalCBOR decodes the supplied data into a Use code-point if
// possible, otherwise as string
func (u *Use) UnmarshalCBOR(data []byte) error {
return cborToCode(data, stringToUse, &u.val)
}
// MarshalJSON encodes the Use receiver as string
func (u Use) MarshalJSON() ([]byte, error) {
return codeToJSON(u.val, useToString)
}
// UnmarshalJSON decodes the supplied data into a Use code-point if
// possible, otherwise as string
func (u *Use) UnmarshalJSON(data []byte) error {
return jsonToCode(data, stringToUse, &u.val)
}
// MarshalXMLAttr encodes the Use receiver as XML attribute
func (u Use) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return codeToXMLAttr(name, u.val, useToString)
}
// UnmarshalXMLAttr decodes the supplied XML attribute into a Use
// code-point if possible, otherwise as string
func (u *Use) UnmarshalXMLAttr(attr xml.Attr) error {
return xmlAttrToCode(attr, stringToUse, &u.val)
}