-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
map.go
executable file
·92 lines (80 loc) · 1.7 KB
/
map.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
package types
import (
"encoding/json"
)
type MapValue struct {
BaseAttribute
value map[string]string
}
func (b MapValue) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"value": b.value,
"metadata": b.metadata,
})
}
func (b *MapValue) UnmarshalJSON(data []byte) error {
var keys map[string]interface{}
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
if keys["value"] != nil {
var target map[string]string
raw, err := json.Marshal(keys["value"])
if err != nil {
return err
}
if err := json.Unmarshal(raw, &target); err != nil {
return err
}
b.value = target
}
if keys["metadata"] != nil {
raw, err := json.Marshal(keys["metadata"])
if err != nil {
return err
}
var m Metadata
if err := json.Unmarshal(raw, &m); err != nil {
return err
}
b.metadata = m
}
return nil
}
func Map(value map[string]string, m Metadata) MapValue {
return MapValue{
value: value,
BaseAttribute: BaseAttribute{metadata: m},
}
}
func MapDefault(value map[string]string, m Metadata) MapValue {
b := Map(value, m)
b.BaseAttribute.metadata.isDefault = true
return b
}
func MapExplicit(value map[string]string, m Metadata) MapValue {
b := Map(value, m)
b.BaseAttribute.metadata.isExplicit = true
return b
}
func (b MapValue) Value() map[string]string {
return b.value
}
func (b MapValue) GetRawValue() interface{} {
return b.value
}
func (b MapValue) Len() int {
return len(b.value)
}
func (b MapValue) HasKey(key string) bool {
if b.value == nil {
return false
}
_, ok := b.value[key]
return ok
}
func (s MapValue) ToRego() interface{} {
m := s.metadata.ToRego().(map[string]interface{})
m["value"] = s.Value()
return m
}