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

Function MakeMap

parser/macro.go:497–526  ·  view source on GitHub ↗

MakeMap expands the input call arguments into a comprehension that transforms each element in the input to produce an output list. There are two call patterns supported by map: .map( , ) .map( , , ) In the second form only i

(eh ExprHelper, target ast.Expr, args []ast.Expr)

Source from the content-addressed store, hash-verified

495// In the second form only iterVar values which return true when provided to the predicate expression
496// are transformed.
497func MakeMap(eh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
498 v, found := extractIdent(args[0])
499 if !found {
500 return nil, eh.NewError(args[0].ID(), "argument is not an identifier")
501 }
502 accu := eh.AccuIdentName()
503 if v == accu || v == AccumulatorName {
504 return nil, eh.NewError(args[0].ID(), "iteration variable overwrites accumulator variable")
505 }
506
507 var fn ast.Expr
508 var filter ast.Expr
509
510 if len(args) == 3 {
511 filter = args[1]
512 fn = args[2]
513 } else {
514 filter = nil
515 fn = args[1]
516 }
517
518 init := eh.NewList()
519 condition := eh.NewLiteral(types.True)
520 step := eh.NewCall(operators.Add, eh.NewAccuIdent(), eh.NewList(fn))
521
522 if filter != nil {
523 step = eh.NewCall(operators.Conditional, filter, step, eh.NewAccuIdent())
524 }
525 return eh.NewComprehension(target, v, accu, init, condition, step, eh.NewAccuIdent()), nil
526}
527
528// MakeFilter expands the input call arguments into a comprehension which produces a list which contains
529// only elements which match the provided predicate expression:

Callers 2

sortByMacroFunction · 0.92
MapMacroExpanderFunction · 0.92

Calls 9

extractIdentFunction · 0.70
NewErrorMethod · 0.65
IDMethod · 0.65
AccuIdentNameMethod · 0.65
NewListMethod · 0.65
NewLiteralMethod · 0.65
NewCallMethod · 0.65
NewAccuIdentMethod · 0.65
NewComprehensionMethod · 0.65

Tested by

no test coverage detected