-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.go
More file actions
322 lines (289 loc) · 8.32 KB
/
Copy pathcondition.go
File metadata and controls
322 lines (289 loc) · 8.32 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package dyfields
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
// Condition is a deliberately closed set of predicates: enough for real forms,
// but not an expression language. Anything more complex belongs in the caller's
// code, not in the schema.
//
// A condition is either a leaf (one operator against Field) or a combinator
// (all_of / any_of / not).
type Condition struct {
Field string
Equals any
hasEquals bool
In []any
NotIn []any
IsTrue *bool
IsEmpty *bool
EqualsField string
NotEqualsField string
GtField string
GteField string
LtField string
LteField string
AllOf []*Condition
AnyOf []*Condition
Not *Condition
}
var conditionKeys = map[string]bool{
"field": true, "equals": true, "in": true, "not_in": true,
"is_true": true, "is_empty": true,
"equals_field": true, "not_equals_field": true,
"gt_field": true, "gte_field": true, "lt_field": true, "lte_field": true,
"all_of": true, "any_of": true, "not": true,
}
// UnmarshalJSON rejects unknown keys: a typo in a condition must not silently
// become a condition that is always true.
func (c *Condition) UnmarshalJSON(b []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
keys := make([]string, 0, len(raw))
for k := range raw {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := raw[k]
if !conditionKeys[k] {
return fmt.Errorf("unknown condition key %q", k)
}
var err error
switch k {
case "field":
err = json.Unmarshal(v, &c.Field)
case "equals":
err = json.Unmarshal(v, &c.Equals)
c.hasEquals = err == nil
case "in":
err = json.Unmarshal(v, &c.In)
case "not_in":
err = json.Unmarshal(v, &c.NotIn)
case "is_true":
err = json.Unmarshal(v, &c.IsTrue)
case "is_empty":
err = json.Unmarshal(v, &c.IsEmpty)
case "equals_field":
err = json.Unmarshal(v, &c.EqualsField)
case "not_equals_field":
err = json.Unmarshal(v, &c.NotEqualsField)
case "gt_field":
err = json.Unmarshal(v, &c.GtField)
case "gte_field":
err = json.Unmarshal(v, &c.GteField)
case "lt_field":
err = json.Unmarshal(v, &c.LtField)
case "lte_field":
err = json.Unmarshal(v, &c.LteField)
case "all_of":
err = json.Unmarshal(v, &c.AllOf)
case "any_of":
err = json.Unmarshal(v, &c.AnyOf)
case "not":
err = json.Unmarshal(v, &c.Not)
}
if err != nil {
return fmt.Errorf("condition key %q: %w", k, err)
}
}
return nil
}
// MarshalJSON writes only the operators that are set, so that a parsed
// condition marshals back to the same shape.
func (c Condition) MarshalJSON() ([]byte, error) {
m := map[string]any{}
if c.Field != "" {
m["field"] = c.Field
}
if c.hasEquals {
m["equals"] = c.Equals
}
if c.In != nil {
m["in"] = c.In
}
if c.NotIn != nil {
m["not_in"] = c.NotIn
}
if c.IsTrue != nil {
m["is_true"] = *c.IsTrue
}
if c.IsEmpty != nil {
m["is_empty"] = *c.IsEmpty
}
for k, v := range map[string]string{
"equals_field": c.EqualsField,
"not_equals_field": c.NotEqualsField,
"gt_field": c.GtField,
"gte_field": c.GteField,
"lt_field": c.LtField,
"lte_field": c.LteField,
} {
if v != "" {
m[k] = v
}
}
if c.AllOf != nil {
m["all_of"] = c.AllOf
}
if c.AnyOf != nil {
m["any_of"] = c.AnyOf
}
if c.Not != nil {
m["not"] = c.Not
}
return json.Marshal(m)
}
// ---- construction helpers -------------------------------------------------
// Equals builds `field == value`.
func Equals(field string, value any) *Condition {
return &Condition{Field: field, Equals: value, hasEquals: true}
}
// In builds `field ∈ values`.
func In(field string, values ...any) *Condition {
return &Condition{Field: field, In: values}
}
// NotIn builds `field ∉ values`.
func NotIn(field string, values ...any) *Condition {
return &Condition{Field: field, NotIn: values}
}
// IsTrue builds `field is true`.
func IsTrue(field string) *Condition { t := true; return &Condition{Field: field, IsTrue: &t} }
// IsFalse builds `field is false`.
func IsFalse(field string) *Condition { f := false; return &Condition{Field: field, IsTrue: &f} }
// IsEmpty builds `field has no value`.
func IsEmpty(field string) *Condition { t := true; return &Condition{Field: field, IsEmpty: &t} }
// IsNotEmpty builds `field has a value`.
func IsNotEmpty(field string) *Condition { f := false; return &Condition{Field: field, IsEmpty: &f} }
// EqualsField builds `field == other`.
func EqualsField(field, other string) *Condition {
return &Condition{Field: field, EqualsField: other}
}
// NotEqualsField builds `field != other`.
func NotEqualsField(field, other string) *Condition {
return &Condition{Field: field, NotEqualsField: other}
}
// GtField builds `field > other`.
func GtField(field, other string) *Condition { return &Condition{Field: field, GtField: other} }
// GteField builds `field >= other`.
func GteField(field, other string) *Condition { return &Condition{Field: field, GteField: other} }
// LtField builds `field < other`.
func LtField(field, other string) *Condition { return &Condition{Field: field, LtField: other} }
// LteField builds `field <= other`.
func LteField(field, other string) *Condition { return &Condition{Field: field, LteField: other} }
// AllOf builds a conjunction.
func AllOf(cs ...*Condition) *Condition { return &Condition{AllOf: cs} }
// AnyOf builds a disjunction.
func AnyOf(cs ...*Condition) *Condition { return &Condition{AnyOf: cs} }
// Not builds a negation.
func Not(c *Condition) *Condition { return &Condition{Not: c} }
// ---- reference plumbing ---------------------------------------------------
// ref is a parsed field reference: how many scopes to walk out, and the name.
type ref struct {
up int // number of '^' hops
root bool // "$." prefix
name string // leaf field name
raw string
}
func parseRef(s string) ref {
r := ref{raw: s}
switch {
case strings.HasPrefix(s, scopeRoot):
r.root = true
r.name = s[len(scopeRoot):]
default:
i := 0
for i < len(s) && s[i] == scopeParent {
i++
}
if i > 0 && i < len(s) && s[i] == '.' {
r.up = i
r.name = s[i+1:]
} else {
r.name = s
}
}
return r
}
// refs returns every field reference the condition makes, in a stable order.
func (c *Condition) refs() []ref {
if c == nil {
return nil
}
var out []ref
if c.Field != "" {
out = append(out, parseRef(c.Field))
}
for _, s := range []string{
c.EqualsField, c.NotEqualsField, c.GtField, c.GteField, c.LtField, c.LteField,
} {
if s != "" {
out = append(out, parseRef(s))
}
}
for _, sub := range c.AllOf {
out = append(out, sub.refs()...)
}
for _, sub := range c.AnyOf {
out = append(out, sub.refs()...)
}
out = append(out, c.Not.refs()...)
return out
}
// ordRefs returns the reference pairs that need an order comparison, so Compile
// can reject comparing types that have no order.
func (c *Condition) ordRefs() [][2]ref {
if c == nil {
return nil
}
var out [][2]ref
if c.Field != "" {
for _, other := range []string{c.GtField, c.GteField, c.LtField, c.LteField} {
if other != "" {
out = append(out, [2]ref{parseRef(c.Field), parseRef(other)})
}
}
}
for _, sub := range c.AllOf {
out = append(out, sub.ordRefs()...)
}
for _, sub := range c.AnyOf {
out = append(out, sub.ordRefs()...)
}
out = append(out, c.Not.ordRefs()...)
return out
}
// sameScopeRefs returns only the references that resolve in the current scope.
// Cross-scope references always point outwards, so they cannot form a cycle and
// are excluded from the topological check.
func (c *Condition) sameScopeRefs() []string {
var out []string
for _, r := range c.refs() {
if r.up == 0 && !r.root {
out = append(out, r.name)
}
}
return out
}
// isLeaf reports whether the condition uses a leaf operator.
func (c *Condition) isLeaf() bool {
return c.AllOf == nil && c.AnyOf == nil && c.Not == nil
}
// hasOp reports whether any operator at all is set, so Compile can reject an
// empty condition instead of silently treating it as true.
func (c *Condition) hasOp() bool {
if c == nil {
return false
}
if c.AllOf != nil || c.AnyOf != nil || c.Not != nil {
return true
}
return c.hasEquals || c.In != nil || c.NotIn != nil ||
c.IsTrue != nil || c.IsEmpty != nil ||
c.EqualsField != "" || c.NotEqualsField != "" ||
c.GtField != "" || c.GteField != "" || c.LtField != "" || c.LteField != ""
}