| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cel |
| 16 |
|
| 17 |
import ( |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
|
| 21 |
"google.golang.org/protobuf/proto" |
| 22 |
"google.golang.org/protobuf/reflect/protodesc" |
| 23 |
"google.golang.org/protobuf/reflect/protoreflect" |
| 24 |
"google.golang.org/protobuf/reflect/protoregistry" |
| 25 |
"google.golang.org/protobuf/types/dynamicpb" |
| 26 |
|
| 27 |
"github.com/google/cel-go/checker" |
| 28 |
"github.com/google/cel-go/common/containers" |
| 29 |
"github.com/google/cel-go/common/decls" |
| 30 |
"github.com/google/cel-go/common/env" |
| 31 |
"github.com/google/cel-go/common/functions" |
| 32 |
"github.com/google/cel-go/common/types" |
| 33 |
"github.com/google/cel-go/common/types/pb" |
| 34 |
"github.com/google/cel-go/common/types/ref" |
| 35 |
"github.com/google/cel-go/interpreter" |
| 36 |
"github.com/google/cel-go/parser" |
| 37 |
|
| 38 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 39 |
descpb "google.golang.org/protobuf/types/descriptorpb" |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
const ( |
| 46 |
_ = iota |
| 47 |
|
| 48 |
|
| 49 |
featureEnableMacroCallTracking |
| 50 |
|
| 51 |
|
| 52 |
featureCrossTypeNumericComparisons |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
featureEagerlyValidateDeclarations |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
featureDefaultUTCTimeZone |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
featureVariadicLogicalASTs |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
featureEnableErrorOnBadPresenceTest |
| 71 |
|
| 72 |
|
| 73 |
featureIdentEscapeSyntax |
| 74 |
) |
| 75 |
|
| 76 |
var featureIDsToNames = map[int]string{ |
| 77 |
featureEnableMacroCallTracking: "cel.feature.macro_call_tracking", |
| 78 |
featureCrossTypeNumericComparisons: "cel.feature.cross_type_numeric_comparisons", |
| 79 |
featureIdentEscapeSyntax: "cel.feature.backtick_escape_syntax", |
| 80 |
} |
| 81 |
|
| 82 |
func featureNameByID(id int) (string, bool) { |
| 83 |
name, found := featureIDsToNames[id] |
| 84 |
return name, found |
| 85 |
} |
| 86 |
|
| 87 |
func featureIDByName(name string) (int, bool) { |
| 88 |
for id, n := range featureIDsToNames { |
| 89 |
if n == name { |
| 90 |
return id, true |
| 91 |
} |
| 92 |
} |
| 93 |
return 0, false |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
type EnvOption func(e *Env) (*Env, error) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
func ClearMacros() EnvOption { |
| 104 |
return func(e *Env) (*Env, error) { |
| 105 |
e.macros = NoMacros |
| 106 |
return e, nil |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
func CustomTypeAdapter(adapter types.Adapter) EnvOption { |
| 114 |
return func(e *Env) (*Env, error) { |
| 115 |
e.adapter = adapter |
| 116 |
return e, nil |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func CustomTypeProvider(provider any) EnvOption { |
| 126 |
return func(e *Env) (*Env, error) { |
| 127 |
var err error |
| 128 |
e.provider, err = maybeInteropProvider(provider) |
| 129 |
return e, err |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
func Declarations(decls ...*exprpb.Decl) EnvOption { |
| 141 |
declOpts := []EnvOption{} |
| 142 |
var err error |
| 143 |
var opt EnvOption |
| 144 |
|
| 145 |
|
| 146 |
for _, d := range decls { |
| 147 |
opt, err = ExprDeclToDeclaration(d) |
| 148 |
if err != nil { |
| 149 |
break |
| 150 |
} |
| 151 |
declOpts = append(declOpts, opt) |
| 152 |
} |
| 153 |
return func(e *Env) (*Env, error) { |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
for _, o := range declOpts { |
| 158 |
e, err = o(e) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
} |
| 163 |
return e, nil |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
func EagerlyValidateDeclarations(enabled bool) EnvOption { |
| 176 |
return features(featureEagerlyValidateDeclarations, enabled) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
func HomogeneousAggregateLiterals() EnvOption { |
| 185 |
return ASTValidators(ValidateHomogeneousAggregateLiterals()) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
func variadicLogicalOperatorASTs() EnvOption { |
| 197 |
return features(featureVariadicLogicalASTs, true) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
func Macros(macros ...Macro) EnvOption { |
| 204 |
return func(e *Env) (*Env, error) { |
| 205 |
e.macros = append(e.macros, macros...) |
| 206 |
return e, nil |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
func Container(name string) EnvOption { |
| 216 |
return func(e *Env) (*Env, error) { |
| 217 |
cont, err := e.Container.Extend(containers.Name(name)) |
| 218 |
if err != nil { |
| 219 |
return nil, err |
| 220 |
} |
| 221 |
e.Container = cont |
| 222 |
return e, nil |
| 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 |
func Abbrevs(qualifiedNames ...string) EnvOption { |
| 268 |
return func(e *Env) (*Env, error) { |
| 269 |
cont, err := e.Container.Extend(containers.Abbrevs(qualifiedNames...)) |
| 270 |
if err != nil { |
| 271 |
return nil, err |
| 272 |
} |
| 273 |
e.Container = cont |
| 274 |
return e, nil |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
type customTypeRegistry interface { |
| 281 |
RegisterDescriptor(protoreflect.FileDescriptor) error |
| 282 |
RegisterType(...ref.Type) error |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
func Types(addTypes ...any) EnvOption { |
| 296 |
return func(e *Env) (*Env, error) { |
| 297 |
reg, isReg := e.provider.(customTypeRegistry) |
| 298 |
if !isReg { |
| 299 |
return nil, fmt.Errorf("custom types not supported by provider: %T", e.provider) |
| 300 |
} |
| 301 |
for _, t := range addTypes { |
| 302 |
switch v := t.(type) { |
| 303 |
case proto.Message: |
| 304 |
fdMap := pb.CollectFileDescriptorSet(v) |
| 305 |
for _, fd := range fdMap { |
| 306 |
err := reg.RegisterDescriptor(fd) |
| 307 |
if err != nil { |
| 308 |
return nil, err |
| 309 |
} |
| 310 |
} |
| 311 |
case ref.Type: |
| 312 |
err := reg.RegisterType(v) |
| 313 |
if err != nil { |
| 314 |
return nil, err |
| 315 |
} |
| 316 |
default: |
| 317 |
return nil, fmt.Errorf("unsupported type: %T", t) |
| 318 |
} |
| 319 |
} |
| 320 |
return e, nil |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
func TypeDescs(descs ...any) EnvOption { |
| 333 |
return func(e *Env) (*Env, error) { |
| 334 |
reg, isReg := e.provider.(customTypeRegistry) |
| 335 |
if !isReg { |
| 336 |
return nil, fmt.Errorf("custom types not supported by provider: %T", e.provider) |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
var fds *descpb.FileDescriptorSet |
| 342 |
for _, d := range descs { |
| 343 |
switch f := d.(type) { |
| 344 |
case *descpb.FileDescriptorProto: |
| 345 |
if fds == nil { |
| 346 |
fds = &descpb.FileDescriptorSet{ |
| 347 |
File: []*descpb.FileDescriptorProto{}, |
| 348 |
} |
| 349 |
} |
| 350 |
fds.File = append(fds.File, f) |
| 351 |
} |
| 352 |
} |
| 353 |
if fds != nil { |
| 354 |
if err := registerFileSet(reg, fds); err != nil { |
| 355 |
return nil, err |
| 356 |
} |
| 357 |
} |
| 358 |
for _, d := range descs { |
| 359 |
switch f := d.(type) { |
| 360 |
case *protoregistry.Files: |
| 361 |
if err := registerFiles(reg, f); err != nil { |
| 362 |
return nil, err |
| 363 |
} |
| 364 |
case protoreflect.FileDescriptor: |
| 365 |
if err := reg.RegisterDescriptor(f); err != nil { |
| 366 |
return nil, err |
| 367 |
} |
| 368 |
case *descpb.FileDescriptorSet: |
| 369 |
if err := registerFileSet(reg, f); err != nil { |
| 370 |
return nil, err |
| 371 |
} |
| 372 |
case *descpb.FileDescriptorProto: |
| 373 |
|
| 374 |
default: |
| 375 |
return nil, fmt.Errorf("unsupported type descriptor: %T", d) |
| 376 |
} |
| 377 |
} |
| 378 |
return e, nil |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func registerFileSet(reg customTypeRegistry, fileSet *descpb.FileDescriptorSet) error { |
| 383 |
files, err := protodesc.NewFiles(fileSet) |
| 384 |
if err != nil { |
| 385 |
return fmt.Errorf("protodesc.NewFiles(%v) failed: %v", fileSet, err) |
| 386 |
} |
| 387 |
return registerFiles(reg, files) |
| 388 |
} |
| 389 |
|
| 390 |
func registerFiles(reg customTypeRegistry, files *protoregistry.Files) error { |
| 391 |
var err error |
| 392 |
files.RangeFiles(func(fd protoreflect.FileDescriptor) bool { |
| 393 |
err = reg.RegisterDescriptor(fd) |
| 394 |
return err == nil |
| 395 |
}) |
| 396 |
return err |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
type ProgramOption func(p *prog) (*prog, error) |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
func CustomDecorator(dec interpreter.InterpretableDecorator) ProgramOption { |
| 406 |
return func(p *prog) (*prog, error) { |
| 407 |
p.plannerOptions = append(p.plannerOptions, interpreter.CustomDecorator(dec)) |
| 408 |
return p, nil |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
func Functions(funcs ...*functions.Overload) ProgramOption { |
| 417 |
return func(p *prog) (*prog, error) { |
| 418 |
if err := p.dispatcher.Add(funcs...); err != nil { |
| 419 |
return nil, err |
| 420 |
} |
| 421 |
return p, nil |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
func Globals(vars any) ProgramOption { |
| 431 |
return func(p *prog) (*prog, error) { |
| 432 |
defaultVars, err := NewActivation(vars) |
| 433 |
if err != nil { |
| 434 |
return nil, err |
| 435 |
} |
| 436 |
if p.defaultVars != nil { |
| 437 |
defaultVars = interpreter.NewHierarchicalActivation(p.defaultVars, defaultVars) |
| 438 |
} |
| 439 |
p.defaultVars = defaultVars |
| 440 |
return p, nil |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
func OptimizeRegex(regexOptimizations ...*interpreter.RegexOptimization) ProgramOption { |
| 448 |
return func(p *prog) (*prog, error) { |
| 449 |
p.regexOptimizations = append(p.regexOptimizations, regexOptimizations...) |
| 450 |
return p, nil |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
type ConfigOptionFactory func(any) (EnvOption, bool) |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
func FromConfig(config *env.Config, optFactories ...ConfigOptionFactory) EnvOption { |
| 479 |
return func(e *Env) (*Env, error) { |
| 480 |
if err := config.Validate(); err != nil { |
| 481 |
return nil, err |
| 482 |
} |
| 483 |
opts, err := configToEnvOptions(config, e.CELTypeProvider(), optFactories) |
| 484 |
if err != nil { |
| 485 |
return nil, err |
| 486 |
} |
| 487 |
for _, o := range opts { |
| 488 |
e, err = o(e) |
| 489 |
if err != nil { |
| 490 |
return nil, err |
| 491 |
} |
| 492 |
} |
| 493 |
return e, nil |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
func configToEnvOptions(config *env.Config, provider types.Provider, optFactories []ConfigOptionFactory) ([]EnvOption, error) { |
| 500 |
envOpts := []EnvOption{} |
| 501 |
|
| 502 |
if config.StdLib != nil { |
| 503 |
envOpts = append(envOpts, func(e *Env) (*Env, error) { |
| 504 |
if e.HasLibrary("cel.lib.std") { |
| 505 |
return nil, errors.New("invalid subset of stdlib: create a custom env") |
| 506 |
} |
| 507 |
return e, nil |
| 508 |
}) |
| 509 |
if !config.StdLib.Disabled { |
| 510 |
envOpts = append(envOpts, StdLib(StdLibSubset(config.StdLib))) |
| 511 |
} |
| 512 |
} else { |
| 513 |
envOpts = append(envOpts, StdLib()) |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
if config.Container != "" { |
| 518 |
envOpts = append(envOpts, Container(config.Container)) |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
for _, imp := range config.Imports { |
| 523 |
envOpts = append(envOpts, Abbrevs(imp.Name)) |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
if config.ContextVariable != nil { |
| 528 |
typeName := config.ContextVariable.TypeName |
| 529 |
if _, found := provider.FindStructType(typeName); !found { |
| 530 |
return nil, fmt.Errorf("invalid context proto type: %q", typeName) |
| 531 |
} |
| 532 |
|
| 533 |
msg := provider.NewValue(typeName, map[string]ref.Val{}) |
| 534 |
pbMsg, ok := msg.Value().(proto.Message) |
| 535 |
if !ok { |
| 536 |
return nil, fmt.Errorf("unsupported context type: %T", msg.Value()) |
| 537 |
} |
| 538 |
envOpts = append(envOpts, DeclareContextProto(pbMsg.ProtoReflect().Descriptor())) |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
if len(config.Variables) != 0 { |
| 543 |
vars := make([]*decls.VariableDecl, 0, len(config.Variables)) |
| 544 |
for _, v := range config.Variables { |
| 545 |
vDef, err := v.AsCELVariable(provider) |
| 546 |
if err != nil { |
| 547 |
return nil, err |
| 548 |
} |
| 549 |
vars = append(vars, vDef) |
| 550 |
} |
| 551 |
envOpts = append(envOpts, VariableDecls(vars...)) |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
if len(config.Functions) != 0 { |
| 556 |
funcs := make([]*decls.FunctionDecl, 0, len(config.Functions)) |
| 557 |
for _, f := range config.Functions { |
| 558 |
fnDef, err := f.AsCELFunction(provider) |
| 559 |
if err != nil { |
| 560 |
return nil, err |
| 561 |
} |
| 562 |
funcs = append(funcs, fnDef) |
| 563 |
} |
| 564 |
envOpts = append(envOpts, FunctionDecls(funcs...)) |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
for _, feat := range config.Features { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
if id, found := featureIDByName(feat.Name); found { |
| 574 |
envOpts = append(envOpts, features(id, feat.Enabled)) |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
for _, val := range config.Validators { |
| 580 |
if fac, found := astValidatorFactories[val.Name]; found { |
| 581 |
envOpts = append(envOpts, func(e *Env) (*Env, error) { |
| 582 |
validator, err := fac(val) |
| 583 |
if err != nil { |
| 584 |
return nil, fmt.Errorf("%w", err) |
| 585 |
} |
| 586 |
return ASTValidators(validator)(e) |
| 587 |
}) |
| 588 |
} else if opt, handled := handleExtendedConfigOption(val, optFactories); handled { |
| 589 |
envOpts = append(envOpts, opt) |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
for _, ext := range config.Extensions { |
| 597 |
|
| 598 |
ver, _ := ext.VersionNumber() |
| 599 |
if ext.Name == "optional" { |
| 600 |
envOpts = append(envOpts, OptionalTypes(OptionalTypesVersion(ver))) |
| 601 |
} else { |
| 602 |
opt, handled := handleExtendedConfigOption(ext, optFactories) |
| 603 |
if !handled { |
| 604 |
return nil, fmt.Errorf("unrecognized extension: %s", ext.Name) |
| 605 |
} |
| 606 |
envOpts = append(envOpts, opt) |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
return envOpts, nil |
| 611 |
} |
| 612 |
|
| 613 |
func handleExtendedConfigOption(conf any, optFactories []ConfigOptionFactory) (EnvOption, bool) { |
| 614 |
for _, optFac := range optFactories { |
| 615 |
if opt, useOption := optFac(conf); useOption { |
| 616 |
return opt, true |
| 617 |
} |
| 618 |
} |
| 619 |
return nil, false |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
type EvalOption int |
| 625 |
|
| 626 |
const ( |
| 627 |
|
| 628 |
OptTrackState EvalOption = 1 << iota |
| 629 |
|
| 630 |
|
| 631 |
OptExhaustiveEval EvalOption = 1<<iota | OptTrackState |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
OptOptimize EvalOption = 1 << iota |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
OptPartialEval EvalOption = 1 << iota |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
OptTrackCost EvalOption = 1 << iota |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
OptCheckStringFormat EvalOption = 1 << iota |
| 656 |
) |
| 657 |
|
| 658 |
|
| 659 |
func EvalOptions(opts ...EvalOption) ProgramOption { |
| 660 |
return func(p *prog) (*prog, error) { |
| 661 |
for _, opt := range opts { |
| 662 |
p.evalOpts |= opt |
| 663 |
} |
| 664 |
return p, nil |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
func InterruptCheckFrequency(checkFrequency uint) ProgramOption { |
| 671 |
return func(p *prog) (*prog, error) { |
| 672 |
p.interruptCheckFrequency = checkFrequency |
| 673 |
return p, nil |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
func CostEstimatorOptions(costOpts ...checker.CostOption) EnvOption { |
| 679 |
return func(e *Env) (*Env, error) { |
| 680 |
e.costOptions = append(e.costOptions, costOpts...) |
| 681 |
return e, nil |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
func CostTrackerOptions(costOpts ...interpreter.CostTrackerOption) ProgramOption { |
| 689 |
return func(p *prog) (*prog, error) { |
| 690 |
p.costOptions = append(p.costOptions, costOpts...) |
| 691 |
return p, nil |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
func CostTracking(costEstimator interpreter.ActualCostEstimator) ProgramOption { |
| 697 |
return func(p *prog) (*prog, error) { |
| 698 |
p.callCostEstimator = costEstimator |
| 699 |
p.evalOpts |= OptTrackCost |
| 700 |
return p, nil |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
func CostLimit(costLimit uint64) ProgramOption { |
| 709 |
return func(p *prog) (*prog, error) { |
| 710 |
p.costLimit = &costLimit |
| 711 |
p.evalOpts |= OptTrackCost |
| 712 |
return p, nil |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
func fieldToCELType(field protoreflect.FieldDescriptor) (*Type, error) { |
| 717 |
if field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind { |
| 718 |
msgName := (string)(field.Message().FullName()) |
| 719 |
return ObjectType(msgName), nil |
| 720 |
} |
| 721 |
if primitiveType, found := types.ProtoCELPrimitives[field.Kind()]; found { |
| 722 |
return primitiveType, nil |
| 723 |
} |
| 724 |
if field.Kind() == protoreflect.EnumKind { |
| 725 |
return IntType, nil |
| 726 |
} |
| 727 |
return nil, fmt.Errorf("field %s type %s not implemented", field.FullName(), field.Kind().String()) |
| 728 |
} |
| 729 |
|
| 730 |
func fieldToVariable(field protoreflect.FieldDescriptor) (*decls.VariableDecl, error) { |
| 731 |
name := string(field.Name()) |
| 732 |
if field.IsMap() { |
| 733 |
mapKey := field.MapKey() |
| 734 |
mapValue := field.MapValue() |
| 735 |
keyType, err := fieldToCELType(mapKey) |
| 736 |
if err != nil { |
| 737 |
return nil, err |
| 738 |
} |
| 739 |
valueType, err := fieldToCELType(mapValue) |
| 740 |
if err != nil { |
| 741 |
return nil, err |
| 742 |
} |
| 743 |
return decls.NewVariable(name, MapType(keyType, valueType)), nil |
| 744 |
} |
| 745 |
if field.IsList() { |
| 746 |
elemType, err := fieldToCELType(field) |
| 747 |
if err != nil { |
| 748 |
return nil, err |
| 749 |
} |
| 750 |
return decls.NewVariable(name, ListType(elemType)), nil |
| 751 |
} |
| 752 |
celType, err := fieldToCELType(field) |
| 753 |
if err != nil { |
| 754 |
return nil, err |
| 755 |
} |
| 756 |
return decls.NewVariable(name, celType), nil |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
func DeclareContextProto(descriptor protoreflect.MessageDescriptor) EnvOption { |
| 763 |
return func(e *Env) (*Env, error) { |
| 764 |
if e.contextProto != nil { |
| 765 |
return nil, fmt.Errorf("context proto already declared as %q, got %q", |
| 766 |
e.contextProto.FullName(), descriptor.FullName()) |
| 767 |
} |
| 768 |
e.contextProto = descriptor |
| 769 |
fields := descriptor.Fields() |
| 770 |
vars := make([]*decls.VariableDecl, 0, fields.Len()) |
| 771 |
for i := 0; i < fields.Len(); i++ { |
| 772 |
field := fields.Get(i) |
| 773 |
variable, err := fieldToVariable(field) |
| 774 |
if err != nil { |
| 775 |
return nil, err |
| 776 |
} |
| 777 |
vars = append(vars, variable) |
| 778 |
} |
| 779 |
var err error |
| 780 |
e, err = VariableDecls(vars...)(e) |
| 781 |
if err != nil { |
| 782 |
return nil, err |
| 783 |
} |
| 784 |
return Types(dynamicpb.NewMessage(descriptor))(e) |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
func ContextProtoVars(ctx proto.Message) (Activation, error) { |
| 793 |
if ctx == nil || !ctx.ProtoReflect().IsValid() { |
| 794 |
return interpreter.EmptyActivation(), nil |
| 795 |
} |
| 796 |
reg, err := types.NewRegistry(ctx) |
| 797 |
if err != nil { |
| 798 |
return nil, err |
| 799 |
} |
| 800 |
pbRef := ctx.ProtoReflect() |
| 801 |
typeName := string(pbRef.Descriptor().FullName()) |
| 802 |
fields := pbRef.Descriptor().Fields() |
| 803 |
vars := make(map[string]any, fields.Len()) |
| 804 |
for i := 0; i < fields.Len(); i++ { |
| 805 |
field := fields.Get(i) |
| 806 |
sft, found := reg.FindStructFieldType(typeName, field.TextName()) |
| 807 |
if !found { |
| 808 |
return nil, fmt.Errorf("no such field: %s", field.TextName()) |
| 809 |
} |
| 810 |
fieldVal, err := sft.GetFrom(ctx) |
| 811 |
if err != nil { |
| 812 |
return nil, err |
| 813 |
} |
| 814 |
vars[field.TextName()] = fieldVal |
| 815 |
} |
| 816 |
return NewActivation(vars) |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
func EnableMacroCallTracking() EnvOption { |
| 822 |
return features(featureEnableMacroCallTracking, true) |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
func EnableIdentifierEscapeSyntax() EnvOption { |
| 828 |
return features(featureIdentEscapeSyntax, true) |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
func CrossTypeNumericComparisons(enabled bool) EnvOption { |
| 833 |
return features(featureCrossTypeNumericComparisons, enabled) |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
func DefaultUTCTimeZone(enabled bool) EnvOption { |
| 839 |
return features(featureDefaultUTCTimeZone, enabled) |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
func features(flag int, enabled bool) EnvOption { |
| 844 |
return func(e *Env) (*Env, error) { |
| 845 |
e.features[flag] = enabled |
| 846 |
return e, nil |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
func ParserRecursionLimit(limit int) EnvOption { |
| 853 |
return func(e *Env) (*Env, error) { |
| 854 |
e.prsrOpts = append(e.prsrOpts, parser.MaxRecursionDepth(limit)) |
| 855 |
return e, nil |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
func ParserExpressionSizeLimit(limit int) EnvOption { |
| 862 |
return func(e *Env) (*Env, error) { |
| 863 |
e.prsrOpts = append(e.prsrOpts, parser.ExpressionSizeCodePointLimit(limit)) |
| 864 |
return e, nil |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
func EnableHiddenAccumulatorName(enabled bool) EnvOption { |
| 871 |
return func(e *Env) (*Env, error) { |
| 872 |
e.prsrOpts = append(e.prsrOpts, parser.EnableHiddenAccumulatorName(enabled)) |
| 873 |
return e, nil |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
func maybeInteropProvider(provider any) (types.Provider, error) { |
| 878 |
switch p := provider.(type) { |
| 879 |
case types.Provider: |
| 880 |
return p, nil |
| 881 |
case ref.TypeProvider: |
| 882 |
return &interopCELTypeProvider{TypeProvider: p}, nil |
| 883 |
default: |
| 884 |
return nil, fmt.Errorf("unsupported type provider: %T", provider) |
| 885 |
} |
| 886 |
} |