-
Notifications
You must be signed in to change notification settings - Fork 7
/
role.go
150 lines (116 loc) · 2.5 KB
/
role.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
)
type Role int64
/*
$comid-role-type-choice /= comid.tag-creator
$comid-role-type-choice /= comid.creator
$comid-role-type-choice /= comid.maintainer
comid.tag-creator = 0
comid.creator = 1
comid.maintainer = 2
*/
const (
RoleTagCreator Role = iota
RoleCreator
RoleMaintainer
)
var (
roleToString = map[Role]string{
RoleTagCreator: "tagCreator",
RoleCreator: "creator",
RoleMaintainer: "maintainer",
}
stringToRole = map[string]Role{
"tagCreator": RoleTagCreator,
"creator": RoleCreator,
"maintainer": RoleMaintainer,
}
)
// String returns the string representation of the Role.
func (o Role) String() string {
text, ok := roleToString[o]
if ok {
return text
}
return fmt.Sprintf("Role(%d)", o)
}
// RegisterRole creates a new Role association between the provided value and
// name. An error is returned if either clashes with any of the existing roles.
func RegisterRole(val int64, name string) error {
role := Role(val)
if _, ok := roleToString[role]; ok {
return fmt.Errorf("role with value %d already exists", val)
}
if _, ok := stringToRole[name]; ok {
return fmt.Errorf("role with name %q already exists", name)
}
roleToString[role] = name
stringToRole[name] = role
return nil
}
type Roles []Role
func NewRoles() *Roles {
return new(Roles)
}
func (o *Roles) Add(roles ...Role) *Roles {
if o != nil {
*o = append(*o, roles...)
}
return o
}
func (o Roles) Valid() error {
if len(o) == 0 {
return fmt.Errorf("empty roles")
}
return nil
}
func (o Roles) ToCBOR() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
data, err := em.Marshal(o)
if err != nil {
return nil, err
}
return data, nil
}
func (o *Roles) FromCBOR(data []byte) error {
err := dm.Unmarshal(data, o)
if err != nil {
return err
}
return o.Valid()
}
func (o *Roles) UnmarshalJSON(data []byte) error {
var a []string
if err := json.Unmarshal(data, &a); err != nil {
return err
}
if len(a) == 0 {
return fmt.Errorf("no roles found")
}
for _, s := range a {
r, ok := stringToRole[s]
if !ok {
return fmt.Errorf("unknown role %q", s)
}
o = o.Add(r)
}
return nil
}
func (o Roles) MarshalJSON() ([]byte, error) {
roles := []string{}
for _, r := range o {
s, ok := roleToString[r]
if !ok {
return nil, fmt.Errorf("unknown role %d", r)
}
roles = append(roles, s)
}
return json.Marshal(roles)
}