Skip to content

Commit

Permalink
move interface back, add godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversun9 committed Oct 3, 2023
1 parent ce22182 commit 6b4858b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/bufbuild/protovalidate-go/internal/constraints"
"github.com/bufbuild/protovalidate-go/internal/errors"
"github.com/bufbuild/protovalidate-go/internal/expression"
"github.com/bufbuild/protovalidate-go/resolver"
"github.com/google/cel-go/cel"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
Expand All @@ -35,15 +34,21 @@ type Builder struct {
cache atomic.Pointer[MessageCache] // copy-on-write cache.
env *cel.Env
constraints constraints.Cache
resolver resolver.StandardConstraintResolver
resolver StandardConstraintResolver
Load func(desc protoreflect.MessageDescriptor) MessageEvaluator
}

type StandardConstraintResolver interface {
ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints
ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints
ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints
}

// NewBuilder initializes a new Builder.
func NewBuilder(
env *cel.Env,
disableLazy bool,
res resolver.StandardConstraintResolver,
res StandardConstraintResolver,
seedDesc ...protoreflect.MessageDescriptor,
) *Builder {
bldr := &Builder{
Expand Down
13 changes: 7 additions & 6 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ const (
previousExtensionIndex = "51071"
)

type StandardConstraintResolver interface {
ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints
ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints
ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints
}

// DefaultResolver resolves protovalidate constraints options from descriptors.
type DefaultResolver struct{}

// ResolveMessageConstraints returns the MessageConstraints option set for the
// MessageDescriptor.
func (r DefaultResolver) ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints {
constraints := resolveExt[protoreflect.MessageDescriptor, *validate.MessageConstraints](desc, validate.E_Message)
if constraints == nil {
Expand All @@ -44,6 +41,8 @@ func (r DefaultResolver) ResolveMessageConstraints(desc protoreflect.MessageDesc
return constraints
}

// ResolveOneofConstraints returns the OneofConstraints option set for the
// OneofDescriptor.
func (r DefaultResolver) ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints {
constraints := resolveExt[protoreflect.OneofDescriptor, *validate.OneofConstraints](desc, validate.E_Oneof)
if constraints == nil {
Expand All @@ -52,6 +51,8 @@ func (r DefaultResolver) ResolveOneofConstraints(desc protoreflect.OneofDescript
return constraints
}

// ResolveFieldConstraints returns the FieldConstraints option set for the
// FieldDescriptor.
func (r DefaultResolver) ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints {
constraints := resolveExt[protoreflect.FieldDescriptor, *validate.FieldConstraints](desc, validate.E_Field)
if constraints == nil {
Expand Down

0 comments on commit 6b4858b

Please sign in to comment.