Skip to content

Commit dd8b737

Browse files
authored
protoprint: fix handling of uninterpreted options (#584)
1 parent 5d00292 commit dd8b737

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

desc/protoprint/print.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,13 @@ func (p *Printer) typeString(fld *desc.FieldDescriptor, scope string) string {
777777
if fld.IsMap() {
778778
return fmt.Sprintf("map<%s, %s>", p.typeString(fld.GetMapKeyType(), scope), p.typeString(fld.GetMapValueType(), scope))
779779
}
780+
fldProto := fld.AsFieldDescriptorProto()
781+
if fldProto.Type == nil && fldProto.TypeName != nil {
782+
// In an unlinked proto, the type may be absent because it is not known
783+
// whether the symbol is a message or an enum. In that case, just return
784+
// the type name.
785+
return fldProto.GetTypeName()
786+
}
780787
switch fld.GetType() {
781788
case descriptorpb.FieldDescriptorProto_TYPE_INT32:
782789
return "int32"
@@ -1976,7 +1983,7 @@ func uninterpretedToOptions(uninterp []*descriptorpb.UninterpretedOption) []opti
19761983
case unint.NegativeIntValue != nil:
19771984
v = unint.GetNegativeIntValue()
19781985
case unint.AggregateValue != nil:
1979-
v = ident(unint.GetAggregateValue())
1986+
v = ident("{ " + unint.GetAggregateValue() + " }")
19801987
}
19811988

19821989
opts[i] = option{name: buf.String(), val: v}

desc/protoprint/print_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,48 @@ service TestService {
185185
fds, err := pa.ParseFiles("test.proto")
186186
testutil.Ok(t, err)
187187

188+
// Sanity check that this resulted in unrecognized options
189+
unk := fds[0].FindSymbol("TestService.Get").(*desc.MethodDescriptor).GetMethodOptions().ProtoReflect().GetUnknown()
190+
testutil.Require(t, len(unk) > 0)
191+
188192
checkFile(t, &Printer{}, fds[0], "test-unrecognized-options.proto")
189193
}
190194

195+
func TestPrintUninterpretedOptions(t *testing.T) {
196+
files := map[string]string{"test.proto": `
197+
syntax = "proto2";
198+
package pkg;
199+
option go_package = "some.pkg";
200+
import "google/protobuf/descriptor.proto";
201+
message Options {
202+
optional bool some_option_value = 1;
203+
}
204+
extend google.protobuf.MessageOptions {
205+
optional Options my_some_option = 11964;
206+
}
207+
message SomeMessage {
208+
option (.pkg.my_some_option) = {some_option_value : true};
209+
}
210+
`}
211+
212+
pa := &protoparse.Parser{
213+
Accessor: protoparse.FileContentsFromMap(files),
214+
}
215+
fds, err := pa.ParseFilesButDoNotLink("test.proto")
216+
testutil.Ok(t, err)
217+
218+
// Sanity check that this resulted in uninterpreted options
219+
unint := fds[0].MessageType[1].Options.UninterpretedOption
220+
testutil.Require(t, len(unint) > 0)
221+
222+
descFd, err := desc.WrapFile((*descriptorpb.FileDescriptorProto)(nil).ProtoReflect().Descriptor().ParentFile())
223+
testutil.Ok(t, err)
224+
fd, err := desc.CreateFileDescriptor(fds[0], descFd)
225+
testutil.Ok(t, err)
226+
227+
checkFile(t, &Printer{}, fd, "test-uninterpreted-options.proto")
228+
}
229+
191230
func TestPrintNonFileDescriptors(t *testing.T) {
192231
pa := protoparse.Parser{ImportPaths: []string{"../../internal/testprotos"}, IncludeSourceCodeInfo: true}
193232
fds, err := pa.ParseFiles("desc_test_comments.proto")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
syntax = "proto2";
2+
3+
package pkg;
4+
5+
import "google/protobuf/descriptor.proto";
6+
7+
option go_package = "some.pkg";
8+
9+
message Options {
10+
optional bool some_option_value = 1;
11+
}
12+
13+
message SomeMessage {
14+
option (.pkg.my_some_option) = { some_option_value : true };
15+
}
16+
17+
extend google.protobuf.MessageOptions {
18+
optional Options my_some_option = 11964;
19+
}

0 commit comments

Comments
 (0)