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 constraint resolution more flexible to different concrete extension types #57

Merged
merged 2 commits into from
Oct 2, 2023
Merged
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
41 changes: 13 additions & 28 deletions internal/evaluator/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
)

Expand Down Expand Up @@ -70,39 +69,25 @@ func resolveExt[D protoreflect.Descriptor, C proto.Message](
desc D,
extType protoreflect.ExtensionType,
) (constraints C) {
opts := desc.Options().ProtoReflect()
fDesc := extType.TypeDescriptor()

if opts.Has(fDesc) {
msg := opts.Get(fDesc).Message().Interface()
if m, ok := msg.(C); ok {
return m
num := extType.TypeDescriptor().Number()
var msg proto.Message
proto.RangeExtensions(desc.Options(), func(typ protoreflect.ExtensionType, i interface{}) bool {
if num != typ.TypeDescriptor().Number() {
return true
}
}

unknown := opts.GetUnknown()
if len(unknown) == 0 {
return constraints
}
msg, _ = i.(proto.Message)
return false
})

opts = opts.Type().New()
var resolver protoregistry.Types
if err := resolver.RegisterExtension(extType); err != nil {
if msg == nil {
return constraints
}
_ = proto.UnmarshalOptions{Resolver: &resolver}.Unmarshal(unknown, opts.Interface())

if opts.Has(fDesc) {
msg := opts.Get(fDesc).Message().Interface()
if m, ok := msg.(C); ok {
return m
}
}
msg := opts.Get(fDesc).Message().Interface()
if m, ok := msg.(C); ok {
} else if m, ok := msg.(C); ok {
return m
}

constraints, _ = constraints.ProtoReflect().New().Interface().(C)
b, _ := proto.Marshal(msg)
_ = proto.Unmarshal(b, constraints)
Comment on lines +89 to +90
Copy link
Member

@jhump jhump Oct 2, 2023

Choose a reason for hiding this comment

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

It would be really nice to wire up the errors through the call stack. I think if you are asked to validate a message whose options has a completely incompatible extension with the same number as protovalidate's extension(s), an error is suitable (instead of silently eating it).

I assume a completely empty message is the same as no-op (no validation)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, but it'll need to be made as a breaking change (the interface here is exposed indirectly in the public API). Will open an issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

See #58

return constraints
}

Expand Down