Skip to content

Commit

Permalink
protoprint: fix handling of uninterpreted options (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhump authored Nov 3, 2023
1 parent 5d00292 commit dd8b737
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
9 changes: 8 additions & 1 deletion desc/protoprint/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,13 @@ func (p *Printer) typeString(fld *desc.FieldDescriptor, scope string) string {
if fld.IsMap() {
return fmt.Sprintf("map<%s, %s>", p.typeString(fld.GetMapKeyType(), scope), p.typeString(fld.GetMapValueType(), scope))
}
fldProto := fld.AsFieldDescriptorProto()
if fldProto.Type == nil && fldProto.TypeName != nil {
// In an unlinked proto, the type may be absent because it is not known
// whether the symbol is a message or an enum. In that case, just return
// the type name.
return fldProto.GetTypeName()
}
switch fld.GetType() {
case descriptorpb.FieldDescriptorProto_TYPE_INT32:
return "int32"
Expand Down Expand Up @@ -1976,7 +1983,7 @@ func uninterpretedToOptions(uninterp []*descriptorpb.UninterpretedOption) []opti
case unint.NegativeIntValue != nil:
v = unint.GetNegativeIntValue()
case unint.AggregateValue != nil:
v = ident(unint.GetAggregateValue())
v = ident("{ " + unint.GetAggregateValue() + " }")
}

opts[i] = option{name: buf.String(), val: v}
Expand Down
39 changes: 39 additions & 0 deletions desc/protoprint/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,48 @@ service TestService {
fds, err := pa.ParseFiles("test.proto")
testutil.Ok(t, err)

// Sanity check that this resulted in unrecognized options
unk := fds[0].FindSymbol("TestService.Get").(*desc.MethodDescriptor).GetMethodOptions().ProtoReflect().GetUnknown()
testutil.Require(t, len(unk) > 0)

checkFile(t, &Printer{}, fds[0], "test-unrecognized-options.proto")
}

func TestPrintUninterpretedOptions(t *testing.T) {
files := map[string]string{"test.proto": `
syntax = "proto2";
package pkg;
option go_package = "some.pkg";
import "google/protobuf/descriptor.proto";
message Options {
optional bool some_option_value = 1;
}
extend google.protobuf.MessageOptions {
optional Options my_some_option = 11964;
}
message SomeMessage {
option (.pkg.my_some_option) = {some_option_value : true};
}
`}

pa := &protoparse.Parser{
Accessor: protoparse.FileContentsFromMap(files),
}
fds, err := pa.ParseFilesButDoNotLink("test.proto")
testutil.Ok(t, err)

// Sanity check that this resulted in uninterpreted options
unint := fds[0].MessageType[1].Options.UninterpretedOption
testutil.Require(t, len(unint) > 0)

descFd, err := desc.WrapFile((*descriptorpb.FileDescriptorProto)(nil).ProtoReflect().Descriptor().ParentFile())
testutil.Ok(t, err)
fd, err := desc.CreateFileDescriptor(fds[0], descFd)
testutil.Ok(t, err)

checkFile(t, &Printer{}, fd, "test-uninterpreted-options.proto")
}

func TestPrintNonFileDescriptors(t *testing.T) {
pa := protoparse.Parser{ImportPaths: []string{"../../internal/testprotos"}, IncludeSourceCodeInfo: true}
fds, err := pa.ParseFiles("desc_test_comments.proto")
Expand Down
19 changes: 19 additions & 0 deletions desc/protoprint/testfiles/test-uninterpreted-options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto2";

package pkg;

import "google/protobuf/descriptor.proto";

option go_package = "some.pkg";

message Options {
optional bool some_option_value = 1;
}

message SomeMessage {
option (.pkg.my_some_option) = { some_option_value : true };
}

extend google.protobuf.MessageOptions {
optional Options my_some_option = 11964;
}

0 comments on commit dd8b737

Please sign in to comment.