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

Add support for #validate=true to message refs #2633

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions private/buf/buffetch/buffetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (

useProtoNamesKey = "use_proto_names"
useEnumNumbersKey = "use_enum_numbers"
validateKey = "validate"
)

var (
Expand Down Expand Up @@ -110,10 +111,16 @@ type MessageRef interface {
//
// May be used for items such as YAML unmarshaling errors.
Path() string
// UseProtoNames only applies for MessageEncodingYAML at this time.
// UseProtoNames indicates if the message should use proto names when encoding.
//
// Only applies for MessageEncodingYAML at this time.
UseProtoNames() bool
// UseEnumNumbers only applies for MessageEncodingYAML at this time.
// UseEnumNumbers indicates if the message should use enum numbers when encoding.
//
// Only applies for MessageEncodingYAML at this time.
UseEnumNumbers() bool
// Validate indicates if the message should be validated when decoding.
Validate() bool
IsNull() bool
internalSingleRef() internal.SingleRef
}
Expand Down
10 changes: 10 additions & 0 deletions private/buf/buffetch/message_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type messageRef struct {
singleRef internal.SingleRef
useProtoNames bool
useEnumNumbers bool
validate bool
messageEncoding MessageEncoding
}

Expand All @@ -40,10 +41,15 @@ func newMessageRef(
if err != nil {
return nil, err
}
validate, err := getTrueOrFalseForSingleRef(singleRef, validateKey)
if err != nil {
return nil, err
}
return &messageRef{
singleRef: singleRef,
useProtoNames: useProtoNames,
useEnumNumbers: useEnumNumbers,
validate: validate,
messageEncoding: messageEncoding,
}, nil
}
Expand All @@ -68,6 +74,10 @@ func (r *messageRef) UseEnumNumbers() bool {
return r.useEnumNumbers
}

func (r *messageRef) Validate() bool {
return r.validate
}

func (r *messageRef) IsNull() bool {
return r.singleRef.FileScheme() == internal.FileSchemeNull
}
Expand Down
33 changes: 27 additions & 6 deletions private/buf/buffetch/ref_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,29 @@ func newRefParser(logger *zap.Logger) *refParser {
fetchRefParser: internal.NewRefParser(
logger,
internal.WithRawRefProcessor(processRawRef),
internal.WithSingleFormat(formatBin),
internal.WithSingleFormat(formatBinpb),
internal.WithSingleFormat(
formatBin,
internal.WithSingleCustomOptionKey(validateKey),
Copy link
Member

Choose a reason for hiding this comment

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

Validate shouldn't be on anything except yaml right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can validate any message, after it has been decoded. protoyaml just provide better errors if you give it the validator directly.

Copy link
Member

Choose a reason for hiding this comment

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

got it

),
internal.WithSingleFormat(
formatBinpb,
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatJSON,
internal.WithSingleCustomOptionKey(useProtoNamesKey),
internal.WithSingleCustomOptionKey(useEnumNumbersKey),
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatTxtpb,
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(formatTxtpb),
internal.WithSingleFormat(
formatYAML,
internal.WithSingleCustomOptionKey(useProtoNamesKey),
internal.WithSingleCustomOptionKey(useEnumNumbersKey),
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatBingz,
Expand Down Expand Up @@ -107,18 +118,28 @@ func newMessageRefParser(logger *zap.Logger, options ...MessageRefParserOption)
fetchRefParser: internal.NewRefParser(
logger,
internal.WithRawRefProcessor(newProcessRawRefMessage(messageRefParserOptions.defaultMessageEncoding)),
internal.WithSingleFormat(formatBin),
internal.WithSingleFormat(formatBinpb),
internal.WithSingleFormat(
formatBin,
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatBinpb,
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatJSON,
internal.WithSingleCustomOptionKey(useProtoNamesKey),
internal.WithSingleCustomOptionKey(useEnumNumbersKey),
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(formatTxtpb,
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(formatTxtpb),
internal.WithSingleFormat(
formatYAML,
internal.WithSingleCustomOptionKey(useProtoNamesKey),
internal.WithSingleCustomOptionKey(useEnumNumbersKey),
internal.WithSingleCustomOptionKey(validateKey),
),
internal.WithSingleFormat(
formatBingz,
Expand Down
17 changes: 17 additions & 0 deletions private/buf/bufwire/proto_encoding_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/bufbuild/buf/private/bufpkg/bufreflect"
"github.com/bufbuild/buf/private/pkg/app"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/protovalidate-go"
"github.com/bufbuild/protoyaml-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.uber.org/multierr"
Expand Down Expand Up @@ -71,6 +73,14 @@ func (p *protoEncodingReader) GetMessage(
if err != nil {
return nil, err
}
var validator protoyaml.Validator
if messageRef.Validate() {
var err error
validator, err = protovalidate.New()
if err != nil {
return nil, err
}
}
var unmarshaler protoencoding.Unmarshaler
switch messageRef.MessageEncoding() {
case buffetch.MessageEncodingBinpb:
Expand All @@ -83,7 +93,9 @@ func (p *protoEncodingReader) GetMessage(
unmarshaler = protoencoding.NewYAMLUnmarshaler(
resolver,
protoencoding.YAMLUnmarshalerWithPath(messageRef.Path()),
protoencoding.YAMLUnmarshalerWithValidator(validator),
)
validator = nil // Validation errors are handled by the unmarshaler.
default:
return nil, errors.New("unknown message encoding type")
}
Expand All @@ -108,5 +120,10 @@ func (p *protoEncodingReader) GetMessage(
if err := unmarshaler.Unmarshal(data, message); err != nil {
return nil, fmt.Errorf("unable to unmarshal the message: %v", err)
}
if validator != nil {
if err := validator.Validate(message); err != nil {
return nil, err
}
}
return message, nil
}
6 changes: 2 additions & 4 deletions private/buf/cmd/buf/command/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,10 @@ func run(
}
fromMessageRef, err := buffetch.NewMessageRefParser(
container.Logger(),
buffetch.MessageRefParserWithDefaultMessageEncoding(
buffetch.MessageEncodingBinpb,
),
buffetch.MessageRefParserWithDefaultMessageEncoding(buffetch.MessageEncodingBinpb),
).GetMessageRef(ctx, flags.From)
if err != nil {
return fmt.Errorf("--%s: %v", outputFlagName, err)
return fmt.Errorf("--%s: %v", fromFlagName, err)
}
storageosProvider := bufcli.NewStorageosProvider(flags.DisableSymlinks)
runner := command.NewRunner()
Expand Down