/
object.go
500 lines (427 loc) · 12.6 KB
/
object.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package jsval
import (
"errors"
"reflect"
"regexp"
"github.com/lestrrat-go/pdebug"
)
type getPropValuer interface {
// Given a JSON property name, return the actual field value
GetPropValue(string) (interface{}, error)
}
type getPropNameser interface {
// Given a lst of JSON property names
GetPropNames() ([]string, error)
}
// Object creates a new ObjectConstraint
func Object() *ObjectConstraint {
return &ObjectConstraint{
additionalProperties: nil,
minProperties: -1,
maxProperties: -1,
patternProperties: make(map[*regexp.Regexp]Constraint),
properties: make(map[string]Constraint),
propdeps: make(map[string][]string),
required: make(map[string]struct{}),
schemadeps: make(map[string]Constraint),
}
}
// Required specifies required property names
func (o *ObjectConstraint) Required(l ...string) *ObjectConstraint {
o.reqlock.Lock()
defer o.reqlock.Unlock()
for _, pname := range l {
o.required[pname] = struct{}{}
}
return o
}
// IsPropRequired returns true if the given name is listed under
// the required properties
func (o *ObjectConstraint) IsPropRequired(s string) bool {
o.reqlock.Lock()
defer o.reqlock.Unlock()
_, ok := o.required[s]
return ok
}
// MinProperties specifies the minimum number of properties this
// constraint can allow. If unspecified, it is not checked.
func (o *ObjectConstraint) MinProperties(n int) *ObjectConstraint {
o.minProperties = n
return o
}
// MaxProperties specifies the maximum number of properties this
// constraint can allow. If unspecified, it is not checked.
func (o *ObjectConstraint) MaxProperties(n int) *ObjectConstraint {
o.maxProperties = n
return o
}
// AdditionalProperties specifies the constraint that additional
// properties should be validated against.
func (o *ObjectConstraint) AdditionalProperties(c Constraint) *ObjectConstraint {
o.additionalProperties = c
return o
}
// AddProp adds constraints for a named property.
func (o *ObjectConstraint) AddProp(name string, c Constraint) *ObjectConstraint {
o.proplock.Lock()
defer o.proplock.Unlock()
o.properties[name] = c
return o
}
// PatternProperties specifies constraints that properties matching
// this pattern must be validated against. Note that properties listed
// using `AddProp` takes precedence.
func (o *ObjectConstraint) PatternProperties(key *regexp.Regexp, c Constraint) *ObjectConstraint {
o.proplock.Lock()
defer o.proplock.Unlock()
o.patternProperties[key] = c
return o
}
// PatternPropertiesString is the same as PatternProperties, but takes
// a string representing a regular expression. If the regular expression
// cannot be compiled, a panic occurs.
func (o *ObjectConstraint) PatternPropertiesString(key string, c Constraint) *ObjectConstraint {
rx := regexp.MustCompile(key)
return o.PatternProperties(rx, c)
}
// PropDependency specifies properties that must be present when
// `from` is present.
func (o *ObjectConstraint) PropDependency(from string, to ...string) *ObjectConstraint {
o.deplock.Lock()
defer o.deplock.Unlock()
l := o.propdeps[from]
l = append(l, to...)
o.propdeps[from] = l
return o
}
// SchemaDependency specifies a schema that the value being validated
// must also satisfy. Note that the "object" is the target that needs to
// be additionally validated, not the value of the `from` property
func (o *ObjectConstraint) SchemaDependency(from string, c Constraint) *ObjectConstraint {
o.deplock.Lock()
defer o.deplock.Unlock()
o.schemadeps[from] = c
return o
}
// GetPropDependencies returns the list of property names that must
// be present for given property name `from`
func (o *ObjectConstraint) GetPropDependencies(from string) []string {
o.deplock.Lock()
defer o.deplock.Unlock()
l, ok := o.propdeps[from]
if !ok {
return nil
}
return l
}
// GetSchemaDependency returns the Constraint that must be used when
// the property `from` is present.
func (o *ObjectConstraint) GetSchemaDependency(from string) Constraint {
o.deplock.Lock()
defer o.deplock.Unlock()
c, ok := o.schemadeps[from]
if !ok {
return nil
}
return c
}
var structInfoRegistry = StructInfoRegistry{
registry: make(map[reflect.Type]StructInfo),
}
// getProps return all of the property names for this object.
// XXX Map keys can be something other than strings, but
// we can't really allow it?
func (o *ObjectConstraint) getPropNames(rv reflect.Value) ([]string, error) {
switch rv.Kind() {
case reflect.Ptr, reflect.Interface:
rv = rv.Elem()
}
var keys []string
switch rv.Kind() {
case reflect.Map:
vk := rv.MapKeys()
keys = make([]string, len(vk))
for i, v := range vk {
if v.Kind() != reflect.String {
return nil, errors.New("panic: can only handle maps with string keys")
}
keys[i] = v.String()
}
case reflect.Struct:
if gpv, ok := rv.Interface().(getPropNameser); ok {
pv, err := gpv.GetPropNames()
if err == nil {
return pv, nil
}
}
si, ok := structInfoRegistry.Lookup(rv.Type())
if !ok {
si = structInfoRegistry.Register(rv.Type())
}
return si.PropNames(rv), nil
default:
return nil, errors.New("cannot get property names from this value (Kind: " + rv.Kind().String() + ")")
}
return keys, nil
}
var (
maybefloatT = reflect.TypeOf(MaybeFloat{})
maybeintT = reflect.TypeOf(MaybeInt{})
intT = reflect.TypeOf(int64(0))
floatT = reflect.TypeOf(float64(0))
)
func coerceValue(v interface{}, t reflect.Type) reflect.Value {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Interface:
vv = vv.Elem()
}
// For known Maybe types, we should do our best, too
switch {
case t == maybefloatT:
t = floatT
case t == maybeintT:
t = intT
}
if vv.Type().ConvertibleTo(t) {
return vv.Convert(t)
}
return vv
}
type setPropValuer interface {
SetPropValue(string, interface{}) error
}
var spvType = reflect.TypeOf((*setPropValuer)(nil)).Elem()
func (o *ObjectConstraint) setProp(rv reflect.Value, pname string, val interface{}) error {
switch rv.Kind() {
case reflect.Ptr, reflect.Interface:
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Map:
rv.SetMapIndex(reflect.ValueOf(pname), reflect.ValueOf(val))
return nil
case reflect.Struct:
spvm := rv.MethodByName("SetPropValue")
if spvm != zeroval {
out := spvm.Call([]reflect.Value{reflect.ValueOf(pname), reflect.ValueOf(val)})
if !out[0].IsNil() {
return out[0].Interface().(error)
}
return nil
}
f := o.getProp(rv, pname)
if f == zeroval {
return errors.New("setProp: could not find field '" + pname + "'")
}
// Usability: If you specify `Default(10)` on an int64 value,
// it doesn't work. But these values are compatible. We should
// do our best to align them
dv := coerceValue(val, f.Type())
// Is this a Maybe value? If so, we should use its Set() method
var mv reflect.Value
switch {
case f.Type().Implements(maybeif):
mv = f.MethodByName("Set")
case f.CanAddr() && f.Addr().Type().Implements(maybeif):
mv = f.Addr().MethodByName("Set")
default:
f.Set(dv)
return nil
}
if mv == zeroval {
return errors.New("setProp: could not get 'Set' method for value")
}
out := mv.Call([]reflect.Value{dv})
if !out[0].IsNil() {
return out[0].Interface().(error)
}
return nil
default:
return errors.New("setProp: don't know what to do with '" + rv.Kind().String() + "'")
}
}
func (o *ObjectConstraint) getProp(rv reflect.Value, pname string) reflect.Value {
switch rv.Kind() {
case reflect.Map:
pv := reflect.ValueOf(pname)
return rv.MapIndex(pv)
case reflect.Struct:
// This guy knows how to grab the value, given a name. Use that
if gpv, ok := rv.Interface().(getPropValuer); ok {
pv, err := gpv.GetPropValue(pname)
if err == nil {
return reflect.ValueOf(pv)
}
}
si, ok := structInfoRegistry.Lookup(rv.Type())
if !ok {
si = structInfoRegistry.Register(rv.Type())
}
fn, ok := si.FieldName(pname)
if !ok {
if pdebug.Enabled {
pdebug.Printf("Could not resolve name '%s'", pname)
}
return zeroval
}
return rv.FieldByName(fn)
default:
return zeroval
}
}
// Validate validates the given value against this ObjectConstraint
func (o *ObjectConstraint) Validate(v interface{}) (err error) {
if pdebug.Enabled {
g := pdebug.IPrintf("START ObjectConstraint.Validate")
defer func() {
if err == nil {
g.IRelease("END ObjectConstraint.Validate (PASS)")
} else {
g.IRelease("END ObjectConstraint.Validate (FAIL): %s", err)
}
}()
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Interface:
rv = rv.Elem()
}
fields, err := o.getPropNames(rv)
if err != nil {
return err
}
lf := len(fields)
if o.minProperties > -1 && lf < o.minProperties {
return errors.New("fewer properties than minProperties")
}
if o.maxProperties > -1 && lf > o.maxProperties {
return errors.New("more properties than maxProperties")
}
// Find the list of field names that were passed to us
// "premain" shows extra props, if any.
// "pseen" shows props that we have already seen
premain := map[string]struct{}{}
pseen := map[string]struct{}{}
for _, k := range fields {
premain[k] = struct{}{}
}
// Now, for all known constraints, validate the prop
// create a copy of properties so that we don't have to keep the lock
propdefs := make(map[string]Constraint)
o.proplock.Lock()
for pname, c := range o.properties {
propdefs[pname] = c
}
o.proplock.Unlock()
for pname, c := range propdefs {
if pdebug.Enabled {
pdebug.Printf("Validating property '%s'", pname)
}
pval := o.getProp(rv, pname)
propExists := false
switch {
case pval == zeroval:
// If we got a zeroval, we're done for.
case pval.Type().Implements(maybeif) || reflect.PtrTo(pval.Type()).Implements(maybeif):
// If we have a Maybe value, we check the Valid() flag
mv := pval.MethodByName("Valid")
out := mv.Call(nil)
if out[0].Bool() {
propExists = true
// Swap out pval to be the value pointed to by the Maybe value
mv = pval.MethodByName("Value")
out = mv.Call(nil)
pval = out[0]
}
default:
// Everything else, we have *something*
propExists = true
}
if !propExists {
if pdebug.Enabled {
pdebug.Printf("Property '%s' does not exist", pname)
}
if o.IsPropRequired(pname) { // required, and not present.
return errors.New("object property '" + pname + "' is required")
}
// At this point we know that the property was not present
// and that this field was indeed not required.
if c.HasDefault() {
if pdebug.Enabled {
pdebug.Printf("object property '" + pname + "' has default")
}
// We have default
dv := c.DefaultValue()
if err := o.setProp(rv, pname, dv); err != nil {
return errors.New("failed to set default value for property '" + pname + "': " + err.Error())
}
pval = reflect.ValueOf(dv)
}
continue
}
// delete from remaining props
delete(premain, pname)
// ...and add to props that we have seen
pseen[pname] = struct{}{}
if err := c.Validate(pval.Interface()); err != nil {
return errors.New("object property '" + pname + "' validation failed: " + err.Error())
}
}
for pat, c := range o.patternProperties {
if pdebug.Enabled {
pdebug.Printf("Checking patternProperty '%s'", pat.String())
}
for pname := range premain {
if !pat.MatchString(pname) {
if pdebug.Enabled {
pdebug.Printf("Property '%s' does not match pattern...", pname)
}
continue
}
if pdebug.Enabled {
pdebug.Printf("Property '%s' matches!", pname)
}
// No need to check if this pname exists, as we're taking
// this from "premain"
pval := o.getProp(rv, pname)
delete(premain, pname)
pseen[pname] = struct{}{}
if err := c.Validate(pval.Interface()); err != nil {
return errors.New("object property '" + pname + "' validation failed: " + err.Error())
}
}
}
if len(premain) > 0 {
c := o.additionalProperties
if c == nil {
return errors.New("additional properties are not allowed")
}
for pname := range premain {
pval := o.getProp(rv, pname)
if err := c.Validate(pval.Interface()); err != nil {
return errors.New("object property for '" + pname + "' validation failed: " + err.Error())
}
}
}
for pname := range pseen {
if deps := o.GetPropDependencies(pname); len(deps) > 0 {
if pdebug.Enabled {
pdebug.Printf("Property '%s' has dependencies", pname)
}
for _, dep := range deps {
if _, ok := pseen[dep]; !ok {
return errors.New("required dependency '" + dep + "' is mising")
}
}
// can't, and shouldn't do object validation after checking prop deps
continue
}
if depc := o.GetSchemaDependency(pname); depc != nil {
if err := depc.Validate(v); err != nil {
return err
}
}
}
return nil
}