Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DefaultResolver public #59

Merged
merged 2 commits into from
Oct 3, 2023
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
6 changes: 6 additions & 0 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type Builder struct {
Load func(desc protoreflect.MessageDescriptor) MessageEvaluator
}

type StandardConstraintResolver interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was from internal/evaluator/resolver.go, but the rest of that file is moved out of internal.

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,
Expand Down
3 changes: 2 additions & 1 deletion internal/evaluator/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/bufbuild/protovalidate-go/celext"
pb "github.com/bufbuild/protovalidate-go/internal/gen/tests/example/v1"
"github.com/bufbuild/protovalidate-go/resolver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
Expand All @@ -32,7 +33,7 @@ func TestBuildCache(t *testing.T) {
env, err := celext.DefaultEnv(true)
require.NoError(t, err, "failed to construct CEL environment")
bldr := NewBuilder(
env, false, DefaultResolver{},
env, false, resolver.DefaultResolver{},
)
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
Expand Down
15 changes: 8 additions & 7 deletions internal/evaluator/resolver.go → resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package evaluator
package resolver

import (
"strings"
Expand All @@ -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
3 changes: 2 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/bufbuild/protovalidate-go/celext"
"github.com/bufbuild/protovalidate-go/internal/errors"
"github.com/bufbuild/protovalidate-go/internal/evaluator"
"github.com/bufbuild/protovalidate-go/resolver"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ type Validator struct {
// up the CEL execution environment if the configuration is invalid. See the
// individual ValidatorOption for how they impact the fallibility of New.
func New(options ...ValidatorOption) (*Validator, error) {
cfg := config{resolver: evaluator.DefaultResolver{}}
cfg := config{resolver: resolver.DefaultResolver{}}
for _, opt := range options {
opt(&cfg)
}
Expand Down