feat(expressions): add missingHeader function to bot environment (#870)
Also add tests to the bot expressions custom functions.
This commit is contained in:
parent
6b639cd911
commit
76dcd21582
4 changed files with 325 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/google/cel-go/cel"
|
||||
"github.com/google/cel-go/common/types"
|
||||
"github.com/google/cel-go/common/types/ref"
|
||||
"github.com/google/cel-go/common/types/traits"
|
||||
"github.com/google/cel-go/ext"
|
||||
)
|
||||
|
||||
|
|
@ -26,6 +27,33 @@ func BotEnvironment() (*cel.Env, error) {
|
|||
cel.Variable("load_1m", cel.DoubleType),
|
||||
cel.Variable("load_5m", cel.DoubleType),
|
||||
cel.Variable("load_15m", cel.DoubleType),
|
||||
|
||||
// Bot-specific functions:
|
||||
cel.Function("missingHeader",
|
||||
cel.Overload("missingHeader_map_string_string_string",
|
||||
[]*cel.Type{cel.MapType(cel.StringType, cel.StringType), cel.StringType},
|
||||
cel.BoolType,
|
||||
cel.BinaryBinding(func(headers, key ref.Val) ref.Val {
|
||||
// Convert headers to a trait that supports Find
|
||||
headersMap, ok := headers.(traits.Indexer)
|
||||
if !ok {
|
||||
return types.ValOrErr(headers, "headers is not a map, but is %T", headers)
|
||||
}
|
||||
|
||||
keyStr, ok := key.(types.String)
|
||||
if !ok {
|
||||
return types.ValOrErr(key, "key is not a string, but is %T", key)
|
||||
}
|
||||
|
||||
val := headersMap.Get(keyStr)
|
||||
// Check if the key is missing by testing for an error
|
||||
if types.IsError(val) {
|
||||
return types.Bool(true) // header is missing
|
||||
}
|
||||
return types.Bool(false) // header is present
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue