-
Notifications
You must be signed in to change notification settings - Fork 3
/
rel.go
138 lines (122 loc) · 3.3 KB
/
rel.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
// Copyright 2020 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package swid
import (
"encoding/xml"
)
// Rel models the rel type.
type Rel struct {
val interface{}
}
/*
$rel /= ancestor
$rel /= component
$rel /= feature
$rel /= installationmedia
$rel /= packageinstaller
$rel /= parent
$rel /= patches
$rel /= requires
$rel /= see-also
$rel /= supersedes
$rel /= supplemental
$rel /= int / text
ancestor=1
component=2
feature=3
installationmedia=4
packageinstaller=5
parent=6
patches=7
requires=8
see-also=9
supersedes=10
supplemental=11
*/
// Rel constants
const (
RelAncestor = int64(iota + 1)
RelComponent
RelFeature
RelInstallationMedia
RelPackageInstaller
RelParent
RelPatches
RelRequires
RelSeeAlso
RelSupersedes
RelSupplemental
RelUnknown = ^int64(0)
)
var (
relToString = map[int64]string{
RelAncestor: "ancestor",
RelComponent: "component",
RelFeature: "feature",
RelInstallationMedia: "installation media",
RelPackageInstaller: "package installer",
RelParent: "parent",
RelPatches: "patches",
RelRequires: "requires",
RelSeeAlso: "see also",
RelSupersedes: "supersedes",
RelSupplemental: "supplemental",
}
stringToRel = map[string]int64{
"ancestor": RelAncestor,
"component": RelComponent,
"feature": RelFeature,
"installation media": RelInstallationMedia,
"package installer": RelPackageInstaller,
"parent": RelParent,
"patches": RelPatches,
"requires": RelRequires,
"see also": RelSeeAlso,
"supersedes": RelSupersedes,
"supplemental": RelSupplemental,
}
)
// NewRel returns a Rel initialized with the supplied value v
func NewRel(v interface{}) *Rel {
if isStringOrCode(v, "rel") != nil {
return nil
}
return &Rel{v}
}
// String returns the value of the Rel receiver as a string
func (r Rel) String() string {
return codeStringer(r.val, relToString, "rel")
}
// Check returns nil if the Rel receiver is of type string or code-point (i.e.,
// uint)
func (r Rel) Check() error {
return isStringOrCode(r.val, "rel")
}
// MarshalCBOR encodes the Rel receiver as code-point if possible, otherwise as
// string
func (r Rel) MarshalCBOR() ([]byte, error) {
return codeToCBOR(r.val, stringToRel)
}
// UnmarshalCBOR decodes the supplied data into a Rel code-point if possible,
// otherwise as string
func (r *Rel) UnmarshalCBOR(data []byte) error {
return cborToCode(data, stringToRel, &r.val)
}
// MarshalJSON encodes the Rel receiver as string
func (r Rel) MarshalJSON() ([]byte, error) {
return codeToJSON(r.val, relToString)
}
// UnmarshalJSON decodes the supplied data into a Rel code-point if possible,
// otherwise as string
func (r *Rel) UnmarshalJSON(data []byte) error {
return jsonToCode(data, stringToRel, &r.val)
}
// MarshalXMLAttr encodes the Rel receiver as XML attribute
func (r Rel) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return codeToXMLAttr(name, r.val, relToString)
}
// UnmarshalXMLAttr decodes the supplied XML attribute into a Rel code-point if
// possible, otherwise as string
func (r *Rel) UnmarshalXMLAttr(attr xml.Attr) error {
return xmlAttrToCode(attr, stringToRel, &r.val)
}