Skip to content

Commit 05525ac

Browse files
authored
Make DefaultResolver public (#59)
Make DefaultResolver public so that `buf lint` can use it to resolve `MessageConstraints` and `FieldConstraints`.
1 parent 9023bd7 commit 05525ac

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

internal/evaluator/builder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ type Builder struct {
3838
Load func(desc protoreflect.MessageDescriptor) MessageEvaluator
3939
}
4040

41+
type StandardConstraintResolver interface {
42+
ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints
43+
ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints
44+
ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints
45+
}
46+
4147
// NewBuilder initializes a new Builder.
4248
func NewBuilder(
4349
env *cel.Env,

internal/evaluator/builder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/bufbuild/protovalidate-go/celext"
2222
pb "github.com/bufbuild/protovalidate-go/internal/gen/tests/example/v1"
23+
"github.com/bufbuild/protovalidate-go/resolver"
2324
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/require"
2526
"google.golang.org/protobuf/proto"
@@ -32,7 +33,7 @@ func TestBuildCache(t *testing.T) {
3233
env, err := celext.DefaultEnv(true)
3334
require.NoError(t, err, "failed to construct CEL environment")
3435
bldr := NewBuilder(
35-
env, false, DefaultResolver{},
36+
env, false, resolver.DefaultResolver{},
3637
)
3738
wg := sync.WaitGroup{}
3839
for i := 0; i < 100; i++ {

internal/evaluator/resolver.go renamed to resolver/resolver.go

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

15-
package evaluator
15+
package resolver
1616

1717
import (
1818
"strings"
@@ -28,14 +28,11 @@ const (
2828
previousExtensionIndex = "51071"
2929
)
3030

31-
type StandardConstraintResolver interface {
32-
ResolveMessageConstraints(desc protoreflect.MessageDescriptor) *validate.MessageConstraints
33-
ResolveOneofConstraints(desc protoreflect.OneofDescriptor) *validate.OneofConstraints
34-
ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints
35-
}
36-
31+
// DefaultResolver resolves protovalidate constraints options from descriptors.
3732
type DefaultResolver struct{}
3833

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

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

54+
// ResolveFieldConstraints returns the FieldConstraints option set for the
55+
// FieldDescriptor.
5556
func (r DefaultResolver) ResolveFieldConstraints(desc protoreflect.FieldDescriptor) *validate.FieldConstraints {
5657
constraints := resolveExt[protoreflect.FieldDescriptor, *validate.FieldConstraints](desc, validate.E_Field)
5758
if constraints == nil {

validator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/bufbuild/protovalidate-go/celext"
2222
"github.com/bufbuild/protovalidate-go/internal/errors"
2323
"github.com/bufbuild/protovalidate-go/internal/evaluator"
24+
"github.com/bufbuild/protovalidate-go/resolver"
2425
"google.golang.org/protobuf/proto"
2526
"google.golang.org/protobuf/reflect/protoreflect"
2627
)
@@ -58,7 +59,7 @@ type Validator struct {
5859
// up the CEL execution environment if the configuration is invalid. See the
5960
// individual ValidatorOption for how they impact the fallibility of New.
6061
func New(options ...ValidatorOption) (*Validator, error) {
61-
cfg := config{resolver: evaluator.DefaultResolver{}}
62+
cfg := config{resolver: resolver.DefaultResolver{}}
6263
for _, opt := range options {
6364
opt(&cfg)
6465
}

0 commit comments

Comments
 (0)