Skip to content

Commit

Permalink
Make DefaultResolver public (#59)
Browse files Browse the repository at this point in the history
Make DefaultResolver public so that `buf lint` can use it to resolve
`MessageConstraints` and `FieldConstraints`.
  • Loading branch information
oliversun9 authored Oct 3, 2023
1 parent 9023bd7 commit 05525ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
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 {
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

0 comments on commit 05525ac

Please sign in to comment.