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

Function pruneOptionalMapEntries

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

Source from the content-addressed store, hash-verified

374}
375
376func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) {
377 m := e.AsMap()
378 entries := m.Entries()
379 updatedEntries := []ast.EntryExpr{}
380 modified := false
381 for _, e := range entries {
382 entry := e.AsMapEntry()
383 key := entry.Key()
384 val := entry.Value()
385 // If the entry is not optional, or the value-side of the optional hasn't
386 // been resolved to a literal, then preserve the entry as-is.
387 if !entry.IsOptional() || val.Kind() != ast.LiteralKind {
388 updatedEntries = append(updatedEntries, e)
389 continue
390 }
391 optElemVal, ok := val.AsLiteral().(*types.Optional)
392 if !ok {
393 updatedEntries = append(updatedEntries, e)
394 continue
395 }
396 // When the key is not a literal, but the value is, then it needs to be
397 // restored to an optional value.
398 if key.Kind() != ast.LiteralKind {
399 undoOptVal, err := adaptLiteral(ctx, optElemVal)
400 if err != nil {
401 ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err)
402 }
403 ctx.UpdateExpr(val, undoOptVal)
404 updatedEntries = append(updatedEntries, e)
405 continue
406 }
407 modified = true
408 if !optElemVal.HasValue() {
409 continue
410 }
411 ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue()))
412 updatedEntry := ctx.NewMapEntry(key, val, false)
413 updatedEntries = append(updatedEntries, updatedEntry)
414 }
415 if modified {
416 ctx.UpdateExpr(e, ctx.NewMap(updatedEntries))
417 }
418}
419
420func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) {
421 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