summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/celmatcher.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-03-30 11:49:53 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-03-30 11:49:53 -0600
commit105acfa08664c97460a6fe3fb49635618be5bcb2 (patch)
tree3c8ea6bc0fa1d335787a5b4852bd03ae50d4ef81 /modules/caddyhttp/celmatcher.go
parentdeba26d225c5b321a944439eb6b108117ac3d569 (diff)
Keep type information with placeholders until replacements happen
Diffstat (limited to 'modules/caddyhttp/celmatcher.go')
-rw-r--r--modules/caddyhttp/celmatcher.go32
1 files changed, 30 insertions, 2 deletions
diff --git a/modules/caddyhttp/celmatcher.go b/modules/caddyhttp/celmatcher.go
index a78bd9c..84565e4 100644
--- a/modules/caddyhttp/celmatcher.go
+++ b/modules/caddyhttp/celmatcher.go
@@ -86,7 +86,7 @@ func (m *MatchExpression) Provision(_ caddy.Context) error {
decls.NewFunction(placeholderFuncName,
decls.NewOverload(placeholderFuncName+"_httpRequest_string",
[]*exprpb.Type{httpRequestObjectType, decls.String},
- decls.String)),
+ decls.Any)),
),
cel.CustomTypeAdapter(celHTTPRequestTypeAdapter{}),
ext.Strings(),
@@ -210,7 +210,35 @@ func caddyPlaceholderFunc(lhs, rhs ref.Val) ref.Val {
repl := celReq.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
val, _ := repl.Get(string(phStr))
- return types.String(val)
+ // TODO: this is... kinda awful and underwhelming, how can we expand CEL's type system more easily?
+ switch v := val.(type) {
+ case string:
+ return types.String(v)
+ case fmt.Stringer:
+ return types.String(v.String())
+ case error:
+ return types.NewErr(v.Error())
+ case int:
+ return types.Int(v)
+ case int32:
+ return types.Int(v)
+ case int64:
+ return types.Int(v)
+ case uint:
+ return types.Int(v)
+ case uint32:
+ return types.Int(v)
+ case uint64:
+ return types.Int(v)
+ case float32:
+ return types.Double(v)
+ case float64:
+ return types.Double(v)
+ case bool:
+ return types.Bool(v)
+ default:
+ return types.String(fmt.Sprintf("%+v", v))
+ }
}
// Interface guards