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

Function pruneOptionalMapEntries

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

Source from the content-addressed store, hash-verified

313}
314
315func pruneOptionalMapEntries(ctx *OptimizerContext, e ast.Expr) {
316 m := e.AsMap()
317 entries := m.Entries()
318 updatedEntries := []ast.EntryExpr{}
319 modified := false
320 for _, e := range entries {
321 entry := e.AsMapEntry()
322 key := entry.Key()
323 val := entry.Value()
324 // If the entry is not optional, or the value-side of the optional hasn't
325 // been resolved to a literal, then preserve the entry as-is.
326 if !entry.IsOptional() || val.Kind() != ast.LiteralKind {
327 updatedEntries = append(updatedEntries, e)
328 continue
329 }
330 optElemVal, ok := val.AsLiteral().(*types.Optional)
331 if !ok {
332 updatedEntries = append(updatedEntries, e)
333 continue
334 }
335 // When the key is not a literal, but the value is, then it needs to be
336 // restored to an optional value.
337 if key.Kind() != ast.LiteralKind {
338 undoOptVal, err := adaptLiteral(ctx, optElemVal)
339 if err != nil {
340 ctx.ReportErrorAtID(val.ID(), "invalid map value literal %v: %v", optElemVal, err)
341 }
342 ctx.UpdateExpr(val, undoOptVal)
343 updatedEntries = append(updatedEntries, e)
344 continue
345 }
346 modified = true
347 if !optElemVal.HasValue() {
348 continue
349 }
350 ctx.UpdateExpr(val, ctx.NewLiteral(optElemVal.GetValue()))
351 updatedEntry := ctx.NewMapEntry(key, val, false)
352 updatedEntries = append(updatedEntries, updatedEntry)
353 }
354 if modified {
355 ctx.UpdateExpr(e, ctx.NewMap(updatedEntries))
356 }
357}
358
359func pruneOptionalStructFields(ctx *OptimizerContext, e ast.Expr) {
360 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