-
Notifications
You must be signed in to change notification settings - Fork 19
/
ast.go
148 lines (136 loc) · 4.05 KB
/
ast.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
// Copyright 2023-2024 Buf Technologies, Inc.
//
// 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 protovalidate
import (
"fmt"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/interpreter"
"google.golang.org/protobuf/reflect/protoreflect"
)
// astSet represents a collection of compiledAST and their associated cel.Env.
type astSet struct {
env *cel.Env
asts []compiledAST
}
// Merge combines a set with another, producing a new ASTSet.
func (set astSet) Merge(other astSet) astSet {
out := astSet{
env: set.env,
asts: make([]compiledAST, 0, len(set.asts)+len(other.asts)),
}
if out.env == nil {
out.env = other.env
}
out.asts = append(out.asts, set.asts...)
out.asts = append(out.asts, other.asts...)
return out
}
// ReduceResiduals generates a ProgramSet, performing a partial evaluation of
// the ASTSet to optimize the expression. If the expression is optimized to
// either a true or empty string constant result, no compiledProgram is
// generated for it. The main usage of this is to elide tautological expressions
// from the final result.
func (set astSet) ReduceResiduals(opts ...cel.ProgramOption) (programSet, error) {
residuals := make([]compiledAST, 0, len(set.asts))
options := append([]cel.ProgramOption{
cel.EvalOptions(
cel.OptTrackState,
cel.OptExhaustiveEval,
cel.OptOptimize,
cel.OptPartialEval,
),
}, opts...)
for _, ast := range set.asts {
program, err := ast.toProgram(set.env, options...)
if err != nil {
residuals = append(residuals, ast)
continue
}
val, details, _ := program.Program.Eval(interpreter.EmptyActivation())
if val != nil {
switch value := val.Value().(type) {
case bool:
if value {
continue
}
case string:
if value == "" {
continue
}
}
}
residual, err := set.env.ResidualAst(ast.AST, details)
if err != nil {
residuals = append(residuals, ast)
} else {
residuals = append(residuals, compiledAST{
AST: residual,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Descriptor: ast.Descriptor,
})
}
}
return astSet{
env: set.env,
asts: residuals,
}.ToProgramSet(opts...)
}
// ToProgramSet generates a ProgramSet from the specified ASTs.
func (set astSet) ToProgramSet(opts ...cel.ProgramOption) (out programSet, err error) {
if l := len(set.asts); l == 0 {
return nil, nil
}
out = make(programSet, len(set.asts))
for i, ast := range set.asts {
out[i], err = ast.toProgram(set.env, opts...)
if err != nil {
return nil, err
}
}
return out, nil
}
// SetRuleValue sets the rule value for the programs in the ASTSet.
func (set *astSet) SetRuleValue(
ruleValue protoreflect.Value,
ruleDescriptor protoreflect.FieldDescriptor,
) {
set.asts = append([]compiledAST{}, set.asts...)
for i := range set.asts {
set.asts[i].Value = ruleValue
set.asts[i].Descriptor = ruleDescriptor
}
}
type compiledAST struct {
AST *cel.Ast
Source *validate.Constraint
Path []*validate.FieldPathElement
Value protoreflect.Value
Descriptor protoreflect.FieldDescriptor
}
func (ast compiledAST) toProgram(env *cel.Env, opts ...cel.ProgramOption) (out compiledProgram, err error) {
prog, err := env.Program(ast.AST, opts...)
if err != nil {
return out, &CompilationError{cause: fmt.Errorf("failed to compile program %s: %w", ast.Source.GetId(), err)}
}
return compiledProgram{
Program: prog,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Descriptor: ast.Descriptor,
}, nil
}