Przeglądaj źródła

protoc-gen-go: generate per-field comments for oneofs

Comment location and formatting matches previous generator.

Change-Id: Id51489bb8ca6e6bbd2c7bc2129a7bc170e56ed44
Reviewed-on: https://go-review.googlesource.com/c/140617
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Damien Neil 7 lat temu
rodzic
commit
09f89d0246

+ 1 - 0
cmd/protoc-gen-go/internal_gengo/oneof.go

@@ -20,6 +20,7 @@ func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
 	}
 	g.P("// Types that are valid to be assigned to ", oneof.GoName, ":")
 	for _, field := range oneof.Fields {
+		genComment(g, f, field.Path)
 		g.P("//\t*", fieldOneofType(field))
 	}
 	g.P(oneof.GoName, " ", oneofInterfaceName(message, oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")

+ 104 - 11
cmd/protoc-gen-go/testdata/comments/comments.pb.go

@@ -24,9 +24,17 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
 
 // COMMENT: Message1
 type Message1 struct {
-	XXX_NoUnkeyedLiteral struct{} `json:"-"`
-	XXX_unrecognized     []byte   `json:"-"`
-	XXX_sizecache        int32    `json:"-"`
+	// COMMENT: Field1A
+	Field1A *string `protobuf:"bytes,1,opt,name=Field1A" json:"Field1A,omitempty"`
+	// COMMENT: Oneof1A
+	//
+	// Types that are valid to be assigned to Oneof1A:
+	// COMMENT: Oneof1AField1
+	//	*Message1_Oneof1AField1
+	Oneof1A              isMessage1_Oneof1A `protobuf_oneof:"Oneof1a"`
+	XXX_NoUnkeyedLiteral struct{}           `json:"-"`
+	XXX_unrecognized     []byte             `json:"-"`
+	XXX_sizecache        int32              `json:"-"`
 }
 
 func (m *Message1) Reset()         { *m = Message1{} }
@@ -54,6 +62,88 @@ func (m *Message1) XXX_DiscardUnknown() {
 
 var xxx_messageInfo_Message1 proto.InternalMessageInfo
 
+func (m *Message1) GetField1A() string {
+	if m != nil && m.Field1A != nil {
+		return *m.Field1A
+	}
+	return ""
+}
+
+type isMessage1_Oneof1A interface {
+	isMessage1_Oneof1A()
+}
+
+type Message1_Oneof1AField1 struct {
+	Oneof1AField1 string `protobuf:"bytes,2,opt,name=Oneof1AField1,oneof"`
+}
+
+func (*Message1_Oneof1AField1) isMessage1_Oneof1A() {}
+
+func (m *Message1) GetOneof1A() isMessage1_Oneof1A {
+	if m != nil {
+		return m.Oneof1A
+	}
+	return nil
+}
+
+func (m *Message1) GetOneof1AField1() string {
+	if x, ok := m.GetOneof1A().(*Message1_Oneof1AField1); ok {
+		return x.Oneof1AField1
+	}
+	return ""
+}
+
+// XXX_OneofFuncs is for the internal use of the proto package.
+func (*Message1) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error, func(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error), func(msg proto.Message) (n int), []interface{}) {
+	return _Message1_OneofMarshaler, _Message1_OneofUnmarshaler, _Message1_OneofSizer, []interface{}{
+		(*Message1_Oneof1AField1)(nil),
+	}
+}
+
+func _Message1_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
+	m := msg.(*Message1)
+	// Oneof1a
+	switch x := m.Oneof1A.(type) {
+	case *Message1_Oneof1AField1:
+		b.EncodeVarint(2<<3 | proto.WireBytes)
+		b.EncodeStringBytes(x.Oneof1AField1)
+	case nil:
+	default:
+		return fmt.Errorf("Message1.Oneof1A has unexpected type %T", x)
+	}
+	return nil
+}
+
+func _Message1_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
+	m := msg.(*Message1)
+	switch tag {
+	case 2: // Oneof1a.Oneof1AField1
+		if wire != proto.WireBytes {
+			return true, proto.ErrInternalBadWireType
+		}
+		x, err := b.DecodeStringBytes()
+		m.Oneof1A = &Message1_Oneof1AField1{x}
+		return true, err
+	default:
+		return false, nil
+	}
+}
+
+func _Message1_OneofSizer(msg proto.Message) (n int) {
+	m := msg.(*Message1)
+	// Oneof1a
+	switch x := m.Oneof1A.(type) {
+	case *Message1_Oneof1AField1:
+		n += 1 // tag and wire
+		n += proto.SizeVarint(uint64(len(x.Oneof1AField1)))
+		n += len(x.Oneof1AField1)
+	case nil:
+	default:
+		panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
+	}
+	return n
+}
+
 // COMMENT: Message1A
 type Message1_Message1A struct {
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
@@ -226,14 +316,17 @@ func init() {
 func init() { proto.RegisterFile("comments/comments.proto", fileDescriptor_885e8293f1fab554) }
 
 var fileDescriptor_885e8293f1fab554 = []byte{
-	// 139 bytes of a gzipped FileDescriptorProto
+	// 191 bytes of a gzipped FileDescriptorProto
 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4f, 0xce, 0xcf, 0xcd,
 	0x4d, 0xcd, 0x2b, 0x29, 0xd6, 0x87, 0x31, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0xc4, 0xd3,
-	0xf3, 0xc1, 0x0c, 0x08, 0x37, 0x59, 0x0f, 0x26, 0xad, 0xa4, 0xc2, 0xc5, 0xe1, 0x9b, 0x5a, 0x5c,
-	0x9c, 0x98, 0x9e, 0x6a, 0x28, 0xc5, 0xcd, 0xc5, 0x09, 0x63, 0x3b, 0x22, 0x73, 0x9c, 0x90, 0x54,
-	0x19, 0x21, 0x49, 0x18, 0x21, 0xab, 0x32, 0x72, 0x72, 0x72, 0x8e, 0x72, 0x4c, 0xcf, 0x2c, 0xc9,
-	0x28, 0x4d, 0x02, 0x19, 0xaf, 0x9f, 0x9e, 0x9f, 0x93, 0x98, 0x97, 0xae, 0x0f, 0xb6, 0x30, 0xa9,
-	0x34, 0x4d, 0xbf, 0xcc, 0x48, 0x3f, 0x39, 0x37, 0x05, 0xc2, 0x4f, 0xd6, 0x4d, 0x4f, 0xcd, 0xd3,
-	0x4d, 0xcf, 0xd7, 0x2f, 0x49, 0x2d, 0x2e, 0x49, 0x49, 0x2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comments/comments.proto

@@ -18,6 +18,15 @@ message Message1 {
   // COMMENT: Message1B
   message Message1B {
   }
+
+  // COMMENT: Field1A
+  optional string Field1A = 1;
+
+  // COMMENT: Oneof1A
+  oneof Oneof1a {
+    // COMMENT: Oneof1AField1
+    string Oneof1AField1 = 2;
+  }
 }
 
 // COMMENT: Message2