-
Notifications
You must be signed in to change notification settings - Fork 3
/
ownership.go
88 lines (73 loc) · 2.28 KB
/
ownership.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"
// Ownership models the ownership type
type Ownership struct {
val interface{}
}
/*
$ownership /= shared
$ownership /= private
$ownership /= abandon
$ownership /= int / text
abandon=1
private=2
shared=3
*/
// Ownership constants
const (
OwnershipAbandon = int64(iota + 1)
OwnershipPrivate
OwnershipShared
OwnershipUnknown = ^int64(0)
)
var (
ownershipToString = map[int64]string{
OwnershipShared: "shared",
OwnershipPrivate: "private",
OwnershipAbandon: "abandon",
}
stringToOwnership = map[string]int64{
"shared": OwnershipShared,
"private": OwnershipPrivate,
"abandon": OwnershipAbandon,
}
)
// String returns the value of the Ownership receiver as a string
func (o Ownership) String() string {
return codeStringer(o.val, ownershipToString, "ownership")
}
// Check returns nil if the Ownership receiver is of type string or code-point
// (i.e., uint)
func (o Ownership) Check() error {
return isStringOrCode(o.val, "ownership")
}
// MarshalCBOR encodes the Ownership receiver as code-point if possible,
// otherwise as string
func (o Ownership) MarshalCBOR() ([]byte, error) {
return codeToCBOR(o.val, stringToOwnership)
}
// UnmarshalCBOR decodes the supplied data into an Ownership code-point if
// possible, otherwise as string
func (o *Ownership) UnmarshalCBOR(data []byte) error {
return cborToCode(data, stringToOwnership, &o.val)
}
// MarshalJSON encodes the Ownership receiver as string
func (o Ownership) MarshalJSON() ([]byte, error) {
return codeToJSON(o.val, ownershipToString)
}
// UnmarshalJSON decodes the supplied data into an Ownership code-point if
// possible, otherwise as string
func (o *Ownership) UnmarshalJSON(data []byte) error {
return jsonToCode(data, stringToOwnership, &o.val)
}
// MarshalXMLAttr encodes the Ownership receiver as XML attribute
func (o Ownership) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return codeToXMLAttr(name, o.val, ownershipToString)
}
// UnmarshalXMLAttr decodes the supplied XML attribute into an Ownership
// code-point if possible, otherwise as string
func (o *Ownership) UnmarshalXMLAttr(attr xml.Attr) error {
return xmlAttrToCode(attr, stringToOwnership, &o.val)
}