MCPcopy Create free account
hub / github.com/cel-expr/cel-go / exercise2

Function exercise2

codelab/solution/codelab.go:95–116  ·  view source on GitHub ↗

exercise2 shows how to declare and use variables in expressions. Given a `request` of type `google.rpc.context.AttributeContext.Request` determine whether a specific auth claim is set.

()

Source from the content-addressed store, hash-verified

93// Given a `request` of type `google.rpc.context.AttributeContext.Request`
94// determine whether a specific auth claim is set.
95func exercise2() {
96 fmt.Println("=== Exercise 2: Variables ===\n")
97 // Construct a standard environment that accepts 'request' as input and uses
98 // the google.rpc.context.AttributeContext.Request type.
99 env, err := cel.NewEnv(
100 cel.Types(&rpcpb.AttributeContext_Request{}),
101 cel.Variable("request",
102 cel.ObjectType("google.rpc.context.AttributeContext.Request"),
103 ),
104 )
105 if err != nil {
106 glog.Exit(err)
107 }
108 ast := compile(env, `request.auth.claims.group == 'admin'`, cel.BoolType)
109 program, _ := env.Program(ast)
110
111 // Evaluate a request object that sets the proper group claim.
112 // Output: true
113 claims := map[string]string{"group": "admin"}
114 eval(program, request(auth("user:me@acme.co", claims), time.Now()))
115 fmt.Println()
116}
117
118// exercise3 demonstrates how CEL's commutative logical operators work.
119//

Callers 1

mainFunction · 0.70

Calls 8

ProgramMethod · 0.95
NewEnvFunction · 0.92
TypesFunction · 0.92
VariableFunction · 0.92
compileFunction · 0.70
evalFunction · 0.70
requestFunction · 0.70
authFunction · 0.70

Tested by

no test coverage detected