()
| 223 | } |
| 224 | |
| 225 | func 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. |
| 274 | func (m *MatchFile) Provision(ctx caddy.Context) error { |
no test coverage detected