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

Function pruneOptionalMapEntries

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

Source from the content-addressed store, hash-verified

392}
393
394func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) {
395 m := e.AsMap()
396 entries := m.Entries()
397 updatedEntries := []ast.EntryExpr{}
398 modified := false
399 for _, e := range entries {
400 entry := e.AsMapEntry()
401 key := entry.Key()
402 val := entry.Value()
403 // If the entry is not optional, or the value-side of the optional hasn't
404 // been resolved to a literal, then preserve the entry as-is.
405 if !entry.IsOptional() || val.Kind() != ast.LiteralKind {
406 updatedEntries = append(updatedEntries, e)
407 continue
408 }
409 optElemVal, ok := val.AsLiteral().(*types.Optional)
410 if !ok {
411 updatedEntries = append(updatedEntries, e)
412 continue
413 }
414 // When the key is not a literal, but the value is, then it needs to be
415 // restored to an optional value.
416 if key.Kind() != ast.LiteralKind {
417 undoOptVal, err := adaptLiteral(ctx, optElemVal)
418 if err != nil {
419 ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err)
420 }
421 ctx.UpdateExpr(val, undoOptVal)
422 updatedEntries = append(updatedEntries, e)
423 continue
424 }
425 modified = true
426 if !optElemVal.HasValue() {
427 continue
428 }
429 ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue()))
430 updatedEntry := ctx.NewMapEntry(key, val, false)
431 updatedEntries = append(updatedEntries, updatedEntry)
432 }
433 if modified {
434 ctx.UpdateExpr(e, ctx.NewMap(updatedEntries))
435 }
436}
437
438func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) {
439 s := e.AsStruct()

Callers 1

pruneOptionalElementsFunction · 0.85

Calls 15

adaptLiteralFunction · 0.85
UpdateExprMethod · 0.80
HasValueMethod · 0.80
GetValueMethod · 0.80
AsMapMethod · 0.65
EntriesMethod · 0.65
AsMapEntryMethod · 0.65
KeyMethod · 0.65
ValueMethod · 0.65
IsOptionalMethod · 0.65
KindMethod · 0.65
AsLiteralMethod · 0.65

Tested by

no test coverage detected