forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
823 lines (752 loc) · 23.7 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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"fmt"
"reflect"
"strings"
chkdecls "github.com/authzed/cel-go/checker/decls"
"github.com/authzed/cel-go/common/types/ref"
"github.com/authzed/cel-go/common/types/traits"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
// Kind indicates a CEL type's kind which is used to differentiate quickly between simple
// and complex types.
type Kind uint
const (
// UnspecifiedKind is returned when the type is nil or its kind is not specified.
UnspecifiedKind Kind = iota
// DynKind represents a dynamic type. This kind only exists at type-check time.
DynKind
// AnyKind represents a google.protobuf.Any type. This kind only exists at type-check time.
// Prefer DynKind to AnyKind as AnyKind has a specific meaning which is based on protobuf
// well-known types.
AnyKind
// BoolKind represents a boolean type.
BoolKind
// BytesKind represents a bytes type.
BytesKind
// DoubleKind represents a double type.
DoubleKind
// DurationKind represents a CEL duration type.
DurationKind
// ErrorKind represents a CEL error type.
ErrorKind
// IntKind represents an integer type.
IntKind
// ListKind represents a list type.
ListKind
// MapKind represents a map type.
MapKind
// NullTypeKind represents a null type.
NullTypeKind
// OpaqueKind represents an abstract type which has no accessible fields.
OpaqueKind
// StringKind represents a string type.
StringKind
// StructKind represents a structured object with typed fields.
StructKind
// TimestampKind represents a a CEL time type.
TimestampKind
// TypeKind represents the CEL type.
TypeKind
// TypeParamKind represents a parameterized type whose type name will be resolved at type-check time, if possible.
TypeParamKind
// UintKind represents a uint type.
UintKind
// UnknownKind represents an unknown value type.
UnknownKind
)
var (
// AnyType represents the google.protobuf.Any type.
AnyType = &Type{
kind: AnyKind,
runtimeTypeName: "google.protobuf.Any",
traitMask: traits.FieldTesterType |
traits.IndexerType,
}
// BoolType represents the bool type.
BoolType = &Type{
kind: BoolKind,
runtimeTypeName: "bool",
traitMask: traits.ComparerType |
traits.NegatorType,
}
// BytesType represents the bytes type.
BytesType = &Type{
kind: BytesKind,
runtimeTypeName: "bytes",
traitMask: traits.AdderType |
traits.ComparerType |
traits.SizerType,
}
// DoubleType represents the double type.
DoubleType = &Type{
kind: DoubleKind,
runtimeTypeName: "double",
traitMask: traits.AdderType |
traits.ComparerType |
traits.DividerType |
traits.MultiplierType |
traits.NegatorType |
traits.SubtractorType,
}
// DurationType represents the CEL duration type.
DurationType = &Type{
kind: DurationKind,
runtimeTypeName: "google.protobuf.Duration",
traitMask: traits.AdderType |
traits.ComparerType |
traits.NegatorType |
traits.ReceiverType |
traits.SubtractorType,
}
// DynType represents a dynamic CEL type whose type will be determined at runtime from context.
DynType = &Type{
kind: DynKind,
runtimeTypeName: "dyn",
}
// ErrorType represents a CEL error value.
ErrorType = &Type{
kind: ErrorKind,
runtimeTypeName: "error",
}
// IntType represents the int type.
IntType = &Type{
kind: IntKind,
runtimeTypeName: "int",
traitMask: traits.AdderType |
traits.ComparerType |
traits.DividerType |
traits.ModderType |
traits.MultiplierType |
traits.NegatorType |
traits.SubtractorType,
}
// ListType represents the runtime list type.
ListType = NewListType(nil)
// MapType represents the runtime map type.
MapType = NewMapType(nil, nil)
// NullType represents the type of a null value.
NullType = &Type{
kind: NullTypeKind,
runtimeTypeName: "null_type",
}
// StringType represents the string type.
StringType = &Type{
kind: StringKind,
runtimeTypeName: "string",
traitMask: traits.AdderType |
traits.ComparerType |
traits.MatcherType |
traits.ReceiverType |
traits.SizerType,
}
// TimestampType represents the time type.
TimestampType = &Type{
kind: TimestampKind,
runtimeTypeName: "google.protobuf.Timestamp",
traitMask: traits.AdderType |
traits.ComparerType |
traits.ReceiverType |
traits.SubtractorType,
}
// TypeType represents a CEL type
TypeType = &Type{
kind: TypeKind,
runtimeTypeName: "type",
}
// UintType represents a uint type.
UintType = &Type{
kind: UintKind,
runtimeTypeName: "uint",
traitMask: traits.AdderType |
traits.ComparerType |
traits.DividerType |
traits.ModderType |
traits.MultiplierType |
traits.SubtractorType,
}
// UnknownType represents an unknown value type.
UnknownType = &Type{
kind: UnknownKind,
runtimeTypeName: "unknown",
}
)
var _ ref.Type = &Type{}
var _ ref.Val = &Type{}
// Type holds a reference to a runtime type with an optional type-checked set of type parameters.
type Type struct {
// kind indicates general category of the type.
kind Kind
// parameters holds the optional type-checked set of type Parameters that are used during static analysis.
parameters []*Type
// runtimeTypeName indicates the runtime type name of the type.
runtimeTypeName string
// isAssignableType function determines whether one type is assignable to this type.
// A nil value for the isAssignableType function falls back to equality of kind, runtimeType, and parameters.
isAssignableType func(other *Type) bool
// isAssignableRuntimeType function determines whether the runtime type (with erasure) is assignable to this type.
// A nil value for the isAssignableRuntimeType function falls back to the equality of the type or type name.
isAssignableRuntimeType func(other ref.Val) bool
// traitMask is a mask of flags which indicate the capabilities of the type.
traitMask int
}
// ConvertToNative implements ref.Val.ConvertToNative.
func (t *Type) ConvertToNative(typeDesc reflect.Type) (any, error) {
return nil, fmt.Errorf("type conversion not supported for 'type'")
}
// ConvertToType implements ref.Val.ConvertToType.
func (t *Type) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal {
case TypeType:
return TypeType
case StringType:
return String(t.TypeName())
}
return NewErr("type conversion error from '%s' to '%s'", TypeType, typeVal)
}
// Equal indicates whether two types have the same runtime type name.
//
// The name Equal is a bit of a misnomer, but for historical reasons, this is the
// runtime behavior. For a more accurate definition see IsType().
func (t *Type) Equal(other ref.Val) ref.Val {
otherType, ok := other.(ref.Type)
return Bool(ok && t.TypeName() == otherType.TypeName())
}
// HasTrait implements the ref.Type interface method.
func (t *Type) HasTrait(trait int) bool {
return trait&t.traitMask == trait
}
// IsExactType indicates whether the two types are exactly the same. This check also verifies type parameter type names.
func (t *Type) IsExactType(other *Type) bool {
return t.isTypeInternal(other, true)
}
// IsEquivalentType indicates whether two types are equivalent. This check ignores type parameter type names.
func (t *Type) IsEquivalentType(other *Type) bool {
return t.isTypeInternal(other, false)
}
// Kind indicates general category of the type.
func (t *Type) Kind() Kind {
if t == nil {
return UnspecifiedKind
}
return t.kind
}
// isTypeInternal checks whether the two types are equivalent or exactly the same based on the checkTypeParamName flag.
func (t *Type) isTypeInternal(other *Type, checkTypeParamName bool) bool {
if t == nil {
return false
}
if t == other {
return true
}
if t.Kind() != other.Kind() || len(t.Parameters()) != len(other.Parameters()) {
return false
}
if (checkTypeParamName || t.Kind() != TypeParamKind) && t.TypeName() != other.TypeName() {
return false
}
for i, p := range t.Parameters() {
if !p.isTypeInternal(other.Parameters()[i], checkTypeParamName) {
return false
}
}
return true
}
// IsAssignableType determines whether the current type is type-check assignable from the input fromType.
func (t *Type) IsAssignableType(fromType *Type) bool {
if t == nil {
return false
}
if t.isAssignableType != nil {
return t.isAssignableType(fromType)
}
return t.defaultIsAssignableType(fromType)
}
// IsAssignableRuntimeType determines whether the current type is runtime assignable from the input runtimeType.
//
// At runtime, parameterized types are erased and so a function which type-checks to support a map(string, string)
// will have a runtime assignable type of a map.
func (t *Type) IsAssignableRuntimeType(val ref.Val) bool {
if t == nil {
return false
}
if t.isAssignableRuntimeType != nil {
return t.isAssignableRuntimeType(val)
}
return t.defaultIsAssignableRuntimeType(val)
}
// Parameters returns the list of type parameters if set.
//
// For ListKind, Parameters()[0] represents the list element type
// For MapKind, Parameters()[0] represents the map key type, and Parameters()[1] represents the map
// value type.
func (t *Type) Parameters() []*Type {
if t == nil {
return emptyParams
}
return t.parameters
}
// DeclaredTypeName indicates the fully qualified and parameterized type-check type name.
func (t *Type) DeclaredTypeName() string {
// if the type itself is neither null, nor dyn, but is assignable to null, then it's a wrapper type.
if t.Kind() != NullTypeKind && !t.isDyn() && t.IsAssignableType(NullType) {
return fmt.Sprintf("wrapper(%s)", t.TypeName())
}
return t.TypeName()
}
// Type implements the ref.Val interface method.
func (t *Type) Type() ref.Type {
return TypeType
}
// Value implements the ref.Val interface method.
func (t *Type) Value() any {
return t.TypeName()
}
// TypeName returns the type-erased fully qualified runtime type name.
//
// TypeName implements the ref.Type interface method.
func (t *Type) TypeName() string {
if t == nil {
return ""
}
return t.runtimeTypeName
}
// WithTraits creates a copy of the current Type and sets the trait mask to the traits parameter.
//
// This method should be used with Opaque types where the type acts like a container, e.g. vector.
func (t *Type) WithTraits(traits int) *Type {
if t == nil {
return nil
}
return &Type{
kind: t.kind,
parameters: t.parameters,
runtimeTypeName: t.runtimeTypeName,
isAssignableType: t.isAssignableType,
isAssignableRuntimeType: t.isAssignableRuntimeType,
traitMask: traits,
}
}
// String returns a human-readable definition of the type name.
func (t *Type) String() string {
if len(t.Parameters()) == 0 {
return t.DeclaredTypeName()
}
params := make([]string, len(t.Parameters()))
for i, p := range t.Parameters() {
params[i] = p.String()
}
return fmt.Sprintf("%s(%s)", t.DeclaredTypeName(), strings.Join(params, ", "))
}
// isDyn indicates whether the type is dynamic in any way.
func (t *Type) isDyn() bool {
k := t.Kind()
return k == DynKind || k == AnyKind || k == TypeParamKind
}
// defaultIsAssignableType provides the standard definition of what it means for one type to be assignable to another
// where any of the following may return a true result:
// - The from types are the same instance
// - The target type is dynamic
// - The fromType has the same kind and type name as the target type, and all parameters of the target type
//
// are IsAssignableType() from the parameters of the fromType.
func (t *Type) defaultIsAssignableType(fromType *Type) bool {
if t == fromType || t.isDyn() {
return true
}
if t.Kind() != fromType.Kind() ||
t.TypeName() != fromType.TypeName() ||
len(t.Parameters()) != len(fromType.Parameters()) {
return false
}
for i, tp := range t.Parameters() {
fp := fromType.Parameters()[i]
if !tp.IsAssignableType(fp) {
return false
}
}
return true
}
// defaultIsAssignableRuntimeType inspects the type and in the case of list and map elements, the key and element types
// to determine whether a ref.Val is assignable to the declared type for a function signature.
func (t *Type) defaultIsAssignableRuntimeType(val ref.Val) bool {
valType := val.Type()
// If the current type and value type don't agree, then return
if !(t.isDyn() || t.TypeName() == valType.TypeName()) {
return false
}
switch t.Kind() {
case ListKind:
elemType := t.Parameters()[0]
l := val.(traits.Lister)
if l.Size() == IntZero {
return true
}
it := l.Iterator()
elemVal := it.Next()
return elemType.IsAssignableRuntimeType(elemVal)
case MapKind:
keyType := t.Parameters()[0]
elemType := t.Parameters()[1]
m := val.(traits.Mapper)
if m.Size() == IntZero {
return true
}
it := m.Iterator()
keyVal := it.Next()
elemVal := m.Get(keyVal)
return keyType.IsAssignableRuntimeType(keyVal) && elemType.IsAssignableRuntimeType(elemVal)
}
return true
}
// NewListType creates an instances of a list type value with the provided element type.
func NewListType(elemType *Type) *Type {
return &Type{
kind: ListKind,
parameters: []*Type{elemType},
runtimeTypeName: "list",
traitMask: traits.AdderType |
traits.ContainerType |
traits.IndexerType |
traits.IterableType |
traits.SizerType,
}
}
// NewMapType creates an instance of a map type value with the provided key and value types.
func NewMapType(keyType, valueType *Type) *Type {
return &Type{
kind: MapKind,
parameters: []*Type{keyType, valueType},
runtimeTypeName: "map",
traitMask: traits.ContainerType |
traits.IndexerType |
traits.IterableType |
traits.SizerType,
}
}
// NewNullableType creates an instance of a nullable type with the provided wrapped type.
//
// Note: only primitive types are supported as wrapped types.
func NewNullableType(wrapped *Type) *Type {
return &Type{
kind: wrapped.Kind(),
parameters: wrapped.Parameters(),
runtimeTypeName: wrapped.TypeName(),
traitMask: wrapped.traitMask,
isAssignableType: func(other *Type) bool {
return NullType.IsAssignableType(other) || wrapped.IsAssignableType(other)
},
isAssignableRuntimeType: func(other ref.Val) bool {
return NullType.IsAssignableRuntimeType(other) || wrapped.IsAssignableRuntimeType(other)
},
}
}
// NewOptionalType creates an abstract parameterized type instance corresponding to CEL's notion of optional.
func NewOptionalType(param *Type) *Type {
return NewOpaqueType("optional_type", param)
}
// NewOpaqueType creates an abstract parameterized type with a given name.
func NewOpaqueType(name string, params ...*Type) *Type {
return &Type{
kind: OpaqueKind,
parameters: params,
runtimeTypeName: name,
}
}
// NewObjectType creates a type reference to an externally defined type, e.g. a protobuf message type.
//
// An object type is assumed to support field presence testing and field indexing. Additionally, the
// type may also indicate additional traits through the use of the optional traits vararg argument.
func NewObjectType(typeName string, traits ...int) *Type {
// Function sanitizes object types on the fly
if wkt, found := checkedWellKnowns[typeName]; found {
return wkt
}
traitMask := 0
for _, trait := range traits {
traitMask |= trait
}
return &Type{
kind: StructKind,
parameters: emptyParams,
runtimeTypeName: typeName,
traitMask: structTypeTraitMask | traitMask,
}
}
// NewObjectTypeValue creates a type reference to an externally defined type.
//
// Deprecated: use cel.ObjectType(typeName)
func NewObjectTypeValue(typeName string) *Type {
return NewObjectType(typeName)
}
// NewTypeValue creates an opaque type which has a set of optional type traits as defined in
// the common/types/traits package.
//
// Deprecated: use cel.ObjectType(typeName, traits)
func NewTypeValue(typeName string, traits ...int) *Type {
traitMask := 0
for _, trait := range traits {
traitMask |= trait
}
return &Type{
kind: StructKind,
parameters: emptyParams,
runtimeTypeName: typeName,
traitMask: traitMask,
}
}
// NewTypeParamType creates a parameterized type instance.
func NewTypeParamType(paramName string) *Type {
return &Type{
kind: TypeParamKind,
runtimeTypeName: paramName,
}
}
// NewTypeTypeWithParam creates a type with a type parameter.
// Used for type-checking purposes, but equivalent to TypeType otherwise.
func NewTypeTypeWithParam(param *Type) *Type {
return &Type{
kind: TypeKind,
runtimeTypeName: "type",
parameters: []*Type{param},
}
}
// TypeToExprType converts a CEL-native type representation to a protobuf CEL Type representation.
func TypeToExprType(t *Type) (*exprpb.Type, error) {
switch t.Kind() {
case AnyKind:
return chkdecls.Any, nil
case BoolKind:
return maybeWrapper(t, chkdecls.Bool), nil
case BytesKind:
return maybeWrapper(t, chkdecls.Bytes), nil
case DoubleKind:
return maybeWrapper(t, chkdecls.Double), nil
case DurationKind:
return chkdecls.Duration, nil
case DynKind:
return chkdecls.Dyn, nil
case ErrorKind:
return chkdecls.Error, nil
case IntKind:
return maybeWrapper(t, chkdecls.Int), nil
case ListKind:
if len(t.Parameters()) != 1 {
return nil, fmt.Errorf("invalid list, got %d parameters, wanted one", len(t.Parameters()))
}
et, err := TypeToExprType(t.Parameters()[0])
if err != nil {
return nil, err
}
return chkdecls.NewListType(et), nil
case MapKind:
if len(t.Parameters()) != 2 {
return nil, fmt.Errorf("invalid map, got %d parameters, wanted two", len(t.Parameters()))
}
kt, err := TypeToExprType(t.Parameters()[0])
if err != nil {
return nil, err
}
vt, err := TypeToExprType(t.Parameters()[1])
if err != nil {
return nil, err
}
return chkdecls.NewMapType(kt, vt), nil
case NullTypeKind:
return chkdecls.Null, nil
case OpaqueKind:
params := make([]*exprpb.Type, len(t.Parameters()))
for i, p := range t.Parameters() {
pt, err := TypeToExprType(p)
if err != nil {
return nil, err
}
params[i] = pt
}
return chkdecls.NewAbstractType(t.TypeName(), params...), nil
case StringKind:
return maybeWrapper(t, chkdecls.String), nil
case StructKind:
return chkdecls.NewObjectType(t.TypeName()), nil
case TimestampKind:
return chkdecls.Timestamp, nil
case TypeParamKind:
return chkdecls.NewTypeParamType(t.TypeName()), nil
case TypeKind:
if len(t.Parameters()) == 1 {
p, err := TypeToExprType(t.Parameters()[0])
if err != nil {
return nil, err
}
return chkdecls.NewTypeType(p), nil
}
return chkdecls.NewTypeType(nil), nil
case UintKind:
return maybeWrapper(t, chkdecls.Uint), nil
}
return nil, fmt.Errorf("missing type conversion to proto: %v", t)
}
// ExprTypeToType converts a protobuf CEL type representation to a CEL-native type representation.
func ExprTypeToType(t *exprpb.Type) (*Type, error) {
switch t.GetTypeKind().(type) {
case *exprpb.Type_Dyn:
return DynType, nil
case *exprpb.Type_AbstractType_:
paramTypes := make([]*Type, len(t.GetAbstractType().GetParameterTypes()))
for i, p := range t.GetAbstractType().GetParameterTypes() {
pt, err := ExprTypeToType(p)
if err != nil {
return nil, err
}
paramTypes[i] = pt
}
return NewOpaqueType(t.GetAbstractType().GetName(), paramTypes...), nil
case *exprpb.Type_ListType_:
et, err := ExprTypeToType(t.GetListType().GetElemType())
if err != nil {
return nil, err
}
return NewListType(et), nil
case *exprpb.Type_MapType_:
kt, err := ExprTypeToType(t.GetMapType().GetKeyType())
if err != nil {
return nil, err
}
vt, err := ExprTypeToType(t.GetMapType().GetValueType())
if err != nil {
return nil, err
}
return NewMapType(kt, vt), nil
case *exprpb.Type_MessageType:
return NewObjectType(t.GetMessageType()), nil
case *exprpb.Type_Null:
return NullType, nil
case *exprpb.Type_Primitive:
switch t.GetPrimitive() {
case exprpb.Type_BOOL:
return BoolType, nil
case exprpb.Type_BYTES:
return BytesType, nil
case exprpb.Type_DOUBLE:
return DoubleType, nil
case exprpb.Type_INT64:
return IntType, nil
case exprpb.Type_STRING:
return StringType, nil
case exprpb.Type_UINT64:
return UintType, nil
default:
return nil, fmt.Errorf("unsupported primitive type: %v", t)
}
case *exprpb.Type_TypeParam:
return NewTypeParamType(t.GetTypeParam()), nil
case *exprpb.Type_Type:
if t.GetType().GetTypeKind() != nil {
p, err := ExprTypeToType(t.GetType())
if err != nil {
return nil, err
}
return NewTypeTypeWithParam(p), nil
}
return TypeType, nil
case *exprpb.Type_WellKnown:
switch t.GetWellKnown() {
case exprpb.Type_ANY:
return AnyType, nil
case exprpb.Type_DURATION:
return DurationType, nil
case exprpb.Type_TIMESTAMP:
return TimestampType, nil
default:
return nil, fmt.Errorf("unsupported well-known type: %v", t)
}
case *exprpb.Type_Wrapper:
t, err := ExprTypeToType(&exprpb.Type{TypeKind: &exprpb.Type_Primitive{Primitive: t.GetWrapper()}})
if err != nil {
return nil, err
}
return NewNullableType(t), nil
case *exprpb.Type_Error:
return ErrorType, nil
default:
return nil, fmt.Errorf("unsupported type: %v", t)
}
}
func maybeWrapper(t *Type, pbType *exprpb.Type) *exprpb.Type {
if t.IsAssignableType(NullType) {
return chkdecls.NewWrapperType(pbType)
}
return pbType
}
func maybeForeignType(t ref.Type) *Type {
if celType, ok := t.(*Type); ok {
return celType
}
// Inspect the incoming type to determine its traits. The assumption will be that the incoming
// type does not have any field values; however, if the trait mask indicates that field testing
// and indexing are supported, the foreign type is marked as a struct.
traitMask := 0
for _, trait := range allTraits {
if t.HasTrait(trait) {
traitMask |= trait
}
}
// Treat the value like a struct. If it has no fields, this is harmless to denote the type
// as such since it basically becomes an opaque type by convention.
return NewObjectType(t.TypeName(), traitMask)
}
var (
checkedWellKnowns = map[string]*Type{
// Wrapper types.
"google.protobuf.BoolValue": NewNullableType(BoolType),
"google.protobuf.BytesValue": NewNullableType(BytesType),
"google.protobuf.DoubleValue": NewNullableType(DoubleType),
"google.protobuf.FloatValue": NewNullableType(DoubleType),
"google.protobuf.Int64Value": NewNullableType(IntType),
"google.protobuf.Int32Value": NewNullableType(IntType),
"google.protobuf.UInt64Value": NewNullableType(UintType),
"google.protobuf.UInt32Value": NewNullableType(UintType),
"google.protobuf.StringValue": NewNullableType(StringType),
// Well-known types.
"google.protobuf.Any": AnyType,
"google.protobuf.Duration": DurationType,
"google.protobuf.Timestamp": TimestampType,
// Json types.
"google.protobuf.ListValue": NewListType(DynType),
"google.protobuf.NullValue": NullType,
"google.protobuf.Struct": NewMapType(StringType, DynType),
"google.protobuf.Value": DynType,
}
emptyParams = []*Type{}
allTraits = []int{
traits.AdderType,
traits.ComparerType,
traits.ContainerType,
traits.DividerType,
traits.FieldTesterType,
traits.IndexerType,
traits.IterableType,
traits.IteratorType,
traits.MatcherType,
traits.ModderType,
traits.MultiplierType,
traits.NegatorType,
traits.ReceiverType,
traits.SizerType,
traits.SubtractorType,
}
structTypeTraitMask = traits.FieldTesterType | traits.IndexerType
)