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

Function celFileMatcherMacroExpander

modules/caddyhttp/fileserver/matcher.go:225–271  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

223}
224
225func celFileMatcherMacroExpander() parser.MacroExpander {
226 return func(eh parser.ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
227 if len(args) == 0 {
228 return eh.NewCall("file",
229 eh.NewIdent(caddyhttp.CELRequestVarName),
230 eh.NewMap(),
231 ), nil
232 }
233 if len(args) == 1 {
234 arg := args[0]
235 if isCELStringLiteral(arg) || isCELCaddyPlaceholderCall(arg) {
236 return eh.NewCall("file",
237 eh.NewIdent(caddyhttp.CELRequestVarName),
238 eh.NewMap(eh.NewMapEntry(
239 eh.NewLiteral(types.String("try_files")),
240 eh.NewList(arg),
241 false,
242 )),
243 ), nil
244 }
245 if isCELTryFilesLiteral(arg) {
246 return eh.NewCall("file", eh.NewIdent(caddyhttp.CELRequestVarName), arg), nil
247 }
248 return nil, &common.Error{
249 Location: eh.OffsetLocation(arg.ID()),
250 Message: "matcher requires either a map or string literal argument",
251 }
252 }
253
254 for _, arg := range args {
255 if !isCELStringLiteral(arg) && !isCELCaddyPlaceholderCall(arg) {
256 return nil, &common.Error{
257 Location: eh.OffsetLocation(arg.ID()),
258 Message: "matcher only supports repeated string literal arguments",
259 }
260 }
261 }
262 return eh.NewCall("file",
263 eh.NewIdent(caddyhttp.CELRequestVarName),
264 eh.NewMap(eh.NewMapEntry(
265 eh.NewLiteral(types.String("try_files")),
266 eh.NewList(args...),
267 false,
268 )),
269 ), nil
270 }
271}
272
273// Provision sets up m's defaults.
274func (m *MatchFile) Provision(ctx caddy.Context) error {

Callers 1

CELLibraryMethod · 0.85

Calls 5

isCELTryFilesLiteralFunction · 0.85
IDMethod · 0.80
isCELStringLiteralFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected