-
Notifications
You must be signed in to change notification settings - Fork 2
/
clause.go
172 lines (157 loc) · 3.79 KB
/
clause.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package jsonlogic
import (
"encoding/json"
"fmt"
)
// Operator represents a jsonlogic Operator.
type Operator struct {
Name string
}
// Argument represents any valid argument to a jsonlogic
// operator.
type Argument struct {
Clause *Clause
Value interface{}
}
// MarshalJSON implements json.Marshaler. It enforces
// rending of clause arguments as an array (even if there was
// just one non array argument in the original clause when
// unmarshaled).
func (a Argument) MarshalJSON() ([]byte, error) {
switch {
case a.Clause != nil && a.Value != nil:
return nil, fmt.Errorf("an argument should only have a clause, or a value, not both")
case a.Clause != nil:
return json.Marshal(a.Clause)
default:
return json.Marshal(a.Value)
}
}
// UnmarshalJSON implements json.Unmarshaler.
func (a *Argument) UnmarshalJSON(bs []byte) error {
c := Clause{}
clauseErr := json.Unmarshal(bs, &c)
if clauseErr == nil {
*a = Argument{
Clause: &c,
Value: nil,
}
return nil
}
var v interface{}
vErr := json.Unmarshal(bs, &v)
if vErr == nil {
*a = Argument{
Value: v,
}
return nil
}
return fmt.Errorf("could not parse argument, %w", vErr)
}
// Arguments represents the list of arguments to a jsonlogic
// Clause.
type Arguments []Argument
// UnmarshalJSON implements json.Unmarshaler.
func (args *Arguments) UnmarshalJSON(bs []byte) error {
slice := []Argument{}
sliceErr := json.Unmarshal(bs, &slice)
if sliceErr == nil {
*args = slice
return nil
}
arg := Argument{}
if oneErr := json.Unmarshal(bs, &arg); oneErr == nil {
*args = []Argument{arg}
return nil
}
return fmt.Errorf("could not parse arguments")
}
// sliceHasPossibleClause recursivelychecks slices to see if they contain a dictionary
// that could be a clause. This lets us treat slices as clauses only in the cases
// where we have to
func sliceHasPossibleClause(ifs []interface{}) bool {
for i := range ifs {
switch v := ifs[i].(type) {
case []interface{}:
if sliceHasPossibleClause(v) {
return true
}
case map[string]interface{}:
if len(v) == 1 {
return true
}
default:
}
}
return false
}
// Clause represents a JsonLogic clause.
type Clause struct {
Operator Operator
Arguments Arguments
}
// UnmarshalJSON parses JSON data as a JsonLogic
// Clause.
func (c *Clause) UnmarshalJSON(bs []byte) error {
clause := map[string]Arguments{}
err := json.Unmarshal(bs, &clause)
if err == nil && len(clause) == 1 {
for k, v := range clause {
*c = Clause{
Operator: Operator{
Name: k,
},
Arguments: v,
}
return nil
}
}
var raw interface{}
err = json.Unmarshal(bs, &raw)
if err != nil {
return err
}
if rawslice, ok := raw.([]interface{}); ok {
// this is a bit subtle, we want to differentiate instances of the empty
// slice, this forces a new slice header.
if len(rawslice) == 0 {
raw = make([]interface{}, 0, 1)
}
if sliceHasPossibleClause(rawslice) {
// We'll check again to see if this could be a slice being
// used as a naked array clause which might require evaluation.
argsArr := []Argument{}
err = json.Unmarshal(bs, &argsArr)
if err == nil && len(argsArr) != 0 {
*c = Clause{
Arguments: argsArr,
}
return nil
}
}
}
*c = Clause{
Arguments: []Argument{{
Value: raw,
}},
}
return nil
}
// MarshalJSON implements json.Marshaler. It enforces
// rending of clause arguments as an array (even if there was
// just one non array argument in the original clause when
// unmarshaled).
func (c Clause) MarshalJSON() ([]byte, error) {
switch c.Operator.Name {
case "":
if c.Arguments[0].Clause == nil {
return json.Marshal(c.Arguments[0].Value)
}
// This was an array used as a clause
return json.Marshal(c.Arguments)
default:
return json.Marshal(map[string]Arguments{
c.Operator.Name: c.Arguments,
})
}
}