Skip to content

Commit 3bfc182

Browse files
jchadwick-bufbufdev
andauthored
Refactor the API for v1 (#173)
Co-authored-by: bufdev <[email protected]>
1 parent 9744b53 commit 3bfc182

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+580
-1323
lines changed

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ issues:
6464
- varnamelen
6565
- unparam
6666
- gosec
67-
- path: celext/lib.go
67+
- path: cel/library.go
6868
linters:
6969
# setting up custom functions/overloads appears duplicative (false positive)
7070
- dupl
7171
# Types are checked internally within CEL. There are bigger issues if its
7272
# type analysis is wrong
7373
- forcetypeassert
7474
# static unexported lookup tables
75-
- path: internal/constraints/lookups.go
75+
- path: lookups.go
7676
linters:
7777
- gochecknoglobals
7878
- path: resolver/resolver.go

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,9 @@ validator, err := protovalidate.New(
182182
)
183183
```
184184

185-
### Support legacy `protoc-gen-validate` constraints
185+
### Legacy `protoc-gen-validate` constraints
186186

187-
The `protovalidate-go` module comes with a `legacy` package which adds opt-in support
188-
for existing `protoc-gen-validate` constraints. Provide the`legacy.WithLegacySupport`
189-
option when initializing the validator:
190-
191-
```go
192-
validator, err := protovalidate.New(
193-
legacy.WithLegacySupport(legacy.ModeMerge),
194-
)
195-
```
196-
197-
`protoc-gen-validate` code generation is **not** used by `protovalidate-go`. The
198-
`legacy` package assumes the `protoc-gen-validate` extensions are imported into
199-
the generated code via `github.com/envoyproxy/protoc-gen-validate/validate`.
200-
201-
A [migration tool](https://github.com/bufbuild/protovalidate/tree/main/tools/protovalidate-migrate) is also available to incrementally upgrade legacy constraints in `.proto` files.
187+
`protoc-gen-validate` code generation is **not** used by `protovalidate-go`. A [migration tool](https://github.com/bufbuild/protovalidate/tree/main/tools/protovalidate-migrate) is available to upgrade legacy constraints in `.proto` files.
202188

203189
## Performance
204190

internal/evaluator/any.go renamed to any.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package evaluator
15+
package protovalidate
1616

1717
import (
1818
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
19-
"github.com/bufbuild/protovalidate-go/internal/errors"
2019
"google.golang.org/protobuf/proto"
2120
"google.golang.org/protobuf/reflect/protoreflect"
2221
)
@@ -27,15 +26,15 @@ var (
2726
anyInRuleDescriptor = (&validate.AnyRules{}).ProtoReflect().Descriptor().Fields().ByName("in")
2827
anyInRulePath = &validate.FieldPath{
2928
Elements: []*validate.FieldPathElement{
30-
errors.FieldPathElement(anyRuleDescriptor),
31-
errors.FieldPathElement(anyInRuleDescriptor),
29+
fieldPathElement(anyRuleDescriptor),
30+
fieldPathElement(anyInRuleDescriptor),
3231
},
3332
}
3433
anyNotInDescriptor = (&validate.AnyRules{}).ProtoReflect().Descriptor().Fields().ByName("not_in")
3534
anyNotInRulePath = &validate.FieldPath{
3635
Elements: []*validate.FieldPathElement{
37-
errors.FieldPathElement(anyRuleDescriptor),
38-
errors.FieldPathElement(anyNotInDescriptor),
36+
fieldPathElement(anyRuleDescriptor),
37+
fieldPathElement(anyNotInDescriptor),
3938
},
4039
}
4140
)
@@ -62,10 +61,10 @@ type anyPB struct {
6261
func (a anyPB) Evaluate(val protoreflect.Value, failFast bool) error {
6362
typeURL := val.Message().Get(a.TypeURLDescriptor).String()
6463

65-
err := &errors.ValidationError{}
64+
err := &ValidationError{}
6665
if len(a.In) > 0 {
6766
if _, ok := a.In[typeURL]; !ok {
68-
err.Violations = append(err.Violations, &errors.Violation{
67+
err.Violations = append(err.Violations, &Violation{
6968
Proto: &validate.Violation{
7069
Field: a.base.fieldPath(),
7170
Rule: a.base.rulePath(anyInRulePath),
@@ -85,7 +84,7 @@ func (a anyPB) Evaluate(val protoreflect.Value, failFast bool) error {
8584

8685
if len(a.NotIn) > 0 {
8786
if _, ok := a.NotIn[typeURL]; ok {
88-
err.Violations = append(err.Violations, &errors.Violation{
87+
err.Violations = append(err.Violations, &Violation{
8988
Proto: &validate.Violation{
9089
Field: a.base.fieldPath(),
9190
Rule: a.base.rulePath(anyNotInRulePath),

internal/expression/ast.go renamed to ast.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expression
15+
package protovalidate
1616

1717
import (
18+
"fmt"
19+
1820
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
19-
"github.com/bufbuild/protovalidate-go/internal/errors"
2021
"github.com/google/cel-go/cel"
2122
"github.com/google/cel-go/interpreter"
2223
"google.golang.org/protobuf/reflect/protoreflect"
2324
)
2425

25-
// ASTSet represents a collection of compiledAST and their associated cel.Env.
26-
type ASTSet struct {
26+
// astSet represents a collection of compiledAST and their associated cel.Env.
27+
type astSet struct {
2728
env *cel.Env
2829
asts []compiledAST
2930
}
3031

3132
// Merge combines a set with another, producing a new ASTSet.
32-
func (set ASTSet) Merge(other ASTSet) ASTSet {
33-
out := ASTSet{
33+
func (set astSet) Merge(other astSet) astSet {
34+
out := astSet{
3435
env: set.env,
3536
asts: make([]compiledAST, 0, len(set.asts)+len(other.asts)),
3637
}
@@ -47,7 +48,7 @@ func (set ASTSet) Merge(other ASTSet) ASTSet {
4748
// either a true or empty string constant result, no compiledProgram is
4849
// generated for it. The main usage of this is to elide tautological expressions
4950
// from the final result.
50-
func (set ASTSet) ReduceResiduals(opts ...cel.ProgramOption) (ProgramSet, error) {
51+
func (set astSet) ReduceResiduals(opts ...cel.ProgramOption) (programSet, error) {
5152
residuals := make([]compiledAST, 0, len(set.asts))
5253
options := append([]cel.ProgramOption{
5354
cel.EvalOptions(
@@ -91,18 +92,18 @@ func (set ASTSet) ReduceResiduals(opts ...cel.ProgramOption) (ProgramSet, error)
9192
}
9293
}
9394

94-
return ASTSet{
95+
return astSet{
9596
env: set.env,
9697
asts: residuals,
9798
}.ToProgramSet(opts...)
9899
}
99100

100101
// ToProgramSet generates a ProgramSet from the specified ASTs.
101-
func (set ASTSet) ToProgramSet(opts ...cel.ProgramOption) (out ProgramSet, err error) {
102+
func (set astSet) ToProgramSet(opts ...cel.ProgramOption) (out programSet, err error) {
102103
if l := len(set.asts); l == 0 {
103104
return nil, nil
104105
}
105-
out = make(ProgramSet, len(set.asts))
106+
out = make(programSet, len(set.asts))
106107
for i, ast := range set.asts {
107108
out[i], err = ast.toProgram(set.env, opts...)
108109
if err != nil {
@@ -113,7 +114,7 @@ func (set ASTSet) ToProgramSet(opts ...cel.ProgramOption) (out ProgramSet, err e
113114
}
114115

115116
// SetRuleValue sets the rule value for the programs in the ASTSet.
116-
func (set *ASTSet) SetRuleValue(
117+
func (set *astSet) SetRuleValue(
117118
ruleValue protoreflect.Value,
118119
ruleDescriptor protoreflect.FieldDescriptor,
119120
) {
@@ -135,8 +136,7 @@ type compiledAST struct {
135136
func (ast compiledAST) toProgram(env *cel.Env, opts ...cel.ProgramOption) (out compiledProgram, err error) {
136137
prog, err := env.Program(ast.AST, opts...)
137138
if err != nil {
138-
return out, errors.NewCompilationErrorf(
139-
"failed to compile program %s: %w", ast.Source.GetId(), err)
139+
return out, &CompilationError{cause: fmt.Errorf("failed to compile program %s: %w", ast.Source.GetId(), err)}
140140
}
141141
return compiledProgram{
142142
Program: prog,

internal/expression/ast_test.go renamed to ast_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expression
15+
package protovalidate
1616

1717
import (
1818
"testing"
1919

2020
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
21-
"github.com/bufbuild/protovalidate-go/celext"
21+
pvcel "github.com/bufbuild/protovalidate-go/cel"
2222
"github.com/google/cel-go/cel"
2323
"github.com/stretchr/testify/assert"
2424
"github.com/stretchr/testify/require"
@@ -28,8 +28,8 @@ import (
2828
func TestASTSet_Merge(t *testing.T) {
2929
t.Parallel()
3030

31-
var set ASTSet
32-
other := ASTSet{
31+
var set astSet
32+
other := astSet{
3333
env: &cel.Env{},
3434
asts: []compiledAST{
3535
{AST: &cel.Ast{}},
@@ -40,7 +40,7 @@ func TestASTSet_Merge(t *testing.T) {
4040
assert.Equal(t, other.env, merged.env)
4141
assert.Equal(t, other.asts, merged.asts)
4242

43-
another := ASTSet{
43+
another := astSet{
4444
asts: []compiledAST{
4545
{AST: &cel.Ast{}},
4646
{AST: &cel.Ast{}},
@@ -56,11 +56,11 @@ func TestASTSet_Merge(t *testing.T) {
5656
func TestASTSet_ToProgramSet(t *testing.T) {
5757
t.Parallel()
5858

59-
env, err := celext.DefaultEnv(false)
59+
env, err := cel.NewEnv(cel.Lib(pvcel.NewLibrary()))
6060
require.NoError(t, err)
6161

62-
asts, err := CompileASTs(
63-
Expressions{
62+
asts, err := compileASTs(
63+
expressions{
6464
Constraints: []*validate.Constraint{
6565
{Expression: proto.String("foo")},
6666
},
@@ -75,7 +75,7 @@ func TestASTSet_ToProgramSet(t *testing.T) {
7575
assert.Len(t, set, 1)
7676
assert.Equal(t, asts.asts[0].Source, set[0].Source)
7777

78-
empty := ASTSet{}
78+
empty := astSet{}
7979
set, err = empty.ToProgramSet()
8080
assert.Empty(t, set)
8181
require.NoError(t, err)
@@ -84,11 +84,11 @@ func TestASTSet_ToProgramSet(t *testing.T) {
8484
func TestASTSet_ReduceResiduals(t *testing.T) {
8585
t.Parallel()
8686

87-
env, err := celext.DefaultEnv(false)
87+
env, err := cel.NewEnv(cel.Lib(pvcel.NewLibrary()))
8888
require.NoError(t, err)
8989

90-
asts, err := CompileASTs(
91-
Expressions{
90+
asts, err := compileASTs(
91+
expressions{
9292
Constraints: []*validate.Constraint{
9393
{Expression: proto.String("foo")},
9494
},
@@ -98,7 +98,7 @@ func TestASTSet_ReduceResiduals(t *testing.T) {
9898
)
9999
require.NoError(t, err)
100100
assert.Len(t, asts.asts, 1)
101-
set, err := asts.ReduceResiduals(cel.Globals(&Variable{Name: "foo", Val: true}))
101+
set, err := asts.ReduceResiduals(cel.Globals(&variable{Name: "foo", Val: true}))
102102
require.NoError(t, err)
103103
assert.Empty(t, set)
104104
}

internal/evaluator/base.go renamed to base.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package evaluator
15+
package protovalidate
1616

1717
import (
1818
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
19-
"github.com/bufbuild/protovalidate-go/internal/errors"
2019
"google.golang.org/protobuf/reflect/protoreflect"
2120
)
2221

@@ -39,7 +38,7 @@ type base struct {
3938
func newBase(valEval *value) base {
4039
return base{
4140
Descriptor: valEval.Descriptor,
42-
FieldPathElement: errors.FieldPathElement(valEval.Descriptor),
41+
FieldPathElement: fieldPathElement(valEval.Descriptor),
4342
RulePrefix: valEval.NestedRule,
4443
}
4544
}

0 commit comments

Comments
 (0)