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

Function pruneOptionalMapEntries

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

Source from the content-addressed store, hash-verified

354}
355
356func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) {
357 m := e.AsMap()
358 entries := m.Entries()
359 updatedEntries := []ast.EntryExpr{}
360 modified := false
361 for _, e := range entries {
362 entry := e.AsMapEntry()
363 key := entry.Key()
364 val := entry.Value()
365 // If the entry is not optional, or the value-side of the optional hasn't
366 // been resolved to a literal, then preserve the entry as-is.
367 if !entry.IsOptional() || val.Kind() != ast.LiteralKind {
368 updatedEntries = append(updatedEntries, e)
369 continue
370 }
371 optElemVal, ok := val.AsLiteral().(*types.Optional)
372 if !ok {
373 updatedEntries = append(updatedEntries, e)
374 continue
375 }
376 // When the key is not a literal, but the value is, then it needs to be
377 // restored to an optional value.
378 if key.Kind() != ast.LiteralKind {
379 undoOptVal, err := adaptLiteral(ctx, optElemVal)
380 if err != nil {
381 ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err)
382 }
383 ctx.UpdateExpr(val, undoOptVal)
384 updatedEntries = append(updatedEntries, e)
385 continue
386 }
387 modified = true
388 if !optElemVal.HasValue() {
389 continue
390 }
391 ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue()))
392 updatedEntry := ctx.NewMapEntry(key, val, false)
393 updatedEntries = append(updatedEntries, updatedEntry)
394 }
395 if modified {
396 ctx.UpdateExpr(e, ctx.NewMap(updatedEntries))
397 }
398}
399
400func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) {
401 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