forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
869 lines (785 loc) · 17.3 KB
/
types.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package types declares data types for Rego values and helper functions to
// operate on these types.
package types
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/open-policy-agent/opa/util"
)
// Sprint returns the string representation of the type.
func Sprint(x Type) string {
if x == nil {
return "???"
}
return x.String()
}
// Type represents a type of a term in the language.
type Type interface {
String() string
typeMarker() string
json.Marshaler
}
func (Null) typeMarker() string { return "null" }
func (Boolean) typeMarker() string { return "boolean" }
func (Number) typeMarker() string { return "number" }
func (String) typeMarker() string { return "string" }
func (*Array) typeMarker() string { return "array" }
func (*Object) typeMarker() string { return "object" }
func (*Set) typeMarker() string { return "set" }
func (Any) typeMarker() string { return "any" }
func (Function) typeMarker() string { return "function" }
// Null represents the null type.
type Null struct{}
// NewNull returns a new Null type.
func NewNull() Null {
return Null{}
}
// MarshalJSON returns the JSON encoding of t.
func (t Null) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": t.typeMarker(),
})
}
func (t Null) String() string {
return "null"
}
// Boolean represents the boolean type.
type Boolean struct{}
// B represents an instance of the boolean type.
var B = NewBoolean()
// NewBoolean returns a new Boolean type.
func NewBoolean() Boolean {
return Boolean{}
}
// MarshalJSON returns the JSON encoding of t.
func (t Boolean) MarshalJSON() ([]byte, error) {
repr := map[string]interface{}{
"type": t.typeMarker(),
}
return json.Marshal(repr)
}
func (t Boolean) String() string {
return t.typeMarker()
}
// String represents the string type.
type String struct{}
// S represents an instance of the string type.
var S = NewString()
// NewString returns a new String type.
func NewString() String {
return String{}
}
// MarshalJSON returns the JSON encoding of t.
func (t String) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": t.typeMarker(),
})
}
func (t String) String() string {
return "string"
}
// Number represents the number type.
type Number struct{}
// N represents an instance of the number type.
var N = NewNumber()
// NewNumber returns a new Number type.
func NewNumber() Number {
return Number{}
}
// MarshalJSON returns the JSON encoding of t.
func (t Number) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": t.typeMarker(),
})
}
func (Number) String() string {
return "number"
}
// Array represents the array type.
type Array struct {
static []Type // static items
dynamic Type // dynamic items
}
// NewArray returns a new Array type.
func NewArray(static []Type, dynamic Type) *Array {
return &Array{
static: static,
dynamic: dynamic,
}
}
// MarshalJSON returns the JSON encoding of t.
func (t *Array) MarshalJSON() ([]byte, error) {
repr := map[string]interface{}{
"type": t.typeMarker(),
}
if len(t.static) != 0 {
repr["static"] = t.static
}
if t.dynamic != nil {
repr["dynamic"] = t.dynamic
}
return json.Marshal(repr)
}
func (t *Array) String() string {
prefix := "array"
buf := []string{}
for _, tpe := range t.static {
buf = append(buf, Sprint(tpe))
}
var repr = prefix
if len(buf) > 0 {
repr += "<" + strings.Join(buf, ", ") + ">"
}
if t.dynamic != nil {
repr += "[" + t.dynamic.String() + "]"
}
return repr
}
// Dynamic returns the type of the array's dynamic elements.
func (t *Array) Dynamic() Type {
return t.dynamic
}
// Len returns the number of static array elements.
func (t *Array) Len() int {
return len(t.static)
}
// Select returns the type of element at the zero-based pos.
func (t *Array) Select(pos int) Type {
if pos >= 0 {
if len(t.static) > pos {
return t.static[pos]
}
if t.dynamic != nil {
return t.dynamic
}
}
return nil
}
// Set represents the set type.
type Set struct {
of Type
}
// NewSet returns a new Set type.
func NewSet(of Type) *Set {
return &Set{
of: of,
}
}
// MarshalJSON returns the JSON encoding of t.
func (t *Set) MarshalJSON() ([]byte, error) {
repr := map[string]interface{}{
"type": t.typeMarker(),
}
if t.of != nil {
repr["of"] = t.of
}
return json.Marshal(repr)
}
func (t *Set) String() string {
prefix := "set"
return prefix + "[" + Sprint(t.of) + "]"
}
// StaticProperty represents a static object property.
type StaticProperty struct {
Key interface{}
Value Type
}
// NewStaticProperty returns a new StaticProperty object.
func NewStaticProperty(key interface{}, value Type) *StaticProperty {
return &StaticProperty{
Key: key,
Value: value,
}
}
// MarshalJSON returns the JSON encoding of p.
func (p *StaticProperty) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"key": p.Key,
"value": p.Value,
})
}
// DynamicProperty represents a dynamic object property.
type DynamicProperty struct {
Key Type
Value Type
}
// NewDynamicProperty returns a new DynamicProperty object.
func NewDynamicProperty(key, value Type) *DynamicProperty {
return &DynamicProperty{
Key: key,
Value: value,
}
}
// MarshalJSON returns the JSON encoding of p.
func (p *DynamicProperty) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"key": p.Key,
"value": p.Value,
})
}
func (p *DynamicProperty) String() string {
return fmt.Sprintf("%s: %s", Sprint(p.Key), Sprint(p.Value))
}
// Object represents the object type.
type Object struct {
static []*StaticProperty // constant properties
dynamic *DynamicProperty // dynamic properties
}
// NewObject returns a new Object type.
func NewObject(static []*StaticProperty, dynamic *DynamicProperty) *Object {
sort.Slice(static, func(i, j int) bool {
cmp := util.Compare(static[i].Key, static[j].Key)
return cmp == -1
})
return &Object{
static: static,
dynamic: dynamic,
}
}
func (t *Object) String() string {
prefix := "object"
buf := make([]string, 0, len(t.static))
for _, p := range t.static {
buf = append(buf, fmt.Sprintf("%v: %v", p.Key, Sprint(p.Value)))
}
var repr = prefix
if len(buf) > 0 {
repr += "<" + strings.Join(buf, ", ") + ">"
}
if t.dynamic != nil {
repr += "[" + t.dynamic.String() + "]"
}
return repr
}
// DynamicValue returns the type of the object's dynamic elements.
func (t *Object) DynamicValue() Type {
if t.dynamic == nil {
return nil
}
return t.dynamic.Value
}
// Keys returns the keys of the object's static elements.
func (t *Object) Keys() []interface{} {
sl := make([]interface{}, 0, len(t.static))
for _, p := range t.static {
sl = append(sl, p.Key)
}
return sl
}
// MarshalJSON returns the JSON encoding of t.
func (t *Object) MarshalJSON() ([]byte, error) {
repr := map[string]interface{}{
"type": t.typeMarker(),
}
if len(t.static) != 0 {
repr["static"] = t.static
}
if t.dynamic != nil {
repr["dynamic"] = t.dynamic
}
return json.Marshal(repr)
}
// Select returns the type of the named property.
func (t *Object) Select(name interface{}) Type {
for _, p := range t.static {
if util.Compare(p.Key, name) == 0 {
return p.Value
}
}
if t.dynamic != nil {
if Contains(t.dynamic.Key, TypeOf(name)) {
return t.dynamic.Value
}
}
return nil
}
// Any represents a dynamic type.
type Any []Type
// A represents the superset of all types.
var A = NewAny()
// NewAny returns a new Any type.
func NewAny(of ...Type) Any {
sl := make(Any, len(of))
for i := range sl {
sl[i] = of[i]
}
return sl
}
// Contains returns true if t is a superset of other.
func (t Any) Contains(other Type) bool {
if _, ok := other.(*Function); ok {
return false
}
for i := range t {
if Compare(t[i], other) == 0 {
return true
}
}
return len(t) == 0
}
// MarshalJSON returns the JSON encoding of t.
func (t Any) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"type": t.typeMarker(),
"of": []Type(t),
})
}
// Merge return a new Any type that is the superset of t and other.
func (t Any) Merge(other Type) Any {
if otherAny, ok := other.(Any); ok {
return t.Union(otherAny)
}
if t.Contains(other) {
return t
}
return append(t, other)
}
// Union returns a new Any type that is the union of the two Any types.
func (t Any) Union(other Any) Any {
if len(t) == 0 {
return t
}
if len(other) == 0 {
return other
}
cpy := make(Any, len(t))
for i := range cpy {
cpy[i] = t[i]
}
for i := range other {
if !cpy.Contains(other[i]) {
cpy = append(cpy, other[i])
}
}
return cpy
}
func (t Any) String() string {
prefix := "any"
if len(t) == 0 {
return prefix
}
buf := make([]string, len(t))
for i := range t {
buf[i] = Sprint(t[i])
}
return prefix + "<" + strings.Join(buf, ", ") + ">"
}
// Function represents a function type.
type Function struct {
args []Type
result Type
}
// Args returns an argument list.
func Args(x ...Type) []Type {
return x
}
// NewFunction returns a new Function object where xs[:len(xs)-1] are arguments
// and xs[len(xs)-1] is the result type.
func NewFunction(args []Type, result Type) *Function {
return &Function{
args: args,
result: result,
}
}
// Args returns the function's argument types.
func (t *Function) Args() []Type {
return t.args
}
// Result returns the function's result type.
func (t *Function) Result() Type {
return t.result
}
func (t *Function) String() string {
var args string
if len(t.args) != 1 {
args = "("
}
buf := []string{}
for _, a := range t.Args() {
buf = append(buf, Sprint(a))
}
args += strings.Join(buf, ", ")
if len(t.args) != 1 {
args += ")"
}
return fmt.Sprintf("%v => %v", args, Sprint(t.Result()))
}
// MarshalJSON returns the JSON encoding of t.
func (t *Function) MarshalJSON() ([]byte, error) {
repr := map[string]interface{}{
"type": t.typeMarker(),
}
if len(t.args) > 0 {
repr["args"] = t.args
}
if t.result != nil {
repr["result"] = t.result
}
return json.Marshal(repr)
}
// Union returns a new function represnting the union of t and other. Functions
// must have the same arity to be unioned.
func (t *Function) Union(other *Function) *Function {
if other == nil {
return t
} else if t == nil {
return other
}
a := t.Args()
b := other.Args()
if len(a) != len(b) {
return nil
}
args := make([]Type, len(a))
for i := range a {
args[i] = Or(a[i], b[i])
}
return NewFunction(args, Or(t.Result(), other.Result()))
}
// Compare returns -1, 0, 1 based on comparison between a and b.
func Compare(a, b Type) int {
x := typeOrder(a)
y := typeOrder(b)
if x > y {
return 1
} else if x < y {
return -1
}
switch a.(type) {
case nil, Null, Boolean, Number, String:
return 0
case *Array:
arrA := a.(*Array)
arrB := b.(*Array)
if arrA.dynamic != nil && arrB.dynamic == nil {
return 1
} else if arrB.dynamic != nil && arrA.dynamic == nil {
return -1
}
if arrB.dynamic != nil && arrA.dynamic != nil {
if cmp := Compare(arrA.dynamic, arrB.dynamic); cmp != 0 {
return cmp
}
}
return typeSliceCompare(arrA.static, arrB.static)
case *Object:
objA := a.(*Object)
objB := b.(*Object)
if objA.dynamic != nil && objB.dynamic == nil {
return 1
} else if objB.dynamic != nil && objA.dynamic == nil {
return -1
}
if objA.dynamic != nil && objB.dynamic != nil {
if cmp := Compare(objA.dynamic.Key, objB.dynamic.Key); cmp != 0 {
return cmp
}
if cmp := Compare(objA.dynamic.Value, objB.dynamic.Value); cmp != 0 {
return cmp
}
}
lenStaticA := len(objA.static)
lenStaticB := len(objB.static)
minLen := lenStaticA
if lenStaticB < minLen {
minLen = lenStaticB
}
for i := 0; i < minLen; i++ {
if cmp := util.Compare(objA.static[i].Key, objB.static[i].Key); cmp != 0 {
return cmp
}
if cmp := Compare(objA.static[i].Value, objB.static[i].Value); cmp != 0 {
return cmp
}
}
if lenStaticA < lenStaticB {
return -1
} else if lenStaticB < lenStaticA {
return 1
}
return 0
case *Set:
setA := a.(*Set)
setB := b.(*Set)
if setA.of == nil && setB.of == nil {
return 0
} else if setA.of == nil {
return -1
} else if setB.of == nil {
return 1
}
return Compare(setA.of, setB.of)
case Any:
sl1 := typeSlice(a.(Any))
sl2 := typeSlice(b.(Any))
sort.Sort(sl1)
sort.Sort(sl2)
return typeSliceCompare(sl1, sl2)
case *Function:
fA := a.(*Function)
fB := b.(*Function)
if len(fA.args) < len(fB.args) {
return -1
} else if len(fA.args) > len(fB.args) {
return 1
}
for i := 0; i < len(fA.args); i++ {
if cmp := Compare(fA.args[i], fB.args[i]); cmp != 0 {
return cmp
}
}
return Compare(fA.result, fB.result)
default:
panic("unreachable")
}
}
// Contains returns true if a is a superset or equal to b.
func Contains(a, b Type) bool {
if any, ok := a.(Any); ok {
return any.Contains(b)
}
return Compare(a, b) == 0
}
// Or returns a type that represents the union of a and b. If one type is a
// superset of the other, the superset is returned unchanged.
func Or(a, b Type) Type {
if a == nil {
return b
} else if b == nil {
return a
}
fA, ok1 := a.(*Function)
fB, ok2 := b.(*Function)
if ok1 && ok2 {
return fA.Union(fB)
} else if ok1 || ok2 {
return nil
}
anyA, ok1 := a.(Any)
anyB, ok2 := b.(Any)
if ok1 {
return anyA.Merge(b)
}
if ok2 {
return anyB.Merge(a)
}
if Compare(a, b) == 0 {
return a
}
return NewAny(a, b)
}
// Select returns a property or item of a.
func Select(a Type, x interface{}) Type {
switch a := a.(type) {
case *Array:
n, ok := x.(json.Number)
if !ok {
return nil
}
pos, err := n.Int64()
if err != nil {
return nil
}
return a.Select(int(pos))
case *Object:
return a.Select(x)
case *Set:
tpe := TypeOf(x)
if Compare(a.of, tpe) == 0 {
return a.of
}
if any, ok := a.of.(Any); ok {
if any.Contains(tpe) {
return tpe
}
}
return nil
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
// TODO(tsandall): test nil/nil
tpe = Or(Select(a[i], x), tpe)
}
return tpe
default:
return nil
}
}
// Keys returns the type of keys that can be enumerated for a. For arrays, the
// keys are always number types, for objects the keys are always string types,
// and for sets the keys are always the type of the set element.
func Keys(a Type) Type {
switch a := a.(type) {
case *Array:
return N
case *Object:
var tpe Type
for _, k := range a.Keys() {
tpe = Or(tpe, TypeOf(k))
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Key)
}
return tpe
case *Set:
return a.of
case Any:
// TODO(tsandall): ditto test
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Keys(a[i]), tpe)
}
return tpe
}
return nil
}
// Values returns the type of values that can be enumerated for a.
func Values(a Type) Type {
switch a := a.(type) {
case *Array:
var tpe Type
for i := range a.static {
tpe = Or(tpe, a.static[i])
}
return Or(tpe, a.dynamic)
case *Object:
var tpe Type
for _, v := range a.static {
tpe = Or(tpe, v.Value)
}
if a.dynamic != nil {
tpe = Or(tpe, a.dynamic.Value)
}
return tpe
case *Set:
return a.of
case Any:
if Compare(a, A) == 0 {
return A
}
var tpe Type
for i := range a {
tpe = Or(Values(a[i]), tpe)
}
return tpe
}
return nil
}
// Nil returns true if a's type is unknown.
func Nil(a Type) bool {
switch a := a.(type) {
case nil:
return true
case *Function:
for i := range a.args {
if Nil(a.args[i]) {
return true
}
}
return Nil(a.result)
case *Array:
for i := range a.static {
if Nil(a.static[i]) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic)
}
case *Object:
for i := range a.static {
if Nil(a.static[i].Value) {
return true
}
}
if a.dynamic != nil {
return Nil(a.dynamic.Key) || Nil(a.dynamic.Value)
}
case *Set:
return Nil(a.of)
}
return false
}
// TypeOf returns the type of the Golang native value.
func TypeOf(x interface{}) Type {
switch x := x.(type) {
case nil:
return NewNull()
case bool:
return B
case string:
return S
case json.Number:
return N
case map[interface{}]interface{}:
static := make([]*StaticProperty, 0, len(x))
for k, v := range x {
static = append(static, NewStaticProperty(k, TypeOf(v)))
}
return NewObject(static, nil)
case []interface{}:
static := make([]Type, len(x))
for i := range x {
static[i] = TypeOf(x[i])
}
return NewArray(static, nil)
}
panic("unreachable")
}
type typeSlice []Type
func (s typeSlice) Less(i, j int) bool { return Compare(s[i], s[j]) < 0 }
func (s typeSlice) Swap(i, j int) { x := s[i]; s[i] = s[j]; s[j] = x }
func (s typeSlice) Len() int { return len(s) }
func typeSliceCompare(a, b []Type) int {
minLen := len(a)
if len(b) < minLen {
minLen = len(b)
}
for i := 0; i < minLen; i++ {
if cmp := Compare(a[i], b[i]); cmp != 0 {
return cmp
}
}
if len(a) < len(b) {
return -1
} else if len(b) < len(a) {
return 1
}
return 0
}
func typeOrder(x Type) int {
switch x.(type) {
case Null:
return 0
case Boolean:
return 1
case Number:
return 2
case String:
return 3
case *Array:
return 4
case *Object:
return 5
case *Set:
return 6
case Any:
return 7
case *Function:
return 8
case nil:
return -1
}
panic("unreachable")
}