-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcondition.go
71 lines (62 loc) · 1.37 KB
/
condition.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
package iamgo
import (
"encoding/json"
"github.com/liamg/jfather"
)
type Conditions struct {
inner []Condition
r Range
}
type Condition struct {
operator String
key String
value Strings
}
func (c *Conditions) UnmarshalJSONWithMetadata(node jfather.Node) error {
var data map[string]map[string]Strings
if err := node.Decode(&data); err != nil {
return err
}
c.r = Range{
StartLine: node.Range().Start.Line,
EndLine: node.Range().End.Line,
}
for operator, comparison := range data {
for key, value := range comparison {
value.r = c.r
c.inner = append(c.inner, Condition{
operator: String{
inner: operator,
r: c.r,
},
key: String{
inner: key,
r: c.r,
},
value: value,
})
}
}
return nil
}
func (c Conditions) MarshalJSON() ([]byte, error) {
data := make(map[string]map[string]Strings)
for _, condition := range c.inner {
existing, ok := data[condition.operator.inner]
if !ok {
existing = make(map[string]Strings)
}
existing[condition.key.inner] = condition.value
data[condition.operator.inner] = existing
}
return json.Marshal(data)
}
func (c *Condition) Operator() (string, Range) {
return c.operator.inner, c.operator.r
}
func (c *Condition) Key() (string, Range) {
return c.key.inner, c.key.r
}
func (c *Condition) Value() ([]string, Range) {
return c.value.inner, c.value.r
}