Debian Codegraph @

pkg/cel/common/typeprovider.go

raw
1 /*
2 Copyright 2024 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package common
18
19 import (
20 "github.com/google/cel-go/cel"
21 "github.com/google/cel-go/common/types"
22 "github.com/google/cel-go/common/types/ref"
23 )
24
25 // TypeResolver resolves a type by a given name.
26 type TypeResolver interface {
27 // Resolve resolves the type by its name.
28 // This function returns false if the name does not refer to a known object type.
29 Resolve(name string) (ResolvedType, bool)
30 }
31
32 // ResolvedType refers an object type that can be looked up for its fields.
33 type ResolvedType interface {
34 ref.Type
35
36 Type() *types.Type
37
38 // Field finds the field by the field name, or false if the field is not known.
39 // This function directly return a FieldType that is known to CEL to be more customizable.
40 Field(name string) (*types.FieldType, bool)
41
42 // FieldNames returns the field names associated with the type, if the type
43 // is found.
44 FieldNames() ([]string, bool)
45
46 // Val creates an instance for the ResolvedType, given its fields and their values.
47 Val(fields map[string]ref.Val) ref.Val
48 }
49
50 // ResolverTypeProvider delegates type resolution first to the TypeResolver and then
51 // to the underlying types.Provider for types not resolved by the TypeResolver.
52 type ResolverTypeProvider struct {
53 typeResolver TypeResolver
54 underlyingTypeProvider types.Provider
55 }
56
57 var _ types.Provider = (*ResolverTypeProvider)(nil)
58
59 // FindStructType returns the Type give a qualified type name, by looking it up with
60 // the DynamicTypeResolver and translating it to CEL Type.
61 // If the type is not known to the DynamicTypeResolver, the lookup falls back to the underlying
62 // ResolverTypeProvider instead.
63 func (p *ResolverTypeProvider) FindStructType(structType string) (*types.Type, bool) {
64 t, ok := p.typeResolver.Resolve(structType)
65 if ok {
66 return types.NewTypeTypeWithParam(t.Type()), true
67 }
68 return p.underlyingTypeProvider.FindStructType(structType)
69 }
70
71 // FindStructFieldNames returns the field names associated with the type, if the type
72 // is found.
73 func (p *ResolverTypeProvider) FindStructFieldNames(structType string) ([]string, bool) {
74 t, ok := p.typeResolver.Resolve(structType)
75 if ok {
76 return t.FieldNames()
77 }
78 return p.underlyingTypeProvider.FindStructFieldNames(structType)
79 }
80
81 // FindStructFieldType returns the field type for a checked type value.
82 // Returns false if the field could not be found.
83 func (p *ResolverTypeProvider) FindStructFieldType(structType, fieldName string) (*types.FieldType, bool) {
84 t, ok := p.typeResolver.Resolve(structType)
85 if ok {
86 return t.Field(fieldName)
87 }
88 return p.underlyingTypeProvider.FindStructFieldType(structType, fieldName)
89 }
90
91 // NewValue creates a new type value from a qualified name and map of fields.
92 func (p *ResolverTypeProvider) NewValue(structType string, fields map[string]ref.Val) ref.Val {
93 t, ok := p.typeResolver.Resolve(structType)
94 if ok {
95 return t.Val(fields)
96 }
97 return p.underlyingTypeProvider.NewValue(structType, fields)
98 }
99
100 func (p *ResolverTypeProvider) EnumValue(enumName string) ref.Val {
101 return p.underlyingTypeProvider.EnumValue(enumName)
102 }
103
104 func (p *ResolverTypeProvider) FindIdent(identName string) (ref.Val, bool) {
105 return p.underlyingTypeProvider.FindIdent(identName)
106 }
107
108 // ResolverEnvOption creates the ResolverTypeProvider with a given DynamicTypeResolver,
109 // and also returns the CEL ResolverEnvOption to apply it to the env.
110 func ResolverEnvOption(resolver TypeResolver) cel.EnvOption {
111 _, envOpt := NewResolverTypeProviderAndEnvOption(resolver)
112 return envOpt
113 }
114
115 // NewResolverTypeProviderAndEnvOption creates the ResolverTypeProvider with a given DynamicTypeResolver,
116 // and also returns the CEL ResolverEnvOption to apply it to the env.
117 func NewResolverTypeProviderAndEnvOption(resolver TypeResolver) (*ResolverTypeProvider, cel.EnvOption) {
118 tp := &ResolverTypeProvider{typeResolver: resolver}
119 var envOption cel.EnvOption = func(e *cel.Env) (*cel.Env, error) {
120 // wrap the existing type provider (acquired from the env)
121 // and set new type provider for the env.
122 tp.underlyingTypeProvider = e.CELTypeProvider()
123 typeProviderOption := cel.CustomTypeProvider(tp)
124 return typeProviderOption(e)
125 }
126 return tp, envOption
127 }