feat(expressions): add segments function to break path into segments (#916)
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
bf42014ac3
commit
a735770c93
4 changed files with 266 additions and 80 deletions
|
|
@ -2,6 +2,7 @@ package expressions
|
|||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cel-go/cel"
|
||||
"github.com/google/cel-go/common/types"
|
||||
|
|
@ -54,6 +55,28 @@ func BotEnvironment() (*cel.Env, error) {
|
|||
}),
|
||||
),
|
||||
),
|
||||
|
||||
cel.Function("segments",
|
||||
cel.Overload("segments_string_list_string",
|
||||
[]*cel.Type{cel.StringType},
|
||||
cel.ListType(cel.StringType),
|
||||
cel.UnaryBinding(func(path ref.Val) ref.Val {
|
||||
pathStrType, ok := path.(types.String)
|
||||
if !ok {
|
||||
return types.ValOrErr(path, "path is not a string, but is %T", path)
|
||||
}
|
||||
|
||||
pathStr := string(pathStrType)
|
||||
if !strings.HasPrefix(pathStr, "/") {
|
||||
return types.ValOrErr(path, "path does not start with /")
|
||||
}
|
||||
|
||||
pathList := strings.Split(string(pathStr), "/")[1:]
|
||||
|
||||
return types.NewStringList(types.DefaultTypeAdapter, pathList)
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue