Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.pprof
*.svg
cover.out
*.test
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
- noinlineerr # inline is fine
- nonamedreturns # usage of named returns should be selective
- testpackage # internal tests are fine
- thelper # overzealous breaking of stack traces
- wrapcheck # don't _always_ need to wrap errors
- wsl # over-generous whitespace violates house style
- wsl_v5 # over-generous whitespace violates house style
Expand Down
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ SHELL := bash
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
MAKEFLAGS += --no-print-directory
BIN := .tmp/bin
TMP := .tmp
BIN := $(TMP)/bin
BENCH_TMP := $(TMP)/bench
COPYRIGHT_YEARS := 2023-2025
LICENSE_IGNORE := -e internal/testdata/
# Set to use a different compiler. For example, `GO=go1.18rc1 make test`.
Expand Down Expand Up @@ -94,10 +96,26 @@ checkgenerate: generate
@# Used in CI to verify that `make generate` doesn't produce a diff.
test -z "$$(git status --porcelain | tee /dev/stderr)"


BENCH ?= .
BENCH_COUNT ?= 10
BENCH_NAME ?= $(shell date +%F:%T)
.PHONY: bench
bench: $(BENCH_TMP)
go test -bench="$(BENCH)" -benchmem \
-memprofile "$(BENCH_TMP)/$(BENCH_NAME).mem.profile" \
-cpuprofile "$(BENCH_TMP)/$(BENCH_NAME).cpu.profile" \
-count $(BENCH_COUNT) \
| tee "$(BENCH_TMP)/$(BENCH_NAME).bench.txt"


.PHONY: upgrade-go
upgrade-go:
$(GO) get -u -t ./... && $(GO) mod tidy -v

$(BENCH_TMP):
@mkdir -p $(BENCH_TMP)

$(BIN):
@mkdir -p $(BIN)

Expand Down
18 changes: 13 additions & 5 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
pvcel "buf.build/go/protovalidate/cel"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/interpreter"
"google.golang.org/protobuf/reflect/protoreflect"
)

Expand All @@ -41,7 +40,7 @@ func (set astSet) Merge(other astSet) astSet {
// 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) {
func (set astSet) ReduceResiduals(rules protoreflect.Message, opts ...cel.ProgramOption) (programSet, error) {
residuals := make(astSet, 0, len(set))
options := append([]cel.ProgramOption{
cel.EvalOptions(
Expand All @@ -52,17 +51,26 @@ func (set astSet) ReduceResiduals(opts ...cel.ProgramOption) (programSet, error)
),
}, opts...)

baseActivation := &variable{
Name: "rules",
Val: rules.Interface(),
}

for _, ast := range set {
options := slices.Clone(options)
activation := baseActivation
if ast.Value.IsValid() {
options = append(options, cel.Globals(&variable{Name: "rule", Val: ast.Value.Interface()}))
activation = &variable{
Name: "rule",
Val: ast.Value.Interface(),
Next: activation,
}
}
program, err := ast.toProgram(ast.Env, options...)
if err != nil {
residuals = append(residuals, ast)
continue
}
val, details, _ := program.Program.Eval(interpreter.EmptyActivation())
val, details, _ := program.Program.Eval(activation)
if val != nil {
switch value := val.Value().(type) {
case bool:
Expand Down
5 changes: 4 additions & 1 deletion ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func TestASTSet_ReduceResiduals(t *testing.T) {
)
require.NoError(t, err)
assert.Len(t, asts, 1)
set, err := asts.ReduceResiduals(cel.Globals(&variable{Name: "foo", Val: true}))
set, err := asts.ReduceResiduals(
(&validate.StringRules{}).ProtoReflect(),
cel.Globals(&variable{Name: "foo", Val: true}),
)
require.NoError(t, err)
assert.Empty(t, set)
}
2 changes: 2 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ managed:
disable:
- file_option: go_package
module: buf.build/bufbuild/protovalidate
- file_option: go_package
module: buf.build/rodaine/protogofakeit
override:
- file_option: go_package_prefix
value: buf.build/go/protovalidate/internal/gen
Expand Down
3 changes: 3 additions & 0 deletions buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ deps:
- name: buf.build/bufbuild/protovalidate
commit: 52f32327d4b045a79293a6ad4e7e1236
digest: b5:cbabc98d4b7b7b0447c9b15f68eeb8a7a44ef8516cb386ac5f66e7fd4062cd6723ed3f452ad8c384b851f79e33d26e7f8a94e2b807282b3def1cd966c7eace97
- name: buf.build/rodaine/protogofakeit
commit: 9caf0fc578d3413590962a1764b81b94
digest: b5:eeead7373f2f598ebc8f91aa3a68d6b50630076341d875b22dc6760126bc56c82cf1e98f5a2eff9815ba55fa48ab81745c93a5aeefd5e4697bf43c9ea4694735
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ modules:
- path: proto
deps:
- buf.build/bufbuild/protovalidate:v1.0.0
- buf.build/rodaine/protogofakeit
lint:
use:
- STANDARD
Expand Down
3 changes: 1 addition & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func (c *cache) Build(
return nil, err
}

rulesGlobal := cel.Globals(&variable{Name: "rules", Val: rules.Interface()})
set, err = asts.ReduceResiduals(rulesGlobal)
set, err = asts.ReduceResiduals(rules)
return set, err
}

Expand Down
47 changes: 34 additions & 13 deletions cel/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"slices"
"strconv"
"strings"
"sync"
"unicode/utf8"

"github.com/google/cel-go/cel"
Expand Down Expand Up @@ -49,7 +50,14 @@ var (
// Using this function, you can create a CEL environment that is identical to
// the one used to evaluate protovalidate CEL expressions.
func NewLibrary() cel.Library {
return library{}
return &library{
uniqueScalarPool: sync.Pool{New: func() any {
return map[ref.Val]struct{}{}
}},
uniqueBytesPool: sync.Pool{New: func() any {
return map[string]struct{}{}
}},
}
}

// library is the collection of functions and settings required by protovalidate
Expand All @@ -59,9 +67,12 @@ func NewLibrary() cel.Library {
//
// All implementations of protovalidate MUST implement these functions and
// should avoid exposing additional functions as they will not be portable.
type library struct{}
type library struct {
uniqueScalarPool sync.Pool
uniqueBytesPool sync.Pool
}

func (l library) CompileOptions() []cel.EnvOption { //nolint:funlen,gocyclo
func (l *library) CompileOptions() []cel.EnvOption { //nolint:funlen,gocyclo
return []cel.EnvOption{
cel.TypeDescs(protoregistry.GlobalFiles),
cel.DefaultUTCTimeZone(true),
Expand Down Expand Up @@ -375,15 +386,15 @@ func (l library) CompileOptions() []cel.EnvOption { //nolint:funlen,gocyclo
}
}

func (l library) ProgramOptions() []cel.ProgramOption {
func (l *library) ProgramOptions() []cel.ProgramOption {
return []cel.ProgramOption{
cel.EvalOptions(
cel.OptOptimize,
),
}
}

func (l library) uniqueMemberOverload(itemType *cel.Type, overload func(lister traits.Lister) ref.Val) cel.FunctionOpt {
func (l *library) uniqueMemberOverload(itemType *cel.Type, overload func(lister traits.Lister) ref.Val) cel.FunctionOpt {
return cel.MemberOverload(
itemType.String()+"_unique_bool",
[]*cel.Type{cel.ListType(itemType)},
Expand All @@ -398,15 +409,19 @@ func (l library) uniqueMemberOverload(itemType *cel.Type, overload func(lister t
)
}

func (l library) uniqueScalar(list traits.Lister) ref.Val {
func (l *library) uniqueScalar(list traits.Lister) ref.Val {
size, ok := list.Size().Value().(int64)
if !ok {
return types.UnsupportedRefValConversionErr(list.Size().Value())
}
if size <= 1 {
return types.Bool(true)
}
exist := make(map[ref.Val]struct{}, size)
exist := l.uniqueScalarPool.Get().(map[ref.Val]struct{}) //nolint:errcheck // guaranteed to match
defer func() {
clear(exist)
l.uniqueScalarPool.Put(exist)
}()
for i := range size {
val := list.Get(types.Int(i))
if _, ok := exist[val]; ok {
Expand All @@ -421,24 +436,30 @@ func (l library) uniqueScalar(list traits.Lister) ref.Val {
// compares bytes type CEL values. This function is used instead of uniqueScalar
// as the bytes ([]uint8) type is not hashable in Go; we cheat this by converting
// the value to a string.
func (l library) uniqueBytes(list traits.Lister) ref.Val {
func (l *library) uniqueBytes(list traits.Lister) ref.Val {
size, ok := list.Size().Value().(int64)
if !ok {
return types.UnsupportedRefValConversionErr(list.Size().Value())
}
if size <= 1 {
return types.Bool(true)
}
exist := make(map[any]struct{}, size)
exist := l.uniqueBytesPool.Get().(map[string]struct{}) //nolint:errcheck // guaranteed to match
defer func() {
clear(exist)
l.uniqueBytesPool.Put(exist)
}()
for i := range size {
val := list.Get(types.Int(i)).Value()
if b, ok := val.([]uint8); ok {
val = string(b)
b, ok := val.([]byte)
if !ok {
return types.NewErr("expected bytes, got %v", val)
}
if _, ok := exist[val]; ok {
str := string(b)
if _, ok := exist[str]; ok {
return types.Bool(false)
}
exist[val] = struct{}{}
exist[str] = struct{}{}
}
return types.Bool(true)
}
Expand Down
8 changes: 7 additions & 1 deletion error_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func fieldPath(field protoreflect.FieldDescriptor) *validate.FieldPath {
// path is reversed. Rule paths are generally static, so this optimization isn't
// applied for rule paths.
func updateViolationPaths(err error, fieldSuffix *validate.FieldPathElement, rulePrefix []*validate.FieldPathElement) {
if fieldSuffix == nil && len(rulePrefix) == 0 {
if err == nil || (fieldSuffix == nil && len(rulePrefix) == 0) {
return
}
var valErr *ValidationError
Expand All @@ -117,6 +117,9 @@ func updateViolationPaths(err error, fieldSuffix *validate.FieldPathElement, rul
// finalizeViolationPaths reverses all field paths in the error and populates
// the deprecated string-based field path.
func finalizeViolationPaths(err error) {
if err == nil {
return
}
var valErr *ValidationError
if errors.As(err, &valErr) {
for _, violation := range valErr.Violations {
Expand Down Expand Up @@ -161,6 +164,9 @@ func FieldPathString(path *validate.FieldPath) string {
// markViolationForKey marks the provided error as being for a map key, by
// setting the `for_key` flag on each violation within the validation error.
func markViolationForKey(err error) {
if err == nil {
return
}
var valErr *ValidationError
if errors.As(err, &valErr) {
for _, violation := range valErr.Violations {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.24.0
require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1
buf.build/go/hyperpb v0.1.3
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/google/cel-go v0.26.1
github.com/rodaine/protogofakeit v0.1.1
github.com/stretchr/testify v1.11.1
google.golang.org/protobuf v1.36.10
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -30,6 +32,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/protocolbuffers/protoscope v0.0.0-20221109213918-8e7a6aafa2c9 h1:arwj11zP0yJIxIRiDn22E0H8PxfF7TsTrc2wIPFIsf4=
github.com/protocolbuffers/protoscope v0.0.0-20221109213918-8e7a6aafa2c9/go.mod h1:SKZx6stCn03JN3BOWTwvVIO2ajMkb/zQdTceXYhKw/4=
github.com/rodaine/protogofakeit v0.1.1 h1:ZKouljuRM3A+TArppfBqnH8tGZHOwM/pjvtXe9DaXH8=
github.com/rodaine/protogofakeit v0.1.1/go.mod h1:pXn/AstBYMaSfc1/RqH3N82pBuxtWgejz1AlYpY1mI0=
github.com/stoewer/go-strcase v1.3.1 h1:iS0MdW+kVTxgMoE1LAZyMiYJFKlOzLooE4MxjirtkAs=
github.com/stoewer/go-strcase v1.3.1/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Loading