| 1 |
// Copyright 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package cel |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"math" |
| 21 |
"reflect" |
| 22 |
"strings" |
| 23 |
"sync" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/google/cel-go/common" |
| 27 |
"github.com/google/cel-go/common/ast" |
| 28 |
"github.com/google/cel-go/common/decls" |
| 29 |
"github.com/google/cel-go/common/env" |
| 30 |
"github.com/google/cel-go/common/operators" |
| 31 |
"github.com/google/cel-go/common/types" |
| 32 |
"github.com/google/cel-go/common/types/ref" |
| 33 |
|
| 34 |
"google.golang.org/protobuf/proto" |
| 35 |
|
| 36 |
proto3pb "github.com/google/cel-go/test/proto3pb" |
| 37 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 38 |
) |
| 39 |
|
| 40 |
func TestAstNil(t *testing.T) { |
| 41 |
var ast *Ast |
| 42 |
if ast.IsChecked() { |
| 43 |
t.Error("ast.IsChecked() returned true for nil ast") |
| 44 |
} |
| 45 |
if ast.Expr() != nil { |
| 46 |
t.Errorf("ast.Expr() got %v, wanted nil", ast.Expr()) |
| 47 |
} |
| 48 |
if ast.SourceInfo() != nil { |
| 49 |
t.Errorf("ast.SourceInfo() got %v, wanted nil", ast.SourceInfo()) |
| 50 |
} |
| 51 |
if ast.OutputType() != types.ErrorType { |
| 52 |
t.Errorf("ast.OutputType() got %v, wanted error type", ast.OutputType()) |
| 53 |
} |
| 54 |
if ast.Source() != nil { |
| 55 |
t.Errorf("ast.Source() got %v, wanted nil", ast.Source()) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestIssuesNil(t *testing.T) { |
| 60 |
var iss *Issues |
| 61 |
iss = iss.Append(iss) |
| 62 |
if iss.Err() != nil { |
| 63 |
t.Errorf("iss.Err() got %v, wanted nil given nil issue set", iss.Err()) |
| 64 |
} |
| 65 |
if len(iss.Errors()) != 0 { |
| 66 |
t.Errorf("iss.Errors() got %v, wanted empty value", iss.Errors()) |
| 67 |
} |
| 68 |
if iss.String() != "" { |
| 69 |
t.Errorf("iss.String() returned %v, wanted empty value", iss.String()) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestIssuesEmpty(t *testing.T) { |
| 74 |
iss := NewIssues(common.NewErrors(nil)) |
| 75 |
if iss.Err() != nil { |
| 76 |
t.Errorf("iss.Err() got %v, wanted nil given nil issue set", iss.Err()) |
| 77 |
} |
| 78 |
if len(iss.Errors()) != 0 { |
| 79 |
t.Errorf("iss.Errors() got %v, wanted empty value", iss.Errors()) |
| 80 |
} |
| 81 |
if iss.String() != "" { |
| 82 |
t.Errorf("iss.String() returned %v, wanted empty value", iss.String()) |
| 83 |
} |
| 84 |
var iss2 *Issues |
| 85 |
iss3 := iss.Append(iss2) |
| 86 |
iss4 := iss3.Append(nil) |
| 87 |
if !reflect.DeepEqual(iss4, iss) { |
| 88 |
t.Error("Append() with a nil value resulted in the creation of a new issue set") |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestIssuesAppendSelf(t *testing.T) { |
| 93 |
e, err := NewEnv() |
| 94 |
if err != nil { |
| 95 |
t.Fatalf("NewEnv() failed: %v", err) |
| 96 |
} |
| 97 |
_, iss := e.Compile("a") |
| 98 |
if len(iss.Errors()) != 1 { |
| 99 |
t.Errorf("iss.Errors() got %v, wanted 1 error", iss.Errors()) |
| 100 |
} |
| 101 |
iss = iss.Append(iss) |
| 102 |
if len(iss.Errors()) != 1 { |
| 103 |
t.Errorf("iss.Errors() got %v, wanted 1 error", iss.Errors()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestIssues(t *testing.T) { |
| 108 |
e, err := NewEnv() |
| 109 |
if err != nil { |
| 110 |
t.Fatalf("NewEnv() failed: %v", err) |
| 111 |
} |
| 112 |
_, iss := e.Compile("-") |
| 113 |
_, iss2 := e.Compile("b") |
| 114 |
iss = iss.Append(iss2) |
| 115 |
if len(iss.Errors()) != 3 { |
| 116 |
t.Errorf("iss.Errors() got %v, wanted 3 errors", iss.Errors()) |
| 117 |
} |
| 118 |
|
| 119 |
wantIss := `ERROR: <input>:1:1: undeclared reference to 'b' (in container '') |
| 120 |
| - |
| 121 |
| ^ |
| 122 |
ERROR: <input>:1:2: Syntax error: no viable alternative at input '-' |
| 123 |
| - |
| 124 |
| .^ |
| 125 |
ERROR: <input>:1:2: Syntax error: mismatched input '<EOF>' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER} |
| 126 |
| - |
| 127 |
| .^` |
| 128 |
if iss.String() != wantIss { |
| 129 |
t.Errorf("iss.String() returned %v, wanted %v", iss.String(), wantIss) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestFormatCELTypeEquivalence(t *testing.T) { |
| 134 |
values := []*Type{ |
| 135 |
AnyType, |
| 136 |
MapType(StringType, DynType), |
| 137 |
types.NewTypeTypeWithParam(ListType(IntType)), |
| 138 |
TypeType, |
| 139 |
NullableType(DoubleType), |
| 140 |
} |
| 141 |
for _, v := range values { |
| 142 |
v := v |
| 143 |
t.Run(v.String(), func(t *testing.T) { |
| 144 |
celStr := FormatCELType(v) |
| 145 |
et, err := TypeToExprType(v) |
| 146 |
if err != nil { |
| 147 |
t.Fatalf("TypeToExprType(%v) failed: %v", v, err) |
| 148 |
} |
| 149 |
exprStr := FormatType(et) |
| 150 |
if celStr != exprStr { |
| 151 |
t.Errorf("FormatCELType(%v) got %s, wanted %s", v, celStr, exprStr) |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestEnvCheckExtendRace(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
for i := 0; i < 500; i++ { |
| 160 |
wg := &sync.WaitGroup{} |
| 161 |
wg.Add(2) |
| 162 |
env, err := NewCustomEnv(StdLib()) |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("NewCustomEnv() failed: %v", err) |
| 165 |
} |
| 166 |
t.Run(fmt.Sprintf("Compile[%d]", i), func(t *testing.T) { |
| 167 |
go func() { |
| 168 |
defer wg.Done() |
| 169 |
_, _ = env.Compile(`1 + 1 * 20 < 400`) |
| 170 |
}() |
| 171 |
}) |
| 172 |
t.Run(fmt.Sprintf("Extend[%d]", i), func(t *testing.T) { |
| 173 |
go func() { |
| 174 |
defer wg.Done() |
| 175 |
_, _ = env.Extend(Variable("bar", BoolType)) |
| 176 |
}() |
| 177 |
}) |
| 178 |
wg.Wait() |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestEnvPartialVarsError(t *testing.T) { |
| 183 |
env := testEnv(t) |
| 184 |
_, err := env.PartialVars(10) |
| 185 |
if err == nil { |
| 186 |
t.Error("env.PartialVars(10) succeeded, wanted error") |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestTypeProviderInterop(t *testing.T) { |
| 191 |
reg, err := types.NewRegistry(&proto3pb.TestAllTypes{}) |
| 192 |
if err != nil { |
| 193 |
t.Fatalf("types.NewRegistry() failed: %v", err) |
| 194 |
} |
| 195 |
tests := []struct { |
| 196 |
name string |
| 197 |
provider any |
| 198 |
}{ |
| 199 |
{ |
| 200 |
name: "custom provider", |
| 201 |
provider: &customCELProvider{provider: reg}, |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "custom legacy provider", |
| 205 |
provider: &customLegacyProvider{provider: reg}, |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "provider", |
| 209 |
provider: reg, |
| 210 |
}, |
| 211 |
} |
| 212 |
for _, tst := range tests { |
| 213 |
tc := tst |
| 214 |
t.Run(tc.name, func(t *testing.T) { |
| 215 |
env, err := NewEnv(CustomTypeProvider(tc.provider)) |
| 216 |
if err != nil { |
| 217 |
t.Fatalf("NewEnv(CustomTypeProvider()) failed: %v", err) |
| 218 |
} |
| 219 |
// Found type |
| 220 |
pbType, found := env.TypeProvider().FindType("google.expr.proto3.test.TestAllTypes") |
| 221 |
if !found { |
| 222 |
t.Fatal("FindType(google.expr.proto3.test.TestAllTypes) failed") |
| 223 |
} |
| 224 |
celType, found := env.CELTypeProvider().FindStructType("google.expr.proto3.test.TestAllTypes") |
| 225 |
if !found { |
| 226 |
t.Fatal("FindStructType(google.expr.proto3.test.TestAllTypes) failed") |
| 227 |
} |
| 228 |
pbConvType, err := types.ExprTypeToType(pbType) |
| 229 |
if err != nil { |
| 230 |
t.Fatalf("types.ExprTypeToType(%v) failed: %v", pbType, err) |
| 231 |
} |
| 232 |
if !celType.IsExactType(pbConvType) { |
| 233 |
t.Errorf("got converted type %v, wanted %v", pbConvType, celType) |
| 234 |
} |
| 235 |
// Found field |
| 236 |
pbFieldType, found := env.TypeProvider().FindFieldType("google.expr.proto3.test.TestAllTypes", "single_int32") |
| 237 |
if !found { |
| 238 |
t.Fatal("FindFieldType(google.expr.proto3.test.TestAllTypes, single_int32) not found") |
| 239 |
} |
| 240 |
celFieldType, found := env.CELTypeProvider().FindStructFieldType("google.expr.proto3.test.TestAllTypes", "single_int32") |
| 241 |
if !found { |
| 242 |
t.Fatal("FindStructFieldType(google.expr.proto3.test.TestAllTypes, single_int32) not found") |
| 243 |
} |
| 244 |
pbConvFieldType, err := types.ExprTypeToType(pbFieldType.Type) |
| 245 |
if err != nil { |
| 246 |
t.Fatalf("types.ExprTypeToType(%v) failed: %v", pbFieldType.Type, err) |
| 247 |
} |
| 248 |
if !celFieldType.Type.IsExactType(pbConvFieldType) { |
| 249 |
t.Errorf("got converted field type %v, wanted %v", pbConvFieldType, celFieldType) |
| 250 |
} |
| 251 |
// Not found type |
| 252 |
_, found = env.TypeProvider().FindType("test.BadTypeName") |
| 253 |
if found { |
| 254 |
t.Fatal("FindType(test.BadTypeName) succeeded") |
| 255 |
} |
| 256 |
_, found = env.CELTypeProvider().FindStructType("test.BadTypeName") |
| 257 |
if found { |
| 258 |
t.Fatal("FindStructType(test.BadTypeName) succeeded") |
| 259 |
} |
| 260 |
// Not found field |
| 261 |
_, found = env.TypeProvider().FindFieldType("google.expr.proto3.test.TestAllTypes", "undefined_field") |
| 262 |
if found { |
| 263 |
t.Fatal("FindFieldType(google.expr.proto3.test.TestAllTypes, undefined_field) was found") |
| 264 |
} |
| 265 |
_, found = env.CELTypeProvider().FindStructFieldType("google.expr.proto3.test.TestAllTypes", "undefined_field") |
| 266 |
if found { |
| 267 |
t.Fatal("FindStructFieldType(google.expr.proto3.test.TestAllTypes, undefined_field) not found") |
| 268 |
} |
| 269 |
}) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestLibraries(t *testing.T) { |
| 274 |
e, err := NewEnv(OptionalTypes()) |
| 275 |
if err != nil { |
| 276 |
t.Fatalf("NewEnv() failed: %v", err) |
| 277 |
} |
| 278 |
for _, expected := range []string{"cel.lib.std", "cel.lib.optional"} { |
| 279 |
if !e.HasLibrary(expected) { |
| 280 |
t.Errorf("Expected HasLibrary() to return true for '%s'", expected) |
| 281 |
} |
| 282 |
libMap := map[string]struct{}{} |
| 283 |
libraries := e.Libraries() |
| 284 |
for _, lib := range libraries { |
| 285 |
libMap[lib] = struct{}{} |
| 286 |
} |
| 287 |
if len(libraries) != 2 { |
| 288 |
t.Errorf("Expected HasLibrary() to contain exactly 2 libraries but got: %v", libraries) |
| 289 |
} |
| 290 |
|
| 291 |
if _, ok := libMap[expected]; !ok { |
| 292 |
t.Errorf("Expected Libraries() to include '%s'", expected) |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
func TestFunctions(t *testing.T) { |
| 298 |
e, err := NewEnv(OptionalTypes()) |
| 299 |
if err != nil { |
| 300 |
t.Fatalf("NewEnv() failed: %v", err) |
| 301 |
} |
| 302 |
for _, expected := range []string{"optional.of", "or"} { |
| 303 |
if !e.HasFunction(expected) { |
| 304 |
t.Errorf("Expected HasFunction() to return true for '%s'", expected) |
| 305 |
} |
| 306 |
|
| 307 |
if _, ok := e.Functions()[expected]; !ok { |
| 308 |
t.Errorf("Expected Functions() to include '%s'", expected) |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
func TestEnvToConfig(t *testing.T) { |
| 314 |
tests := []struct { |
| 315 |
name string |
| 316 |
opts []EnvOption |
| 317 |
want *env.Config |
| 318 |
}{ |
| 319 |
{ |
| 320 |
name: "std env", |
| 321 |
want: env.NewConfig("std env"), |
| 322 |
}, |
| 323 |
{ |
| 324 |
name: "std env - container", |
| 325 |
opts: []EnvOption{ |
| 326 |
Container("example.container"), |
| 327 |
}, |
| 328 |
want: env.NewConfig("std env - container").SetContainer("example.container"), |
| 329 |
}, |
| 330 |
{ |
| 331 |
name: "std env - aliases", |
| 332 |
opts: []EnvOption{ |
| 333 |
Abbrevs("example.type.name"), |
| 334 |
}, |
| 335 |
want: env.NewConfig("std env - aliases").AddImports(env.NewImport("example.type.name")), |
| 336 |
}, |
| 337 |
{ |
| 338 |
name: "std env disabled", |
| 339 |
opts: []EnvOption{ |
| 340 |
func(*Env) (*Env, error) { |
| 341 |
return NewCustomEnv() |
| 342 |
}, |
| 343 |
}, |
| 344 |
want: env.NewConfig("std env disabled").SetStdLib( |
| 345 |
env.NewLibrarySubset().SetDisabled(true)), |
| 346 |
}, |
| 347 |
{ |
| 348 |
name: "std env - with variable", |
| 349 |
opts: []EnvOption{ |
| 350 |
Variable("var", IntType), |
| 351 |
}, |
| 352 |
want: env.NewConfig("std env - with variable").AddVariables(env.NewVariable("var", env.NewTypeDesc("int"))), |
| 353 |
}, |
| 354 |
{ |
| 355 |
name: "std env - with function", |
| 356 |
opts: []EnvOption{Function("hello", Overload("hello_string", []*Type{StringType}, StringType))}, |
| 357 |
want: env.NewConfig("std env - with function").AddFunctions( |
| 358 |
env.NewFunction("hello", |
| 359 |
env.NewOverload("hello_string", |
| 360 |
[]*env.TypeDesc{env.NewTypeDesc("string")}, env.NewTypeDesc("string")), |
| 361 |
)), |
| 362 |
}, |
| 363 |
{ |
| 364 |
name: "optional lib", |
| 365 |
opts: []EnvOption{ |
| 366 |
OptionalTypes(), |
| 367 |
}, |
| 368 |
want: env.NewConfig("optional lib").AddExtensions(env.NewExtension("optional", math.MaxUint32)), |
| 369 |
}, |
| 370 |
{ |
| 371 |
name: "optional lib - versioned", |
| 372 |
opts: []EnvOption{ |
| 373 |
OptionalTypes(OptionalTypesVersion(1)), |
| 374 |
}, |
| 375 |
want: env.NewConfig("optional lib - versioned").AddExtensions(env.NewExtension("optional", 1)), |
| 376 |
}, |
| 377 |
{ |
| 378 |
name: "optional lib - alt last()", |
| 379 |
opts: []EnvOption{ |
| 380 |
OptionalTypes(), |
| 381 |
Function("last", |
| 382 |
FunctionDocs(`return the last value in a list, or last character in a string`), |
| 383 |
MemberOverload("string_last", []*Type{StringType}, StringType)), |
| 384 |
}, |
| 385 |
want: env.NewConfig("optional lib - alt last()"). |
| 386 |
AddExtensions(env.NewExtension("optional", math.MaxUint32)). |
| 387 |
AddFunctions(env.NewFunctionWithDoc("last", |
| 388 |
`return the last value in a list, or last character in a string`, |
| 389 |
env.NewMemberOverload("string_last", env.NewTypeDesc("string"), []*env.TypeDesc{}, env.NewTypeDesc("string")), |
| 390 |
)), |
| 391 |
}, |
| 392 |
{ |
| 393 |
name: "context proto - with extra variable", |
| 394 |
opts: []EnvOption{ |
| 395 |
DeclareContextProto((&proto3pb.TestAllTypes{}).ProtoReflect().Descriptor()), |
| 396 |
Variable("extra", StringType), |
| 397 |
}, |
| 398 |
want: env.NewConfig("context proto - with extra variable"). |
| 399 |
SetContextVariable(env.NewContextVariable("google.expr.proto3.test.TestAllTypes")). |
| 400 |
AddVariables(env.NewVariable("extra", env.NewTypeDesc("string"))), |
| 401 |
}, |
| 402 |
{ |
| 403 |
name: "context proto", |
| 404 |
opts: []EnvOption{ |
| 405 |
DeclareContextProto((&proto3pb.TestAllTypes{}).ProtoReflect().Descriptor()), |
| 406 |
}, |
| 407 |
want: env.NewConfig("context proto").SetContextVariable(env.NewContextVariable("google.expr.proto3.test.TestAllTypes")), |
| 408 |
}, |
| 409 |
{ |
| 410 |
name: "feature flags", |
| 411 |
opts: []EnvOption{ |
| 412 |
DefaultUTCTimeZone(false), |
| 413 |
EnableMacroCallTracking(), |
| 414 |
}, |
| 415 |
want: env.NewConfig("feature flags").AddFeatures( |
| 416 |
env.NewFeature("cel.feature.macro_call_tracking", true), |
| 417 |
), |
| 418 |
}, |
| 419 |
{ |
| 420 |
name: "validators", |
| 421 |
opts: []EnvOption{ |
| 422 |
ExtendedValidations(), |
| 423 |
ASTValidators(ValidateComprehensionNestingLimit(1)), |
| 424 |
}, |
| 425 |
want: env.NewConfig("validators").AddValidators( |
| 426 |
env.NewValidator("cel.validator.duration"), |
| 427 |
env.NewValidator("cel.validator.timestamp"), |
| 428 |
env.NewValidator("cel.validator.matches"), |
| 429 |
env.NewValidator("cel.validator.homogeneous_literals"), |
| 430 |
env.NewValidator("cel.validator.comprehension_nesting_limit").SetConfig(map[string]any{"limit": 1}), |
| 431 |
), |
| 432 |
}, |
| 433 |
} |
| 434 |
|
| 435 |
for _, tst := range tests { |
| 436 |
tc := tst |
| 437 |
t.Run(tc.name, func(t *testing.T) { |
| 438 |
e, err := NewEnv(tc.opts...) |
| 439 |
if err != nil { |
| 440 |
t.Fatalf("NewEnv() failed: %v", err) |
| 441 |
} |
| 442 |
gotConfig, err := e.ToConfig(tc.name) |
| 443 |
if err != nil { |
| 444 |
t.Fatalf("ToConfig() failed: %v", err) |
| 445 |
} |
| 446 |
if !reflect.DeepEqual(gotConfig, tc.want) { |
| 447 |
t.Errorf("e.Config() got %v, wanted %v", gotConfig, tc.want) |
| 448 |
} |
| 449 |
}) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
func TestEnvFromConfig(t *testing.T) { |
| 454 |
type exprCase struct { |
| 455 |
name string |
| 456 |
in any |
| 457 |
expr string |
| 458 |
iss error |
| 459 |
out ref.Val |
| 460 |
} |
| 461 |
tests := []struct { |
| 462 |
name string |
| 463 |
beforeOpts []EnvOption |
| 464 |
afterOpts []EnvOption |
| 465 |
conf *env.Config |
| 466 |
confHandlers []ConfigOptionFactory |
| 467 |
exprs []exprCase |
| 468 |
}{ |
| 469 |
{ |
| 470 |
name: "std env", |
| 471 |
conf: env.NewConfig("std env"), |
| 472 |
exprs: []exprCase{ |
| 473 |
{ |
| 474 |
name: "literal", |
| 475 |
expr: "'hello world'", |
| 476 |
out: types.String("hello world"), |
| 477 |
}, |
| 478 |
{ |
| 479 |
name: "size", |
| 480 |
expr: "'hello world'.size()", |
| 481 |
out: types.Int(11), |
| 482 |
}, |
| 483 |
}, |
| 484 |
}, |
| 485 |
{ |
| 486 |
name: "std env - imports", |
| 487 |
beforeOpts: []EnvOption{Types(&proto3pb.TestAllTypes{})}, |
| 488 |
conf: env.NewConfig("std env - context proto"). |
| 489 |
AddImports(env.NewImport("google.expr.proto3.test.TestAllTypes")), |
| 490 |
exprs: []exprCase{ |
| 491 |
{ |
| 492 |
name: "literal", |
| 493 |
expr: "TestAllTypes{single_int64: 15}.single_int64", |
| 494 |
out: types.Int(15), |
| 495 |
}, |
| 496 |
}, |
| 497 |
}, |
| 498 |
{ |
| 499 |
name: "std env - context proto", |
| 500 |
beforeOpts: []EnvOption{Types(&proto3pb.TestAllTypes{})}, |
| 501 |
conf: env.NewConfig("std env - context proto"). |
| 502 |
SetContainer("google.expr.proto3.test"). |
| 503 |
SetContextVariable(env.NewContextVariable("google.expr.proto3.test.TestAllTypes")), |
| 504 |
exprs: []exprCase{ |
| 505 |
{ |
| 506 |
name: "field select literal", |
| 507 |
in: mustContextProto(t, &proto3pb.TestAllTypes{SingleInt64: 10}), |
| 508 |
expr: "TestAllTypes{single_int64: single_int64}.single_int64", |
| 509 |
out: types.Int(10), |
| 510 |
}, |
| 511 |
}, |
| 512 |
}, |
| 513 |
{ |
| 514 |
name: "custom env - variables", |
| 515 |
beforeOpts: []EnvOption{Types(&proto3pb.TestAllTypes{})}, |
| 516 |
conf: env.NewConfig("custom env - variables"). |
| 517 |
SetStdLib(env.NewLibrarySubset().SetDisabled(true)). |
| 518 |
SetContainer("google.expr.proto3.test"). |
| 519 |
AddVariables(env.NewVariable("single_int64", env.NewTypeDesc("int"))), |
| 520 |
exprs: []exprCase{ |
| 521 |
{ |
| 522 |
name: "field select literal", |
| 523 |
in: map[string]any{"single_int64": 42}, |
| 524 |
expr: "TestAllTypes{single_int64: single_int64}.single_int64", |
| 525 |
out: types.Int(42), |
| 526 |
}, |
| 527 |
{ |
| 528 |
name: "invalid operator", |
| 529 |
in: map[string]any{"single_int64": 42}, |
| 530 |
expr: "TestAllTypes{single_int64: single_int64}.single_int64 + 1", |
| 531 |
iss: errors.New("undeclared reference"), |
| 532 |
}, |
| 533 |
}, |
| 534 |
}, |
| 535 |
{ |
| 536 |
name: "custom env - functions", |
| 537 |
afterOpts: []EnvOption{ |
| 538 |
Function("plus", |
| 539 |
MemberOverload("int_plus_int", []*Type{IntType, IntType}, IntType, |
| 540 |
BinaryBinding(func(lhs, rhs ref.Val) ref.Val { |
| 541 |
l := lhs.(types.Int) |
| 542 |
r := rhs.(types.Int) |
| 543 |
return l + r |
| 544 |
}), |
| 545 |
), |
| 546 |
)}, |
| 547 |
conf: env.NewConfig("custom env - functions"). |
| 548 |
SetStdLib(env.NewLibrarySubset().SetDisabled(true)). |
| 549 |
AddVariables(env.NewVariable("x", env.NewTypeDesc("int"))). |
| 550 |
AddFunctions(env.NewFunction("plus", |
| 551 |
env.NewMemberOverload("int_plus_int", |
| 552 |
env.NewTypeDesc("int"), |
| 553 |
[]*env.TypeDesc{env.NewTypeDesc("int")}, |
| 554 |
env.NewTypeDesc("int"), |
| 555 |
), |
| 556 |
)), |
| 557 |
exprs: []exprCase{ |
| 558 |
{ |
| 559 |
name: "plus", |
| 560 |
in: map[string]any{"x": 42}, |
| 561 |
expr: "x.plus(2)", |
| 562 |
out: types.Int(44), |
| 563 |
}, |
| 564 |
{ |
| 565 |
name: "plus invalid type", |
| 566 |
in: map[string]any{"x": 42}, |
| 567 |
expr: "x.plus(2.0)", |
| 568 |
iss: errors.New("no matching overload"), |
| 569 |
}, |
| 570 |
}, |
| 571 |
}, |
| 572 |
{ |
| 573 |
name: "pure custom env", |
| 574 |
beforeOpts: []EnvOption{func(*Env) (*Env, error) { |
| 575 |
return NewCustomEnv() |
| 576 |
}}, |
| 577 |
conf: env.NewConfig("pure custom env").SetStdLib( |
| 578 |
env.NewLibrarySubset().AddIncludedFunctions([]*env.Function{{Name: "_==_"}}...), |
| 579 |
), |
| 580 |
exprs: []exprCase{ |
| 581 |
{ |
| 582 |
name: "equals", |
| 583 |
expr: "'hello world' == 'hello'", |
| 584 |
out: types.False, |
| 585 |
}, |
| 586 |
{ |
| 587 |
name: "not equals - invalid", |
| 588 |
expr: "'hello world' != 'hello'", |
| 589 |
iss: errors.New("undeclared reference"), |
| 590 |
}, |
| 591 |
}, |
| 592 |
}, |
| 593 |
{ |
| 594 |
name: "std env - allow subset", |
| 595 |
conf: env.NewConfig("std env - allow subset").SetStdLib( |
| 596 |
env.NewLibrarySubset().AddIncludedFunctions([]*env.Function{{Name: "_==_"}}...), |
| 597 |
), |
| 598 |
exprs: []exprCase{ |
| 599 |
{ |
| 600 |
name: "equals", |
| 601 |
expr: "'hello world' == 'hello'", |
| 602 |
out: types.False, |
| 603 |
}, |
| 604 |
{ |
| 605 |
name: "not equals - invalid", |
| 606 |
expr: "'hello world' != 'hello'", |
| 607 |
iss: errors.New("undeclared reference"), |
| 608 |
}, |
| 609 |
}, |
| 610 |
}, |
| 611 |
{ |
| 612 |
name: "std env - deny subset", |
| 613 |
conf: env.NewConfig("std env - deny subset").SetStdLib( |
| 614 |
env.NewLibrarySubset().AddExcludedFunctions([]*env.Function{{Name: "size"}}...), |
| 615 |
), |
| 616 |
exprs: []exprCase{ |
| 617 |
{ |
| 618 |
name: "size - invalid", |
| 619 |
expr: "'hello world'.size()", |
| 620 |
iss: errors.New("undeclared reference"), |
| 621 |
}, |
| 622 |
{ |
| 623 |
name: "equals", |
| 624 |
expr: "'hello world' == 'hello'", |
| 625 |
out: types.False, |
| 626 |
}, |
| 627 |
}, |
| 628 |
}, |
| 629 |
{ |
| 630 |
name: "extensions", |
| 631 |
conf: env.NewConfig("extensions"). |
| 632 |
AddVariables( |
| 633 |
env.NewVariable("m", |
| 634 |
env.NewTypeDesc("map", env.NewTypeDesc("string"), env.NewTypeDesc("string")))). |
| 635 |
AddExtensions(env.NewExtension("optional", math.MaxUint32)), |
| 636 |
exprs: []exprCase{ |
| 637 |
{ |
| 638 |
name: "optional none", |
| 639 |
expr: "optional.none()", |
| 640 |
out: types.OptionalNone, |
| 641 |
}, |
| 642 |
{ |
| 643 |
name: "optional key", |
| 644 |
expr: "m.?key.hasValue()", |
| 645 |
in: map[string]any{"m": map[string]string{"key": "value"}}, |
| 646 |
out: types.True, |
| 647 |
}, |
| 648 |
}, |
| 649 |
}, |
| 650 |
{ |
| 651 |
name: "extensions - config factory", |
| 652 |
conf: env.NewConfig("extensions"). |
| 653 |
AddExtensions(env.NewExtension("plus", math.MaxUint32)), |
| 654 |
confHandlers: []ConfigOptionFactory{ |
| 655 |
func(a any) (EnvOption, bool) { |
| 656 |
ext, ok := a.(*env.Extension) |
| 657 |
if !ok || ext.Name != "plus" { |
| 658 |
return nil, false |
| 659 |
} |
| 660 |
return Function("plus", Overload("plus_int_int", []*Type{IntType, IntType}, IntType, |
| 661 |
decls.BinaryBinding(func(lhs, rhs ref.Val) ref.Val { |
| 662 |
l := lhs.(types.Int) |
| 663 |
r := rhs.(types.Int) |
| 664 |
return l + r |
| 665 |
}))), true |
| 666 |
}, |
| 667 |
}, |
| 668 |
exprs: []exprCase{ |
| 669 |
{ |
| 670 |
name: "plus", |
| 671 |
expr: "plus(1, 2)", |
| 672 |
out: types.Int(3), |
| 673 |
}, |
| 674 |
}, |
| 675 |
}, |
| 676 |
{ |
| 677 |
name: "features", |
| 678 |
conf: env.NewConfig("features"). |
| 679 |
AddVariables( |
| 680 |
env.NewVariable("m", |
| 681 |
env.NewTypeDesc("map", env.NewTypeDesc("string"), env.NewTypeDesc("string")))). |
| 682 |
AddFeatures( |
| 683 |
env.NewFeature("cel.feature.backtick_escape_syntax", true), |
| 684 |
env.NewFeature("cel.feature.unknown_feature_name", true)), |
| 685 |
exprs: []exprCase{ |
| 686 |
{ |
| 687 |
name: "optional key", |
| 688 |
expr: "m.`key-name` == 'value'", |
| 689 |
in: map[string]any{"m": map[string]string{"key-name": "value"}}, |
| 690 |
out: types.True, |
| 691 |
}, |
| 692 |
}, |
| 693 |
}, |
| 694 |
{ |
| 695 |
name: "validators", |
| 696 |
conf: env.NewConfig("validators"). |
| 697 |
AddVariables( |
| 698 |
env.NewVariable("m", |
| 699 |
env.NewTypeDesc("map", env.NewTypeDesc("string"), env.NewTypeDesc("string"))), |
| 700 |
). |
| 701 |
AddValidators( |
| 702 |
env.NewValidator(durationValidatorName), |
| 703 |
env.NewValidator(timestampValidatorName), |
| 704 |
env.NewValidator(regexValidatorName), |
| 705 |
env.NewValidator(homogeneousValidatorName), |
| 706 |
env.NewValidator(nestingLimitValidatorName).SetConfig(map[string]any{"limit": 0}), |
| 707 |
), |
| 708 |
exprs: []exprCase{ |
| 709 |
{ |
| 710 |
name: "bad duration", |
| 711 |
expr: "duration('1')", |
| 712 |
iss: errors.New("invalid duration"), |
| 713 |
}, |
| 714 |
{ |
| 715 |
name: "bad timestamp", |
| 716 |
expr: "timestamp('1')", |
| 717 |
iss: errors.New("invalid timestamp"), |
| 718 |
}, |
| 719 |
{ |
| 720 |
name: "bad regex", |
| 721 |
expr: "'hello'.matches('?^()')", |
| 722 |
iss: errors.New("invalid matches"), |
| 723 |
}, |
| 724 |
{ |
| 725 |
name: "mixed type list", |
| 726 |
expr: "[1, 2.0]", |
| 727 |
iss: errors.New("expected type 'int'"), |
| 728 |
}, |
| 729 |
{ |
| 730 |
name: "disabled comprehension", |
| 731 |
expr: "[1, 2].exists(x, x % 2 == 0)", |
| 732 |
iss: errors.New("comprehension exceeds nesting limit"), |
| 733 |
}, |
| 734 |
}, |
| 735 |
}, |
| 736 |
{ |
| 737 |
name: "validators - config factory", |
| 738 |
conf: env.NewConfig("validators"). |
| 739 |
AddValidators( |
| 740 |
env.NewValidator("cel.validators.return_type").SetConfig(map[string]any{"type_name": "string"}), |
| 741 |
), |
| 742 |
confHandlers: []ConfigOptionFactory{ |
| 743 |
func(a any) (EnvOption, bool) { |
| 744 |
val, ok := a.(*env.Validator) |
| 745 |
if !ok || val.Name != "cel.validators.return_type" { |
| 746 |
return nil, false |
| 747 |
} |
| 748 |
typeName, found := val.ConfigValue("type_name") |
| 749 |
if !found { |
| 750 |
return func(*Env) (*Env, error) { |
| 751 |
return nil, fmt.Errorf("invalid validator: %s missing config parameter 'type_name'", val.Name) |
| 752 |
}, true |
| 753 |
} |
| 754 |
return func(e *Env) (*Env, error) { |
| 755 |
t, err := env.NewTypeDesc(typeName.(string)).AsCELType(e.CELTypeProvider()) |
| 756 |
if err != nil { |
| 757 |
return nil, err |
| 758 |
} |
| 759 |
return ASTValidators(returnTypeValidator{returnType: t})(e) |
| 760 |
}, true |
| 761 |
}, |
| 762 |
}, |
| 763 |
exprs: []exprCase{ |
| 764 |
{ |
| 765 |
name: "string - ok", |
| 766 |
expr: "'hello'", |
| 767 |
out: types.String("hello"), |
| 768 |
}, |
| 769 |
{ |
| 770 |
name: "int - error", |
| 771 |
expr: "1", |
| 772 |
iss: errors.New("unsupported return type: int, want string"), |
| 773 |
}, |
| 774 |
}, |
| 775 |
}, |
| 776 |
} |
| 777 |
for _, tst := range tests { |
| 778 |
tc := tst |
| 779 |
t.Run(tc.name, func(t *testing.T) { |
| 780 |
opts := tc.beforeOpts |
| 781 |
opts = append(opts, FromConfig(tc.conf, tc.confHandlers...)) |
| 782 |
opts = append(opts, tc.afterOpts...) |
| 783 |
var e *Env |
| 784 |
var err error |
| 785 |
if tc.conf.StdLib != nil { |
| 786 |
e, err = NewCustomEnv(opts...) |
| 787 |
} else { |
| 788 |
e, err = NewEnv(opts...) |
| 789 |
} |
| 790 |
if err != nil { |
| 791 |
t.Fatalf("NewEnv(FromConfig()) failed: %v", err) |
| 792 |
} |
| 793 |
for _, ex := range tc.exprs { |
| 794 |
t.Run(ex.name, func(t *testing.T) { |
| 795 |
ast, iss := e.Compile(ex.expr) |
| 796 |
if iss.Err() != nil { |
| 797 |
if ex.iss == nil || !strings.Contains(iss.Err().Error(), ex.iss.Error()) { |
| 798 |
t.Errorf("e.Compile() failed with %v, wanted %v", iss.Err(), ex.iss) |
| 799 |
} |
| 800 |
return |
| 801 |
} |
| 802 |
if ex.iss != nil { |
| 803 |
t.Fatalf("e.Compile() succeeded, wanted error %v", ex.iss) |
| 804 |
} |
| 805 |
prg, err := e.Program(ast) |
| 806 |
if err != nil { |
| 807 |
t.Fatalf("e.Program() failed: %v", err) |
| 808 |
} |
| 809 |
var in any = map[string]any{} |
| 810 |
if ex.in != nil { |
| 811 |
in = ex.in |
| 812 |
} |
| 813 |
out, _, err := prg.Eval(in) |
| 814 |
if err != nil { |
| 815 |
t.Fatalf("prg.Eval() failed: %v", err) |
| 816 |
} |
| 817 |
if out.Equal(ex.out) != types.True { |
| 818 |
t.Errorf("prg.Eval() got %v, wanted %v", out, ex.out) |
| 819 |
} |
| 820 |
}) |
| 821 |
} |
| 822 |
}) |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
func TestEnvFromConfigErrors(t *testing.T) { |
| 827 |
tests := []struct { |
| 828 |
name string |
| 829 |
conf *env.Config |
| 830 |
want error |
| 831 |
}{ |
| 832 |
{ |
| 833 |
name: "bad container", |
| 834 |
conf: env.NewConfig("bad container").SetContainer(".hello.world"), |
| 835 |
want: errors.New("container name must not contain"), |
| 836 |
}, |
| 837 |
{ |
| 838 |
name: "colliding imports", |
| 839 |
conf: env.NewConfig("colliding imports").AddImports(env.NewImport("pkg.ImportName"), env.NewImport("pkg2.ImportName")), |
| 840 |
want: errors.New("abbreviation collides"), |
| 841 |
}, |
| 842 |
{ |
| 843 |
name: "invalid subset", |
| 844 |
conf: env.NewConfig("invalid subset").SetStdLib(env.NewLibrarySubset().SetDisableMacros(true)), |
| 845 |
want: errors.New("invalid subset"), |
| 846 |
}, |
| 847 |
{ |
| 848 |
name: "invalid import", |
| 849 |
conf: env.NewConfig("invalid import").AddImports(env.NewImport("")), |
| 850 |
want: errors.New("invalid import"), |
| 851 |
}, |
| 852 |
{ |
| 853 |
name: "invalid context proto", |
| 854 |
conf: env.NewConfig("invalid context proto").SetContextVariable(env.NewContextVariable("invalid")), |
| 855 |
want: errors.New("invalid context proto type"), |
| 856 |
}, |
| 857 |
{ |
| 858 |
name: "undefined variable type", |
| 859 |
conf: env.NewConfig("undefined variable type").AddVariables(env.NewVariable("undef", env.NewTypeDesc("undefined"))), |
| 860 |
want: errors.New("invalid variable"), |
| 861 |
}, |
| 862 |
{ |
| 863 |
name: "undefined function type", |
| 864 |
conf: env.NewConfig("undefined function type").AddFunctions(env.NewFunction("invalid", env.NewOverload("invalid", []*env.TypeDesc{}, env.NewTypeDesc("undefined")))), |
| 865 |
want: errors.New("invalid function"), |
| 866 |
}, |
| 867 |
{ |
| 868 |
name: "unrecognized extension", |
| 869 |
conf: env.NewConfig("unrecognized extension"). |
| 870 |
AddExtensions(env.NewExtension("unrecognized", math.MaxUint32)), |
| 871 |
want: errors.New("unrecognized extension"), |
| 872 |
}, |
| 873 |
{ |
| 874 |
name: "invalid validator config", |
| 875 |
conf: env.NewConfig("invalid validator config"). |
| 876 |
AddValidators(env.NewValidator("cel.validator.comprehension_nesting_limit")), |
| 877 |
want: errors.New("invalid validator"), |
| 878 |
}, |
| 879 |
{ |
| 880 |
name: "invalid validator config type - unsupported type", |
| 881 |
conf: env.NewConfig("invalid validator config"). |
| 882 |
AddValidators(env.NewValidator("cel.validator.comprehension_nesting_limit").SetConfig(map[string]any{"limit": "2"})), |
| 883 |
want: errors.New("invalid validator"), |
| 884 |
}, |
| 885 |
{ |
| 886 |
name: "invalid validator config type - fractional", |
| 887 |
conf: env.NewConfig("invalid validator config"). |
| 888 |
AddValidators(env.NewValidator("cel.validator.comprehension_nesting_limit").SetConfig(map[string]any{"limit": 2.5})), |
| 889 |
want: errors.New("invalid validator: cel.validator.comprehension_nesting_limit, limit value is not a whole number: 2.5"), |
| 890 |
}, |
| 891 |
} |
| 892 |
for _, tst := range tests { |
| 893 |
tc := tst |
| 894 |
t.Run(tc.name, func(t *testing.T) { |
| 895 |
_, err := NewEnv(FromConfig(tc.conf)) |
| 896 |
if err == nil || !strings.Contains(err.Error(), tc.want.Error()) { |
| 897 |
t.Fatalf("NewEnv(FromConfig()) got %v, wanted error containing %v", err, tc.want) |
| 898 |
} |
| 899 |
}) |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
func BenchmarkNewCustomEnvLazy(b *testing.B) { |
| 904 |
b.ResetTimer() |
| 905 |
for i := 0; i < b.N; i++ { |
| 906 |
_, err := NewCustomEnv(StdLib(), EagerlyValidateDeclarations(false)) |
| 907 |
if err != nil { |
| 908 |
b.Fatalf("NewCustomEnv() failed: %v", err) |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
func BenchmarkNewCustomEnvEager(b *testing.B) { |
| 914 |
for i := 0; i < b.N; i++ { |
| 915 |
_, err := NewCustomEnv(StdLib(), EagerlyValidateDeclarations(true)) |
| 916 |
if err != nil { |
| 917 |
b.Fatalf("NewCustomEnv() failed: %v", err) |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
func BenchmarkNewEnvLazy(b *testing.B) { |
| 923 |
for i := 0; i < b.N; i++ { |
| 924 |
_, err := NewEnv() |
| 925 |
if err != nil { |
| 926 |
b.Fatalf("NewEnv() failed: %v", err) |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
func BenchmarkNewEnvEager(b *testing.B) { |
| 932 |
for i := 0; i < b.N; i++ { |
| 933 |
env, err := NewEnv() |
| 934 |
if err != nil { |
| 935 |
b.Fatalf("NewEnv() failed: %v", err) |
| 936 |
} |
| 937 |
_, iss := env.Compile("123") |
| 938 |
if iss.Err() != nil { |
| 939 |
b.Fatalf("env.Compile(123) failed: %v", iss.Err()) |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
func BenchmarkEnvExtendEager(b *testing.B) { |
| 945 |
env, err := NewEnv() |
| 946 |
if err != nil { |
| 947 |
b.Fatalf("NewEnv() failed: %v", err) |
| 948 |
} |
| 949 |
for i := 0; i < b.N; i++ { |
| 950 |
ext, err := env.Extend() |
| 951 |
if err != nil { |
| 952 |
b.Fatalf("env.Extend() failed: %v", err) |
| 953 |
} |
| 954 |
_, iss := ext.Compile("123") |
| 955 |
if iss.Err() != nil { |
| 956 |
b.Fatalf("env.Compile(123) failed: %v", iss.Err()) |
| 957 |
} |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
func BenchmarkEnvExtendEagerTypes(b *testing.B) { |
| 962 |
env, err := NewEnv(EagerlyValidateDeclarations(true)) |
| 963 |
if err != nil { |
| 964 |
b.Fatalf("NewEnv() failed: %v", err) |
| 965 |
} |
| 966 |
for i := 0; i < b.N; i++ { |
| 967 |
ext, err := env.Extend(CustomTypeProvider(types.NewEmptyRegistry())) |
| 968 |
if err != nil { |
| 969 |
b.Fatalf("env.Extend() failed: %v", err) |
| 970 |
} |
| 971 |
_, iss := ext.Compile("123") |
| 972 |
if iss.Err() != nil { |
| 973 |
b.Fatalf("env.Compile(123) failed: %v", iss.Err()) |
| 974 |
} |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
func BenchmarkEnvExtendEagerDecls(b *testing.B) { |
| 979 |
env, err := NewEnv(EagerlyValidateDeclarations(true)) |
| 980 |
if err != nil { |
| 981 |
b.Fatalf("NewEnv() failed: %v", err) |
| 982 |
} |
| 983 |
for i := 0; i < b.N; i++ { |
| 984 |
ext, err := env.Extend( |
| 985 |
Variable("test_var", StringType), |
| 986 |
Function( |
| 987 |
operators.In, |
| 988 |
Overload("string_in_string", []*Type{StringType, StringType}, BoolType), |
| 989 |
), |
| 990 |
) |
| 991 |
if err != nil { |
| 992 |
b.Fatalf("env.Extend() failed: %v", err) |
| 993 |
} |
| 994 |
_, iss := ext.Compile("123") |
| 995 |
if iss.Err() != nil { |
| 996 |
b.Fatalf("env.Compile(123) failed: %v", iss.Err()) |
| 997 |
} |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
func mustContextProto(t *testing.T, pb proto.Message) Activation { |
| 1002 |
t.Helper() |
| 1003 |
ctx, err := ContextProtoVars(pb) |
| 1004 |
if err != nil { |
| 1005 |
t.Fatalf("ContextProtoVars() failed: %v", err) |
| 1006 |
} |
| 1007 |
return ctx |
| 1008 |
} |
| 1009 |
|
| 1010 |
type returnTypeValidator struct { |
| 1011 |
returnType *Type |
| 1012 |
} |
| 1013 |
|
| 1014 |
func (returnTypeValidator) Name() string { |
| 1015 |
return "cel.validators.return_type" |
| 1016 |
} |
| 1017 |
|
| 1018 |
func (v returnTypeValidator) Validate(_ *Env, c ValidatorConfig, a *ast.AST, iss *Issues) { |
| 1019 |
if a.GetType(a.Expr().ID()) != v.returnType { |
| 1020 |
iss.ReportErrorAtID(a.Expr().ID(), |
| 1021 |
"unsupported return type: %s, want %s", |
| 1022 |
a.GetType(a.Expr().ID()), v.returnType.TypeName()) |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
func (v returnTypeValidator) ToConfig() *env.Validator { |
| 1027 |
return env.NewValidator(v.Name()).SetConfig(map[string]any{"type_name": v.returnType.TypeName()}) |
| 1028 |
} |
| 1029 |
|
| 1030 |
type customLegacyProvider struct { |
| 1031 |
provider ref.TypeProvider |
| 1032 |
} |
| 1033 |
|
| 1034 |
func (p *customLegacyProvider) EnumValue(enumName string) ref.Val { |
| 1035 |
return p.provider.EnumValue(enumName) |
| 1036 |
} |
| 1037 |
|
| 1038 |
func (p *customLegacyProvider) FindIdent(identName string) (ref.Val, bool) { |
| 1039 |
return p.provider.FindIdent(identName) |
| 1040 |
} |
| 1041 |
|
| 1042 |
func (p *customLegacyProvider) FindType(typeName string) (*exprpb.Type, bool) { |
| 1043 |
return p.provider.FindType(typeName) |
| 1044 |
} |
| 1045 |
|
| 1046 |
func (p *customLegacyProvider) FindFieldType(structType, fieldName string) (*ref.FieldType, bool) { |
| 1047 |
return p.provider.FindFieldType(structType, fieldName) |
| 1048 |
} |
| 1049 |
|
| 1050 |
func (p *customLegacyProvider) NewValue(structType string, fields map[string]ref.Val) ref.Val { |
| 1051 |
return p.provider.NewValue(structType, fields) |
| 1052 |
} |
| 1053 |
|
| 1054 |
type customCELProvider struct { |
| 1055 |
provider types.Provider |
| 1056 |
} |
| 1057 |
|
| 1058 |
func (p *customCELProvider) EnumValue(enumName string) ref.Val { |
| 1059 |
return p.provider.EnumValue(enumName) |
| 1060 |
} |
| 1061 |
|
| 1062 |
func (p *customCELProvider) FindIdent(identName string) (ref.Val, bool) { |
| 1063 |
return p.provider.FindIdent(identName) |
| 1064 |
} |
| 1065 |
|
| 1066 |
func (p *customCELProvider) FindStructType(typeName string) (*types.Type, bool) { |
| 1067 |
return p.provider.FindStructType(typeName) |
| 1068 |
} |
| 1069 |
|
| 1070 |
func (p *customCELProvider) FindStructFieldNames(typeName string) ([]string, bool) { |
| 1071 |
return p.provider.FindStructFieldNames(typeName) |
| 1072 |
} |
| 1073 |
|
| 1074 |
func (p *customCELProvider) FindStructFieldType(structType, fieldName string) (*types.FieldType, bool) { |
| 1075 |
return p.provider.FindStructFieldType(structType, fieldName) |
| 1076 |
} |
| 1077 |
|
| 1078 |
func (p *customCELProvider) NewValue(structType string, fields map[string]ref.Val) ref.Val { |
| 1079 |
return p.provider.NewValue(structType, fields) |
| 1080 |
} |