-
Notifications
You must be signed in to change notification settings - Fork 216
/
envcheck.go
343 lines (318 loc) · 10.2 KB
/
envcheck.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2019 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 envcheck checks runtime support of declarations.
//
// A set of checker declarations is scanned to produce a set of CEL
// parse trees, each of which is then sent to the runtime.
//
// Identifier declarations are compiled to an expression of just that
// identifiers. For instance, the "int" type identifier produces:
//
// int
//
// Function declarations are compiled to a separate expression for
// each overload. The expression is an invocation of the overload with
// "zeroish" arguments of the appropriate type. The zeroish arguments
// are:
//
// int 0
// uint 0u
// double 0.0
// bool false
// string ""
// bytes b""
// null_type null
// type type
// list<A> []
// map<A,B> {}
// enum E 0
// message M M{}
//
// For instance, the "_/_" function with overloads
//
// _/_: (int, int) -> int
// _/_: (uint, uint) -> uint
// _/_: (double, double) -> double
//
// compiles to the expressions
//
// (0)/(0)
// (0u)/(0u)
// (0.0)/(0.0)
//
// which are then evaluated.
//
// This test suite does not check that the overloads are implemented
// correctly, only that they are implemented at all. The test will pass
// unless the expression evaluates (with no bindings) to any result or
// error other than "no_matching_overload". For instance, the first
// two expressions for _/_ will generate division-by-zero errors, but
// this will pass the test.
//
package envcheck
import (
"context"
"fmt"
"testing"
"github.com/google/cel-spec/tools/celrpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
confpb "google.golang.org/genproto/googleapis/api/expr/conformance/v1alpha1"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
spb "google.golang.org/genproto/googleapis/rpc/status"
)
// runConfig holds the client stub for the server for the runtime.
type runConfig struct {
client celrpc.ConfClient
}
var (
// noOverload holds the proto representation of the "no_matching_overload" error.
noOverload = &exprpb.ExprValue{Kind: &exprpb.ExprValue_Error{
Error: &exprpb.ErrorSet{Errors: []*spb.Status{
status.New(codes.InvalidArgument, "no_matching_overload").Proto(),
}},
}}
)
// exprGen is an expression generator. It accepts the next expression Id to use and returns
// an Expr proto and the next unused expression Id.
type exprGen func(int64) (*exprpb.Expr, int64)
// genExpr runs an expression generator with 0 as the first expression Id.
func genExpr(gen exprGen) *exprpb.Expr {
e, _ := gen(int64(0))
return e
}
// exprNil generates a nil pointer as its Expr, not consuming any expression Ids.
// It's useful for uniform handling of nil values when construction Exprs.
var exprNil exprGen = func(id int64) (*exprpb.Expr, int64) {
return nil, id
}
// exprIdent generates an Ident expression.
func exprIdent(name string) exprGen {
return func(id int64) (*exprpb.Expr, int64) {
return &exprpb.Expr{
Id: id,
ExprKind: &exprpb.Expr_IdentExpr{
IdentExpr: &exprpb.Expr_Ident{
Name: name,
},
},
}, id + 1
}
}
// exprConst generates a Constant (literal) expression.
func exprConst(c *exprpb.Constant) exprGen {
return func(id int64) (*exprpb.Expr, int64) {
return &exprpb.Expr{
Id: id,
ExprKind: &exprpb.Expr_ConstExpr{ConstExpr: c},
}, id + 1
}
}
// exprCall generates a Call expression with the given arguments.
func exprCall(f string, args ...exprGen) exprGen {
return exprCallTarget(f, exprNil, args...)
}
// exprCallTarget generates a Call expression with the given target and arguments.
func exprCallTarget(f string, target exprGen, args ...exprGen) exprGen {
return func(id int64) (*exprpb.Expr, int64) {
tExp, id := target(id)
var argExp []*exprpb.Expr
for _, arg := range args {
e, i := arg(id)
argExp = append(argExp, e)
id = i
}
return &exprpb.Expr{
Id: id,
ExprKind: &exprpb.Expr_CallExpr{
CallExpr: &exprpb.Expr_Call{
Target: tExp,
Function: f,
Args: argExp,
},
},
}, id + 1
}
}
// emptyList generates an empty CreateList expression.
var emptyList exprGen = func(id int64) (*exprpb.Expr, int64) {
return &exprpb.Expr{
Id: id,
ExprKind: &exprpb.Expr_ListExpr{
ListExpr: &exprpb.Expr_CreateList{},
},
}, id + 1
}
// exprEmptyStruct generates an empty CreateStruct expression. The messageName may be nil,
// indicating an empty map.
func exprEmptyStruct(messageName string) exprGen {
return func(id int64) (*exprpb.Expr, int64) {
return &exprpb.Expr{
Id: id,
ExprKind: &exprpb.Expr_StructExpr{
StructExpr: &exprpb.Expr_CreateStruct{
MessageName: messageName,
},
},
}, id + 1
}
}
// emptyMap generates a CreateStruct expression for an empty map.
var emptyMap = exprEmptyStruct("")
// Generators for zero-ish constants.
var (
zeroConstBool = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_BoolValue{}})
zeroConstInt = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_Int64Value{}})
zeroConstUint = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_Uint64Value{}})
zeroConstDouble = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_DoubleValue{}})
zeroConstString = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_StringValue{}})
zeroConstBytes = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_BytesValue{}})
zeroConstNull = exprConst(&exprpb.Constant{ConstantKind: &exprpb.Constant_NullValue{}})
)
// zeroValuePrimitive returns a generator for the zero-ish value of a primitive type.
func zeroValuePrimitive(p exprpb.Type_PrimitiveType) (exprGen, error) {
switch p {
case exprpb.Type_BOOL:
return zeroConstBool, nil
case exprpb.Type_INT64:
return zeroConstInt, nil
case exprpb.Type_UINT64:
return zeroConstUint, nil
case exprpb.Type_DOUBLE:
return zeroConstDouble, nil
case exprpb.Type_STRING:
return zeroConstString, nil
case exprpb.Type_BYTES:
return zeroConstBytes, nil
default:
return nil, fmt.Errorf("unknown primitive type: %v", p)
}
}
// zeroValueWellKnown returns a generator for the zero-ish value of a well-known type.
func zeroValueWellKnown(w exprpb.Type_WellKnownType) (exprGen, error) {
switch w {
case exprpb.Type_ANY:
return zeroConstInt, nil
case exprpb.Type_TIMESTAMP:
return exprCall("timestamp", zeroConstInt), nil
case exprpb.Type_DURATION:
return exprCall("duration", zeroConstInt), nil
default:
return nil, fmt.Errorf("unknown well known type: %v", w)
}
}
// zeroValue returns a generator for the zero-ish value of a Type.
func zeroValue(tp *exprpb.Type) (exprGen, error) {
switch t := tp.TypeKind.(type) {
case *exprpb.Type_Dyn:
return zeroConstInt, nil
case *exprpb.Type_Null:
return zeroConstNull, nil
case *exprpb.Type_Primitive:
return zeroValuePrimitive(t.Primitive)
case *exprpb.Type_Wrapper:
return zeroValuePrimitive(t.Wrapper)
case *exprpb.Type_WellKnown:
return zeroValueWellKnown(t.WellKnown)
case *exprpb.Type_ListType_:
return emptyList, nil
case *exprpb.Type_MapType_:
return emptyMap, nil
case *exprpb.Type_Function:
return nil, fmt.Errorf("bad_type_function")
case *exprpb.Type_MessageType:
return exprEmptyStruct(t.MessageType), nil
case *exprpb.Type_TypeParam:
return zeroConstInt, nil
case *exprpb.Type_Type:
return exprIdent("type"), nil
case *exprpb.Type_Error:
return nil, fmt.Errorf("error type")
case *exprpb.Type_AbstractType_:
return nil, fmt.Errorf("abstract type %s", t.AbstractType.Name)
default:
return nil, fmt.Errorf("unknown type kind: %v", tp.GetTypeKind())
}
}
// overloadExpr returns the generator for a call to a given overload with zero-ish arguments.
func overloadExpr(name string, o *exprpb.Decl_FunctionDecl_Overload) (exprGen, error) {
var args []exprGen
for _, param := range o.Params {
arg, err := zeroValue(param)
if err != nil {
return nil, err
}
args = append(args, arg)
}
if o.IsInstanceFunction && len(args) > 0 {
return exprCallTarget(name, args[0], args[1:]...), nil
}
return exprCall(name, args...), nil
}
// TestDecl checks whether the runtime supports a given declaration by generating
// a test expression and running it.
func (r *runConfig) TestDecl(t *testing.T, decl *exprpb.Decl) {
switch d := decl.DeclKind.(type) {
case *exprpb.Decl_Ident:
// For identifiers, the name itself is a suitable program.
prog := genExpr(exprIdent(decl.Name))
result, err := r.runProg(decl.Name, prog)
if err != nil {
t.Fatal(err)
}
// Any value is okay, any error is a problem.
_, ok := result.Kind.(*exprpb.ExprValue_Value)
if !ok {
t.Errorf("got %v, want value", result)
}
case *exprpb.Decl_Function:
for _, o := range d.Function.Overloads {
t.Run(o.OverloadId, func(tt *testing.T) {
g, err := overloadExpr(decl.Name, o)
if err != nil {
tt.Fatal(err)
}
result, err := r.runProg(o.OverloadId, genExpr(g))
if err != nil {
tt.Fatal(err)
}
// Only the "no_matching_overload" error is a problem.
if proto.Equal(result, noOverload) {
tt.Error("no matching overload")
}
})
}
default:
t.Errorf("unknown decl kind %v", decl.DeclKind)
}
}
// runProg evaluates a given expression on the runtime server and returns the result.
func (r *runConfig) runProg(name string, prog *exprpb.Expr) (*exprpb.ExprValue, error) {
parsedExpr := &exprpb.ParsedExpr{
Expr: prog,
SourceInfo: &exprpb.SourceInfo{},
}
ereq := confpb.EvalRequest{
ExprKind: &confpb.EvalRequest_ParsedExpr{ParsedExpr: parsedExpr},
}
eres, err := r.client.Eval(context.Background(), &ereq)
if err != nil {
return nil, fmt.Errorf("fatal eval RPC error for %s: %v", name, err)
}
if eres == nil || eres.Result == nil {
return nil, fmt.Errorf("empty eval response for %s", name)
}
return eres.Result, nil
}