MCPcopy
hub / github.com/caddyserver/caddy / celMatcherJSONMacroExpander

Function celMatcherJSONMacroExpander

modules/caddyhttp/celmatcher.go:629–660  ·  view source on GitHub ↗

celMatcherJSONMacroExpander validates that the macro is called a single map literal argument. The following function call is returned: <funcName>(request, arg)

(funcName string)

Source from the content-addressed store, hash-verified

627//
628// The following function call is returned: <funcName>(request, arg)
629func celMatcherJSONMacroExpander(funcName string) parser.MacroExpander {
630 return func(eh cel.MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
631 if len(args) != 1 {
632 return nil, eh.NewError(0, "matcher requires a map literal argument")
633 }
634 arg := args[0]
635
636 switch arg.Kind() {
637 case ast.StructKind:
638 return nil, eh.NewError(arg.ID(),
639 fmt.Sprintf("matcher input must be a map literal, not a %s", arg.AsStruct().TypeName()))
640 case ast.MapKind:
641 mapExpr := arg.AsMap()
642 for _, entry := range mapExpr.Entries() {
643 isStringPlaceholder := isCELStringExpr(entry.AsMapEntry().Key())
644 if !isStringPlaceholder {
645 return nil, eh.NewError(entry.ID(), "matcher map keys must be string literals")
646 }
647 isStringListPlaceholder := isCELStringExpr(entry.AsMapEntry().Value()) ||
648 isCELStringListLiteral(entry.AsMapEntry().Value())
649 if !isStringListPlaceholder {
650 return nil, eh.NewError(entry.AsMapEntry().Value().ID(), "matcher map values must be string or list literals")
651 }
652 }
653 return eh.NewCall(funcName, eh.NewIdent(CELRequestVarName), arg), nil
654 case ast.UnspecifiedExprKind, ast.CallKind, ast.ComprehensionKind, ast.IdentKind, ast.ListKind, ast.LiteralKind, ast.SelectKind:
655 // appeasing the linter :)
656 }
657
658 return nil, eh.NewError(arg.ID(), "matcher requires a map literal argument")
659 }
660}
661
662// CELValueToMapStrList converts a CEL value to a map[string][]string
663//

Callers 1

CELMatcherImplFunction · 0.85

Calls 4

IDMethod · 0.80
isCELStringExprFunction · 0.70
isCELStringListLiteralFunction · 0.70
ValueMethod · 0.45

Tested by

no test coverage detected