-
Notifications
You must be signed in to change notification settings - Fork 4
/
member.go
61 lines (53 loc) · 1.83 KB
/
member.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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/iam/iammember"
iamv1 "go.einride.tech/iam/proto/gen/einride/iam/v1"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
// MemberFunction is the name of the CEL member function.
const MemberFunction = "member"
const memberFunctionOverload = "member_caller_string_string"
// NewMemberFunctionDeclaration creates a new declaration for the member function.
func NewMemberFunctionDeclaration() *expr.Decl {
return decls.NewFunction(
MemberFunction,
decls.NewInstanceOverload(
memberFunctionOverload,
[]*expr.Type{
decls.NewObjectType(string((&iamv1.Caller{}).ProtoReflect().Descriptor().FullName())),
decls.String,
},
decls.String,
),
)
}
// NewMemberFunctionImplementation creates a new implementation for the member function.
func NewMemberFunctionImplementation() *functions.Overload {
return &functions.Overload{
Operator: memberFunctionOverload,
Binary: func(callerVal, kindVal ref.Val) ref.Val {
caller, ok := callerVal.Value().(*iamv1.Caller)
if !ok {
return types.NewErr("test: unexpected type of arg 1, expected %T but got %T", &iamv1.Caller{}, callerVal.Value())
}
kind, ok := kindVal.Value().(string)
if !ok {
return types.NewErr("test: unexpected type of arg 2, expected string but got %T", kindVal.Value())
}
for _, member := range caller.GetMembers() {
memberKind, memberValue, ok := iammember.Parse(member)
if !ok {
return types.NewErr("member: error parsing caller member '%s'", member)
}
if memberKind == kind {
return types.String(memberValue)
}
}
return types.NewErr("member: no kind '%s' found in caller", kind)
},
}
}