Skip to content

Commit

Permalink
Address last todo
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable committed Sep 27, 2024
1 parent e1bb066 commit 2bdb9ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func checkPredefinedRuleExtension(
if validate.File_buf_validate_validate_proto.Messages().ByName(extendedRuleFullName.Name()) == nil {
return nil
}
predefinedConstraints := resolveExt[*validate.PredefinedConstraints](extensionDescriptor.Options(), validate.E_Predefined, extensionResolver)
predefinedConstraints, err := resolveExt[*validate.PredefinedConstraints](extensionDescriptor.Options(), validate.E_Predefined, extensionResolver)
if err != nil {
return err
}
if predefinedConstraints == nil {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package buflintvalidate

import (
"fmt"

"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/protovalidate-go"
Expand Down Expand Up @@ -46,14 +48,14 @@ func (r *constraintsResolverForTargetField) ResolveOneofConstraints(desc protore
}

// This function is copied directly from protovalidate-go, except refactored to use protoencoding
// for marshalling and unmarshalling.
// for marshalling and unmarshalling. We also added error handling for marshal/unmarshal.
//
// This resolves the given extension and returns the constraints for the extension.
func resolveExt[C proto.Message](
options proto.Message,
extType protoreflect.ExtensionType,
resolver protoencoding.Resolver,
) (constraints C) {
) (constraints C, retErr error) {
num := extType.TypeDescriptor().Number()
var msg proto.Message

Expand All @@ -66,16 +68,20 @@ func resolveExt[C proto.Message](
})

if msg == nil {
return constraints
return constraints, nil
} else if m, ok := msg.(C); ok {
return m
return m, nil
}

constraints, _ = constraints.ProtoReflect().New().Interface().(C)
// TODO: handle marhsal/unmarshaling errors, wtf.
b, _ := protoencoding.NewWireMarshaler().Marshal(msg)
_ = protoencoding.NewWireUnmarshaler(resolver).Unmarshal(b, constraints)
return constraints
var ok bool
constraints, ok = constraints.ProtoReflect().New().Interface().(C)
if !ok {
return constraints, fmt.Errorf("unexpected type for constraints %T", constraints)
}
b, err := protoencoding.NewWireMarshaler().Marshal(msg)
if err != nil {
return constraints, err
}
return constraints, protoencoding.NewWireUnmarshaler(resolver).Unmarshal(b, constraints)
}

func celTypeForStandardRuleMessageDescriptor(
Expand Down

0 comments on commit 2bdb9ff

Please sign in to comment.