MCPcopy Create free account
hub / github.com/cel-expr/cel-go / pruneOptionalStructFields

Function pruneOptionalStructFields

cel/folding.go:359–387  ·  view source on GitHub ↗
(ctx *OptimizerContext, e ast.Expr)

Source from the content-addressed store, hash-verified

357}
358
359func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) {
360 s := e.AsStruct()
361 fields := s.Fields()
362 updatedFields := []ast.EntryExpr{}
363 modified := false
364 for _, f := range fields {
365 field := f.AsStructField()
366 val := field.Value()
367 if !field.IsOptional() || val.Kind() != ast.LiteralKind {
368 updatedFields = append(updatedFields, f)
369 continue
370 }
371 optElemVal, ok := val.AsLiteral().(*types.Optional)
372 if !ok {
373 updatedFields = append(updatedFields, f)
374 continue
375 }
376 modified = true
377 if !optElemVal.HasValue() {
378 continue
379 }
380 ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue()))
381 updatedField := ctx.NewStructField(field.Name(), val, false)
382 updatedFields = append(updatedFields, updatedField)
383 }
384 if modified {
385 ctx.UpdateExpr(e, ctx.NewStruct(s.TypeName(), updatedFields))
386 }
387}
388
389// adaptLiteral converts a runtime CEL value to its equivalent literal expression.
390//

Callers 1

pruneOptionalElementsFunction · 0.85

Calls 15

HasValueMethod · 0.80
UpdateExprMethod · 0.80
GetValueMethod · 0.80
AsStructMethod · 0.65
FieldsMethod · 0.65
AsStructFieldMethod · 0.65
ValueMethod · 0.65
IsOptionalMethod · 0.65
KindMethod · 0.65
AsLiteralMethod · 0.65
NewLiteralMethod · 0.65
NewStructFieldMethod · 0.65

Tested by

no test coverage detected