-
Notifications
You must be signed in to change notification settings - Fork 5
/
value_source.go
84 lines (65 loc) · 1.91 KB
/
value_source.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
package restrict
import (
"bytes"
"encoding/json"
"gopkg.in/yaml.v3"
)
// ValueSource - enum type for source of value for given ValueDescriptor.
type ValueSource int
const (
// NoopValueSource - zero value for ValueSource, useful when marshaling/unmarshaling.
noopValueSource ValueSource = iota
// SubjectField - value that comes from Subject's field.
SubjectField
// ResourceField - value that comes from Resource's field.
ResourceField
// ContextField - value that comes from Context's field.
ContextField
// Explicit - value set explicitly in PolicyDefinition.
Explicit
)
var byValue = map[ValueSource]string{
SubjectField: "SubjectField",
ResourceField: "ResourceField",
ContextField: "ContextField",
Explicit: "Explicit",
}
var byName = map[string]ValueSource{
"SubjectField": SubjectField,
"ResourceField": ResourceField,
"ContextField": ContextField,
"Explicit": Explicit,
}
// String - Stringer implementation.
func (vs ValueSource) String() string {
return byValue[vs]
}
// MarshalJSON - marshals a ValueSource enum into its name as string.
func (vs ValueSource) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(byValue[vs])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
// MarshalYAML - marshals a ValueSource enum into its name as string.
func (vs ValueSource) MarshalYAML() (interface{}, error) {
return byValue[vs], nil
}
// UnmarshalJSON - unmarshals a string into ValueSource.
func (vs *ValueSource) UnmarshalJSON(jsonData []byte) error {
var sourceName string
if err := json.Unmarshal(jsonData, &sourceName); err != nil {
return err
}
*vs = byName[sourceName]
return nil
}
// UnmarshalYAML - unmarshals a string into ValueSource.
func (vs *ValueSource) UnmarshalYAML(value *yaml.Node) error {
var sourceName string
if err := value.Decode(&sourceName); err != nil {
return err
}
*vs = byName[sourceName]
return nil
}