Repository navigation
Expand file tree
/
Copy pathast.go
More file actions
151 lines (138 loc) · 4.19 KB
/
Copy pathast.go
File metadata and controls
151 lines (138 loc) · 4.19 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
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
// Copyright 2023-2026 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"
"slices"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"github.com/google/cel-go/cel"
"google.golang.org/protobuf/reflect/protoreflect"
)
// astSet represents a collection of compiledAST and their associated cel.Env.
type astSet []compiledAST
// Merge combines a set with another, producing a new ASTSet.
func (set astSet) Merge(other astSet) astSet {
out := make([]compiledAST, 0, len(set)+len(other))
out = append(out, set...)
out = append(out, other...)
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(rules protoreflect.Message, opts ...cel.ProgramOption) (programSet, error) {
residuals := make(astSet, 0, len(set))
options := append([]cel.ProgramOption{
cel.EvalOptions(
cel.OptTrackState,
cel.OptExhaustiveEval,
cel.OptOptimize,
cel.OptPartialEval,
),
}, opts...)
activation := getBindings()
defer putBindings(activation)
activation.Rules = rules.Interface()
for _, ast := range set {
activation.Rule = ast.Value.Interface()
program, err := ast.toProgram(ast.Env, options...)
if err != nil {
residuals = append(residuals, ast)
continue
}
val, details, _ := program.Program.Eval(activation)
if val != nil {
switch value := val.Value().(type) {
case bool:
if value {
continue
}
case string:
if value == "" {
continue
}
}
}
residual, err := ast.Env.ResidualAst(ast.AST, details)
if err != nil {
residuals = append(residuals, ast)
} else {
residuals = append(residuals, compiledAST{
AST: residual,
Env: ast.Env,
Rules: ast.Rules,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Descriptor: ast.Descriptor,
})
}
}
return residuals.ToProgramSet(opts...)
}
// ToProgramSet generates a ProgramSet from the specified ASTs.
func (set astSet) ToProgramSet(opts ...cel.ProgramOption) (out programSet, err error) {
if len(set) == 0 {
return out, nil
}
out.env = set[0].Env
programs := make([]compiledProgram, len(set))
for i, ast := range set {
programs[i], err = ast.toProgram(ast.Env, opts...)
if err != nil {
return out, err
}
}
out.programs = programs
return out, nil
}
// SetRuleValue sets the rule and rules value for the programs in the ASTSet.
func (set astSet) WithRuleValues(
rules protoreflect.Message,
ruleValue protoreflect.Value,
ruleDescriptor protoreflect.FieldDescriptor,
) (out astSet, err error) {
out = slices.Clone(set)
for i := range set {
out[i].Rules = rules
out[i].Value = ruleValue
out[i].Descriptor = ruleDescriptor
}
return out, nil
}
type compiledAST struct {
AST *cel.Ast
Env *cel.Env
Rules protoreflect.Message
Source *validate.Rule
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,
Rules: ast.Rules,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Descriptor: ast.Descriptor,
}, nil
}