-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathexample_cel_protocol_buffers_test.go
More file actions
112 lines (98 loc) · 3.28 KB
/
Copy pathexample_cel_protocol_buffers_test.go
File metadata and controls
112 lines (98 loc) · 3.28 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package examples
import (
"fmt"
"log"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/test/proto3pb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// Example_cel_ProtocolBuffers showcases evaluating protobuf messages, enum values,
// field presence (has), and default values as documented in https://celbyexample.com/protocol-buffers/
func Example_cel_ProtocolBuffers() {
userMsg := &proto3pb.TestAllTypes{
SingleString: "Alice",
SingleInt64: 42,
NestedType: &proto3pb.TestAllTypes_SingleNestedEnum{SingleNestedEnum: proto3pb.TestAllTypes_BAR},
}
exprs := []string{
`user.single_string`,
`user.single_int64 == 42`,
`user.single_nested_enum == google.expr.proto3.test.TestAllTypes.NestedEnum.BAR`,
`has(user.single_string)`,
`has(user.single_bytes)`,
`user.single_bool`,
}
opts := []cel.EnvOption{
cel.Types(userMsg),
cel.Variable("user", cel.ObjectType("google.expr.proto3.test.TestAllTypes")),
}
for _, expr := range exprs {
prg, err := cel.Compile(expr, opts...)
if err != nil {
log.Fatalf("cel.Compile() error for %q: %v", expr, err)
}
out, _, err := prg.Eval(map[string]any{"user": userMsg})
if err != nil {
log.Fatalf("prg.Eval() error for %q: %v", expr, err)
}
fmt.Printf("%s -> %v\n", expr, out)
}
// Output:
// user.single_string -> Alice
// user.single_int64 == 42 -> true
// user.single_nested_enum == google.expr.proto3.test.TestAllTypes.NestedEnum.BAR -> true
// has(user.single_string) -> true
// has(user.single_bytes) -> false
// user.single_bool -> false
}
// Example_cel_WellKnownTypes showcases wrapper types (StringValue, BoolValue) and google.protobuf.Struct
// as documented in https://celbyexample.com/well-known-types/
func Example_cel_WellKnownTypes() {
structVal, _ := structpb.NewStruct(map[string]any{
"key": "value",
"count": 10,
})
vars := map[string]any{
"wrapped_str": wrapperspb.String("hello"),
"json_obj": structVal,
}
exprs := []string{
`wrapped_str == "hello"`,
`json_obj.key == "value"`,
`json_obj.count == 10`,
}
opts := []cel.EnvOption{
cel.Types(wrapperspb.String(""), structVal),
cel.Variable("wrapped_str", cel.ObjectType("google.protobuf.StringValue")),
cel.Variable("json_obj", cel.ObjectType("google.protobuf.Struct")),
}
for _, expr := range exprs {
prg, err := cel.Compile(expr, opts...)
if err != nil {
log.Fatalf("cel.Compile() error for %q: %v", expr, err)
}
out, _, err := prg.Eval(vars)
if err != nil {
log.Fatalf("prg.Eval() error for %q: %v", expr, err)
}
fmt.Printf("%s -> %v\n", expr, out)
}
// Output:
// wrapped_str == "hello" -> true
// json_obj.key == "value" -> true
// json_obj.count == 10 -> true
}