-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjoin.go
46 lines (41 loc) · 1.45 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package iamcel
import (
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/interpreter/functions"
"go.einride.tech/aip/resourcename"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
// JoinFunction is the name of the CEL descendant function.
const JoinFunction = "join"
const joinFunctionOverload = "join_string_string"
// NewJoinFunctionDeclaration creates a new declaration for the descendant function.
func NewJoinFunctionDeclaration() *expr.Decl {
return decls.NewFunction(
JoinFunction,
// TODO: if ever possible in CEL-go, declare this as a variadic function.
decls.NewOverload(
joinFunctionOverload,
[]*expr.Type{decls.String, decls.String},
decls.String,
),
)
}
// NewJoinFunctionImplementation creates a new implementation for the descendant function.
func NewJoinFunctionImplementation() *functions.Overload {
return &functions.Overload{
Operator: joinFunctionOverload,
Binary: func(parentVal, childVal ref.Val) ref.Val {
parent, ok := parentVal.Value().(string)
if !ok {
return types.NewErr("parent: unexpected type of arg 1, expected string but got %T", parentVal.Value())
}
child, ok := childVal.Value().(string)
if !ok {
return types.NewErr("child: unexpected type of arg 2, expected string but got %T", childVal.Value())
}
return types.String(resourcename.Join(parent, child))
},
}
}