mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-05 06:39:58 +00:00
cmd/protoc-gen-go: generate XXX_OneofWrappers instead of XXX_OneofFuncs
The marshaler, unmarshaler, and sizer functions are unused ever since the underlying implementation was switched to be table-driven. Change the function to only return the wrapper structs. This change: * enables generated protos to drop dependencies on certain proto types * reduces the size of generated protos * simplifies the implementation of oneofs in protoc-gen-go Updates #708 Change-Id: I845c9009bc0236d1b51d34b014dc3e184303c0f2 Reviewed-on: https://go-review.googlesource.com/c/151357 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
f6d4a4215f
commit
d7e97bc71b
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
func init() { proto.RegisterFile("grpc/deprecation.proto", fileDescriptor_1e7146702b7fe8c5) }
|
||||
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Request struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -27,7 +27,7 @@ import (
|
||||
// It is incremented whenever an incompatibility between the generated code and
|
||||
// proto package is introduced; the generated code references
|
||||
// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
|
||||
const generatedCodeVersion = 2
|
||||
const generatedCodeVersion = 3
|
||||
|
||||
const (
|
||||
fmtPackage = protogen.GoImportPath("fmt")
|
||||
@ -563,7 +563,7 @@ func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, me
|
||||
}
|
||||
|
||||
if len(message.Oneofs) > 0 {
|
||||
genOneofFuncs(gen, g, f, message)
|
||||
genOneofWrappers(gen, g, f, message)
|
||||
}
|
||||
for _, extension := range message.Extensions {
|
||||
genExtension(gen, g, f, extension)
|
||||
@ -832,3 +832,116 @@ var wellKnownTypes = map[protoreflect.FullName]bool{
|
||||
"google.protobuf.UInt64Value": true,
|
||||
"google.protobuf.Value": true,
|
||||
}
|
||||
|
||||
// genOneofField generates the struct field for a oneof.
|
||||
func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
||||
if g.PrintLeadingComments(oneof.Location) {
|
||||
g.P("//")
|
||||
}
|
||||
g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":")
|
||||
for _, field := range oneof.Fields {
|
||||
g.PrintLeadingComments(field.Location)
|
||||
g.P("//\t*", fieldOneofType(field))
|
||||
}
|
||||
g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location)
|
||||
g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")
|
||||
}
|
||||
|
||||
// genOneofTypes generates the interface type used for a oneof field,
|
||||
// and the wrapper types that satisfy that interface.
|
||||
//
|
||||
// It also generates the getter method for the parent oneof field
|
||||
// (but not the member fields).
|
||||
func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
||||
ifName := oneofInterfaceName(oneof)
|
||||
g.P("type ", ifName, " interface {")
|
||||
g.P(ifName, "()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
for _, field := range oneof.Fields {
|
||||
name := fieldOneofType(field)
|
||||
g.Annotate(name.GoName, field.Location)
|
||||
g.Annotate(name.GoName+"."+field.GoName, field.Location)
|
||||
g.P("type ", name, " struct {")
|
||||
goType, _ := fieldGoType(g, field)
|
||||
tags := []string{
|
||||
fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
|
||||
}
|
||||
g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
for _, field := range oneof.Fields {
|
||||
g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}")
|
||||
g.P()
|
||||
}
|
||||
g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
|
||||
g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {")
|
||||
g.P("if m != nil {")
|
||||
g.P("return m.", oneofFieldName(oneof))
|
||||
g.P("}")
|
||||
g.P("return nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
// oneofFieldName returns the name of the struct field holding the oneof value.
|
||||
//
|
||||
// This function is trivial, but pulling out the name like this makes it easier
|
||||
// to experiment with alternative oneof implementations.
|
||||
func oneofFieldName(oneof *protogen.Oneof) string {
|
||||
return oneof.GoName
|
||||
}
|
||||
|
||||
// oneofInterfaceName returns the name of the interface type implemented by
|
||||
// the oneof field value types.
|
||||
func oneofInterfaceName(oneof *protogen.Oneof) string {
|
||||
return fmt.Sprintf("is%s_%s", oneof.ParentMessage.GoIdent.GoName, oneof.GoName)
|
||||
}
|
||||
|
||||
// genOneofWrappers generates the XXX_OneofWrappers method for a message.
|
||||
func genOneofWrappers(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
|
||||
g.P("// XXX_OneofWrappers is for the internal use of the proto package.")
|
||||
g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {")
|
||||
g.P("return []interface{}{")
|
||||
for _, oneof := range message.Oneofs {
|
||||
for _, field := range oneof.Fields {
|
||||
g.P("(*", fieldOneofType(field), ")(nil),")
|
||||
}
|
||||
}
|
||||
g.P("}")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
// fieldOneofType returns the wrapper type used to represent a field in a oneof.
|
||||
func fieldOneofType(field *protogen.Field) protogen.GoIdent {
|
||||
ident := protogen.GoIdent{
|
||||
GoImportPath: field.ParentMessage.GoIdent.GoImportPath,
|
||||
GoName: field.ParentMessage.GoIdent.GoName + "_" + field.GoName,
|
||||
}
|
||||
// Check for collisions with nested messages or enums.
|
||||
//
|
||||
// This conflict resolution is incomplete: Among other things, it
|
||||
// does not consider collisions with other oneof field types.
|
||||
//
|
||||
// TODO: Consider dropping this entirely. Detecting conflicts and
|
||||
// producing an error is almost certainly better than permuting
|
||||
// field and type names in mostly unpredictable ways.
|
||||
Loop:
|
||||
for {
|
||||
for _, message := range field.ParentMessage.Messages {
|
||||
if message.GoIdent == ident {
|
||||
ident.GoName += "_"
|
||||
continue Loop
|
||||
}
|
||||
}
|
||||
for _, enum := range field.ParentMessage.Enums {
|
||||
if enum.GoIdent == ident {
|
||||
ident.GoName += "_"
|
||||
continue Loop
|
||||
}
|
||||
}
|
||||
return ident
|
||||
}
|
||||
}
|
||||
|
@ -1,357 +0,0 @@
|
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package internal_gengo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/v2/protogen"
|
||||
"github.com/golang/protobuf/v2/reflect/protoreflect"
|
||||
)
|
||||
|
||||
// genOneofField generates the struct field for a oneof.
|
||||
func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
||||
if g.PrintLeadingComments(oneof.Location) {
|
||||
g.P("//")
|
||||
}
|
||||
g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":")
|
||||
for _, field := range oneof.Fields {
|
||||
g.PrintLeadingComments(field.Location)
|
||||
g.P("//\t*", fieldOneofType(field))
|
||||
}
|
||||
g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location)
|
||||
g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")
|
||||
}
|
||||
|
||||
// genOneofTypes generates the interface type used for a oneof field,
|
||||
// and the wrapper types that satisfy that interface.
|
||||
//
|
||||
// It also generates the getter method for the parent oneof field
|
||||
// (but not the member fields).
|
||||
func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
||||
ifName := oneofInterfaceName(oneof)
|
||||
g.P("type ", ifName, " interface {")
|
||||
g.P(ifName, "()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
for _, field := range oneof.Fields {
|
||||
name := fieldOneofType(field)
|
||||
g.Annotate(name.GoName, field.Location)
|
||||
g.Annotate(name.GoName+"."+field.GoName, field.Location)
|
||||
g.P("type ", name, " struct {")
|
||||
goType, _ := fieldGoType(g, field)
|
||||
tags := []string{
|
||||
fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
|
||||
}
|
||||
g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
for _, field := range oneof.Fields {
|
||||
g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}")
|
||||
g.P()
|
||||
}
|
||||
g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
|
||||
g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {")
|
||||
g.P("if m != nil {")
|
||||
g.P("return m.", oneofFieldName(oneof))
|
||||
g.P("}")
|
||||
g.P("return nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
// oneofFieldName returns the name of the struct field holding the oneof value.
|
||||
//
|
||||
// This function is trivial, but pulling out the name like this makes it easier
|
||||
// to experiment with alternative oneof implementations.
|
||||
func oneofFieldName(oneof *protogen.Oneof) string {
|
||||
return oneof.GoName
|
||||
}
|
||||
|
||||
// oneofInterfaceName returns the name of the interface type implemented by
|
||||
// the oneof field value types.
|
||||
func oneofInterfaceName(oneof *protogen.Oneof) string {
|
||||
return fmt.Sprintf("is%s_%s", oneof.ParentMessage.GoIdent.GoName, oneof.GoName)
|
||||
}
|
||||
|
||||
// genOneofFuncs generates the XXX_OneofFuncs method for a message.
|
||||
func genOneofFuncs(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
|
||||
protoMessage := g.QualifiedGoIdent(protoPackage.Ident("Message"))
|
||||
protoBuffer := g.QualifiedGoIdent(protoPackage.Ident("Buffer"))
|
||||
encFunc := "_" + message.GoIdent.GoName + "_OneofMarshaler"
|
||||
decFunc := "_" + message.GoIdent.GoName + "_OneofUnmarshaler"
|
||||
sizeFunc := "_" + message.GoIdent.GoName + "_OneofSizer"
|
||||
encSig := "(msg " + protoMessage + ", b *" + protoBuffer + ") error"
|
||||
decSig := "(msg " + protoMessage + ", tag, wire int, b *" + protoBuffer + ") (bool, error)"
|
||||
sizeSig := "(msg " + protoMessage + ") (n int)"
|
||||
|
||||
// XXX_OneofFuncs
|
||||
g.P("// XXX_OneofFuncs is for the internal use of the proto package.")
|
||||
g.P("func (*", message.GoIdent.GoName, ") XXX_OneofFuncs() (func ", encSig, ", func ", decSig, ", func ", sizeSig, ", []interface{}) {")
|
||||
g.P("return ", encFunc, ", ", decFunc, ", ", sizeFunc, ", []interface{}{")
|
||||
for _, oneof := range message.Oneofs {
|
||||
for _, field := range oneof.Fields {
|
||||
g.P("(*", fieldOneofType(field), ")(nil),")
|
||||
}
|
||||
}
|
||||
g.P("}")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// Marshaler
|
||||
g.P("func ", encFunc, encSig, " {")
|
||||
g.P("m := msg.(*", message.GoIdent, ")")
|
||||
for _, oneof := range message.Oneofs {
|
||||
g.P("// ", oneof.Desc.Name())
|
||||
g.P("switch x := m.", oneofFieldName(oneof), ".(type) {")
|
||||
for _, field := range oneof.Fields {
|
||||
genOneofFieldMarshal(g, field)
|
||||
}
|
||||
g.P("case nil:")
|
||||
g.P("default:")
|
||||
g.P("return ", fmtPackage.Ident("Errorf"), `("`, message.GoIdent.GoName, ".", oneofFieldName(oneof), ` has unexpected type %T", x)`)
|
||||
g.P("}")
|
||||
}
|
||||
g.P("return nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// Unmarshaler
|
||||
g.P("func ", decFunc, decSig, " {")
|
||||
g.P("m := msg.(*", message.GoIdent, ")")
|
||||
g.P("switch tag {")
|
||||
for _, oneof := range message.Oneofs {
|
||||
for _, field := range oneof.Fields {
|
||||
genOneofFieldUnmarshal(g, field)
|
||||
}
|
||||
}
|
||||
g.P("default:")
|
||||
g.P("return false, nil")
|
||||
g.P("}")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// Sizer
|
||||
g.P("func ", sizeFunc, sizeSig, " {")
|
||||
g.P("m := msg.(*", message.GoIdent, ")")
|
||||
for _, oneof := range message.Oneofs {
|
||||
g.P("// ", oneof.Desc.Name())
|
||||
g.P("switch x := m.", oneofFieldName(oneof), ".(type) {")
|
||||
for _, field := range oneof.Fields {
|
||||
genOneofFieldSizer(g, field)
|
||||
}
|
||||
g.P("case nil:")
|
||||
g.P("default:")
|
||||
g.P("panic(", fmtPackage.Ident("Sprintf"), `("proto: unexpected type %T in oneof", x))`)
|
||||
g.P("}")
|
||||
}
|
||||
g.P("return n")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
// genOneofFieldMarshal generates the marshal case for a oneof subfield.
|
||||
func genOneofFieldMarshal(g *protogen.GeneratedFile, field *protogen.Field) {
|
||||
g.P("case *", fieldOneofType(field), ":")
|
||||
encodeTag := func(wireType string) {
|
||||
g.P("b.EncodeVarint(", field.Desc.Number(), "<<3|", protoPackage.Ident(wireType), ")")
|
||||
}
|
||||
switch field.Desc.Kind() {
|
||||
case protoreflect.BoolKind:
|
||||
g.P("t := uint64(0)")
|
||||
g.P("if x.", field.GoName, " { t = 1 }")
|
||||
encodeTag("WireVarint")
|
||||
g.P("b.EncodeVarint(t)")
|
||||
case protoreflect.EnumKind, protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
|
||||
encodeTag("WireVarint")
|
||||
g.P("b.EncodeVarint(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.Sint32Kind:
|
||||
encodeTag("WireVarint")
|
||||
g.P("b.EncodeZigzag32(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.Sint64Kind:
|
||||
encodeTag("WireVarint")
|
||||
g.P("b.EncodeZigzag64(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind:
|
||||
encodeTag("WireFixed32")
|
||||
g.P("b.EncodeFixed32(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.FloatKind:
|
||||
encodeTag("WireFixed32")
|
||||
g.P("b.EncodeFixed32(uint64(", mathPackage.Ident("Float32bits"), "(x.", field.GoName, ")))")
|
||||
case protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind:
|
||||
encodeTag("WireFixed64")
|
||||
g.P("b.EncodeFixed64(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.DoubleKind:
|
||||
encodeTag("WireFixed64")
|
||||
g.P("b.EncodeFixed64(", mathPackage.Ident("Float64bits"), "(x.", field.GoName, "))")
|
||||
case protoreflect.StringKind:
|
||||
encodeTag("WireBytes")
|
||||
g.P("b.EncodeStringBytes(x.", field.GoName, ")")
|
||||
case protoreflect.BytesKind:
|
||||
encodeTag("WireBytes")
|
||||
g.P("b.EncodeRawBytes(x.", field.GoName, ")")
|
||||
case protoreflect.MessageKind:
|
||||
encodeTag("WireBytes")
|
||||
g.P("if err := b.EncodeMessage(x.", field.GoName, "); err != nil {")
|
||||
g.P("return err")
|
||||
g.P("}")
|
||||
case protoreflect.GroupKind:
|
||||
encodeTag("WireStartGroup")
|
||||
g.P("if err := b.Marshal(x.", field.GoName, "); err != nil {")
|
||||
g.P("return err")
|
||||
g.P("}")
|
||||
encodeTag("WireEndGroup")
|
||||
}
|
||||
}
|
||||
|
||||
// genOneofFieldUnmarshal generates the unmarshal case for a oneof subfield.
|
||||
func genOneofFieldUnmarshal(g *protogen.GeneratedFile, field *protogen.Field) {
|
||||
oneof := field.OneofType
|
||||
g.P("case ", field.Desc.Number(), ": // ", oneof.Desc.Name(), ".", field.Desc.Name())
|
||||
checkTag := func(wireType string) {
|
||||
g.P("if wire != ", protoPackage.Ident(wireType), " {")
|
||||
g.P("return true, ", protoPackage.Ident("ErrInternalBadWireType"))
|
||||
g.P("}")
|
||||
}
|
||||
switch field.Desc.Kind() {
|
||||
case protoreflect.BoolKind:
|
||||
checkTag("WireVarint")
|
||||
g.P("x, err := b.DecodeVarint()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x != 0}")
|
||||
case protoreflect.EnumKind:
|
||||
checkTag("WireVarint")
|
||||
g.P("x, err := b.DecodeVarint()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", field.EnumType.GoIdent, "(x)}")
|
||||
case protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
|
||||
checkTag("WireVarint")
|
||||
g.P("x, err := b.DecodeVarint()")
|
||||
x := "x"
|
||||
if goType, _ := fieldGoType(g, field); goType != "uint64" {
|
||||
x = goType + "(x)"
|
||||
}
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", x, "}")
|
||||
case protoreflect.Sint32Kind:
|
||||
checkTag("WireVarint")
|
||||
g.P("x, err := b.DecodeZigzag32()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int32(x)}")
|
||||
case protoreflect.Sint64Kind:
|
||||
checkTag("WireVarint")
|
||||
g.P("x, err := b.DecodeZigzag64()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int64(x)}")
|
||||
case protoreflect.Sfixed32Kind:
|
||||
checkTag("WireFixed32")
|
||||
g.P("x, err := b.DecodeFixed32()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int32(x)}")
|
||||
case protoreflect.Fixed32Kind:
|
||||
checkTag("WireFixed32")
|
||||
g.P("x, err := b.DecodeFixed32()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{uint32(x)}")
|
||||
case protoreflect.FloatKind:
|
||||
checkTag("WireFixed32")
|
||||
g.P("x, err := b.DecodeFixed32()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", mathPackage.Ident("Float32frombits"), "(uint32(x))}")
|
||||
case protoreflect.Sfixed64Kind:
|
||||
checkTag("WireFixed64")
|
||||
g.P("x, err := b.DecodeFixed64()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int64(x)}")
|
||||
case protoreflect.Fixed64Kind:
|
||||
checkTag("WireFixed64")
|
||||
g.P("x, err := b.DecodeFixed64()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
||||
case protoreflect.DoubleKind:
|
||||
checkTag("WireFixed64")
|
||||
g.P("x, err := b.DecodeFixed64()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", mathPackage.Ident("Float64frombits"), "(x)}")
|
||||
case protoreflect.StringKind:
|
||||
checkTag("WireBytes")
|
||||
g.P("x, err := b.DecodeStringBytes()")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
||||
case protoreflect.BytesKind:
|
||||
checkTag("WireBytes")
|
||||
g.P("x, err := b.DecodeRawBytes(true)")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
||||
case protoreflect.MessageKind:
|
||||
checkTag("WireBytes")
|
||||
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
||||
g.P("err := b.DecodeMessage(msg)")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{msg}")
|
||||
case protoreflect.GroupKind:
|
||||
checkTag("WireStartGroup")
|
||||
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
||||
g.P("err := b.DecodeGroup(msg)")
|
||||
g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{msg}")
|
||||
}
|
||||
g.P("return true, err")
|
||||
}
|
||||
|
||||
// genOneofFieldSizer generates the sizer case for a oneof subfield.
|
||||
func genOneofFieldSizer(g *protogen.GeneratedFile, field *protogen.Field) {
|
||||
sizeProto := protoPackage.Ident("Size")
|
||||
sizeVarint := protoPackage.Ident("SizeVarint")
|
||||
g.P("case *", fieldOneofType(field), ":")
|
||||
if field.Desc.Kind() == protoreflect.MessageKind {
|
||||
g.P("s := ", sizeProto, "(x.", field.GoName, ")")
|
||||
}
|
||||
// Tag and wire varint is known statically.
|
||||
tagAndWireSize := proto.SizeVarint(uint64(field.Desc.Number()) << 3) // wire doesn't affect varint size
|
||||
g.P("n += ", tagAndWireSize, " // tag and wire")
|
||||
switch field.Desc.Kind() {
|
||||
case protoreflect.BoolKind:
|
||||
g.P("n += 1")
|
||||
case protoreflect.EnumKind, protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
|
||||
g.P("n += ", sizeVarint, "(uint64(x.", field.GoName, "))")
|
||||
case protoreflect.Sint32Kind:
|
||||
g.P("n += ", sizeVarint, "(uint64((uint32(x.", field.GoName, ") << 1) ^ uint32((int32(x.", field.GoName, ") >> 31))))")
|
||||
case protoreflect.Sint64Kind:
|
||||
g.P("n += ", sizeVarint, "(uint64(uint64(x.", field.GoName, "<<1) ^ uint64((int64(x.", field.GoName, ") >> 63))))")
|
||||
case protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind, protoreflect.FloatKind:
|
||||
g.P("n += 4")
|
||||
case protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind, protoreflect.DoubleKind:
|
||||
g.P("n += 8")
|
||||
case protoreflect.StringKind, protoreflect.BytesKind:
|
||||
g.P("n += ", sizeVarint, "(uint64(len(x.", field.GoName, ")))")
|
||||
g.P("n += len(x.", field.GoName, ")")
|
||||
case protoreflect.MessageKind:
|
||||
g.P("n += ", sizeVarint, "(uint64(s))")
|
||||
g.P("n += s")
|
||||
case protoreflect.GroupKind:
|
||||
g.P("n += ", sizeProto, "(x.", field.GoName, ")")
|
||||
g.P("n += ", tagAndWireSize, " // tag and wire")
|
||||
}
|
||||
}
|
||||
|
||||
// fieldOneofType returns the wrapper type used to represent a field in a oneof.
|
||||
func fieldOneofType(field *protogen.Field) protogen.GoIdent {
|
||||
ident := protogen.GoIdent{
|
||||
GoImportPath: field.ParentMessage.GoIdent.GoImportPath,
|
||||
GoName: field.ParentMessage.GoIdent.GoName + "_" + field.GoName,
|
||||
}
|
||||
// Check for collisions with nested messages or enums.
|
||||
//
|
||||
// This conflict resolution is incomplete: Among other things, it
|
||||
// does not consider collisions with other oneof field types.
|
||||
//
|
||||
// TODO: Consider dropping this entirely. Detecting conflicts and
|
||||
// producing an error is almost certainly better than permuting
|
||||
// field and type names in mostly unpredictable ways.
|
||||
Loop:
|
||||
for {
|
||||
for _, message := range field.ParentMessage.Messages {
|
||||
if message.GoIdent == ident {
|
||||
ident.GoName += "_"
|
||||
continue Loop
|
||||
}
|
||||
}
|
||||
for _, enum := range field.ParentMessage.Enums {
|
||||
if enum.GoIdent == ident {
|
||||
ident.GoName += "_"
|
||||
continue Loop
|
||||
}
|
||||
}
|
||||
return ident
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type AnnotationsTestEnum int32
|
||||
|
||||
|
@ -20,7 +20,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// COMMENT: Message1
|
||||
type Message1 struct {
|
||||
@ -93,57 +93,13 @@ func (m *Message1) GetOneof1AField1() string {
|
||||
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{}{
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*Message1) XXX_OneofWrappers() []interface{} {
|
||||
return []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:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type DeprecatedEnum int32 // Deprecated: Do not use.
|
||||
const (
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type BaseMessage struct {
|
||||
Field *string `protobuf:"bytes,1,opt,name=field" json:"field,omitempty"`
|
||||
|
@ -20,7 +20,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Enum int32
|
||||
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type ExtraMessage struct {
|
||||
Data []byte `protobuf:"bytes,1,opt,name=data" json:"data,omitempty"`
|
||||
|
@ -19,7 +19,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Enum int32
|
||||
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// Assorted edge cases in field name conflict resolution.
|
||||
//
|
||||
@ -273,9 +273,9 @@ func (m *Message) GetOneofMessageConflict() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// XXX_OneofFuncs is for the internal use of the proto package.
|
||||
func (*Message) 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 _Message_OneofMarshaler, _Message_OneofUnmarshaler, _Message_OneofSizer, []interface{}{
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*Message) XXX_OneofWrappers() []interface{} {
|
||||
return []interface{}{
|
||||
(*Message_OneofConflictA)(nil),
|
||||
(*Message_OneofNoConflict)(nil),
|
||||
(*Message_OneofConflictB_)(nil),
|
||||
@ -283,116 +283,6 @@ func (*Message) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buffer) error
|
||||
}
|
||||
}
|
||||
|
||||
func _Message_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
|
||||
m := msg.(*Message)
|
||||
// oneof_conflict_a
|
||||
switch x := m.OneofConflictA_.(type) {
|
||||
case *Message_OneofConflictA:
|
||||
b.EncodeVarint(40<<3 | proto.WireBytes)
|
||||
b.EncodeStringBytes(x.OneofConflictA)
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("Message.OneofConflictA_ has unexpected type %T", x)
|
||||
}
|
||||
// oneof_conflict_b
|
||||
switch x := m.OneofConflictB.(type) {
|
||||
case *Message_OneofNoConflict:
|
||||
b.EncodeVarint(50<<3 | proto.WireBytes)
|
||||
b.EncodeStringBytes(x.OneofNoConflict)
|
||||
case *Message_OneofConflictB_:
|
||||
b.EncodeVarint(51<<3 | proto.WireBytes)
|
||||
b.EncodeStringBytes(x.OneofConflictB_)
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("Message.OneofConflictB has unexpected type %T", x)
|
||||
}
|
||||
// oneof_conflict_c
|
||||
switch x := m.OneofConflictC.(type) {
|
||||
case *Message_OneofMessageConflict_:
|
||||
b.EncodeVarint(60<<3 | proto.WireBytes)
|
||||
b.EncodeStringBytes(x.OneofMessageConflict)
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("Message.OneofConflictC has unexpected type %T", x)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _Message_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
|
||||
m := msg.(*Message)
|
||||
switch tag {
|
||||
case 40: // oneof_conflict_a.OneofConflictA
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeStringBytes()
|
||||
m.OneofConflictA_ = &Message_OneofConflictA{x}
|
||||
return true, err
|
||||
case 50: // oneof_conflict_b.oneof_no_conflict
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeStringBytes()
|
||||
m.OneofConflictB = &Message_OneofNoConflict{x}
|
||||
return true, err
|
||||
case 51: // oneof_conflict_b.OneofConflictB
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeStringBytes()
|
||||
m.OneofConflictB = &Message_OneofConflictB_{x}
|
||||
return true, err
|
||||
case 60: // oneof_conflict_c.oneof_message_conflict
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeStringBytes()
|
||||
m.OneofConflictC = &Message_OneofMessageConflict_{x}
|
||||
return true, err
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func _Message_OneofSizer(msg proto.Message) (n int) {
|
||||
m := msg.(*Message)
|
||||
// oneof_conflict_a
|
||||
switch x := m.OneofConflictA_.(type) {
|
||||
case *Message_OneofConflictA:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofConflictA)))
|
||||
n += len(x.OneofConflictA)
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
// oneof_conflict_b
|
||||
switch x := m.OneofConflictB.(type) {
|
||||
case *Message_OneofNoConflict:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofNoConflict)))
|
||||
n += len(x.OneofNoConflict)
|
||||
case *Message_OneofConflictB_:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofConflictB_)))
|
||||
n += len(x.OneofConflictB_)
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
// oneof_conflict_c
|
||||
switch x := m.OneofConflictC.(type) {
|
||||
case *Message_OneofMessageConflict_:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofMessageConflict)))
|
||||
n += len(x.OneofMessageConflict)
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type Message_OneofMessageConflict struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
|
@ -19,7 +19,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
const Default_M_S = sub.Default_M_S
|
||||
|
||||
|
@ -19,7 +19,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Local struct {
|
||||
M *sub.M `protobuf:"bytes,1,opt,name=m" json:"m,omitempty"`
|
||||
|
126
cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
vendored
126
cmd/protoc-gen-go/testdata/import_public/sub/a.pb.go
vendored
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type E int32
|
||||
|
||||
@ -231,70 +231,14 @@ func (m *M) GetOneofInt64() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// XXX_OneofFuncs is for the internal use of the proto package.
|
||||
func (*M) 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 _M_OneofMarshaler, _M_OneofUnmarshaler, _M_OneofSizer, []interface{}{
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*M) XXX_OneofWrappers() []interface{} {
|
||||
return []interface{}{
|
||||
(*M_OneofInt32)(nil),
|
||||
(*M_OneofInt64)(nil),
|
||||
}
|
||||
}
|
||||
|
||||
func _M_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
|
||||
m := msg.(*M)
|
||||
// oneof_field
|
||||
switch x := m.OneofField.(type) {
|
||||
case *M_OneofInt32:
|
||||
b.EncodeVarint(2<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofInt32))
|
||||
case *M_OneofInt64:
|
||||
b.EncodeVarint(3<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofInt64))
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("M.OneofField has unexpected type %T", x)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _M_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
|
||||
m := msg.(*M)
|
||||
switch tag {
|
||||
case 2: // oneof_field.oneof_int32
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &M_OneofInt32{int32(x)}
|
||||
return true, err
|
||||
case 3: // oneof_field.oneof_int64
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &M_OneofInt64{int64(x)}
|
||||
return true, err
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func _M_OneofSizer(msg proto.Message) (n int) {
|
||||
m := msg.(*M)
|
||||
// oneof_field
|
||||
switch x := m.OneofField.(type) {
|
||||
case *M_OneofInt32:
|
||||
n += 1 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofInt32))
|
||||
case *M_OneofInt64:
|
||||
n += 1 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofInt64))
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type M_Submessage struct {
|
||||
// Types that are valid to be assigned to SubmessageOneofField:
|
||||
// *M_Submessage_SubmessageOneofInt32
|
||||
@ -367,70 +311,14 @@ func (m *M_Submessage) GetSubmessageOneofInt64() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// XXX_OneofFuncs is for the internal use of the proto package.
|
||||
func (*M_Submessage) 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 _M_Submessage_OneofMarshaler, _M_Submessage_OneofUnmarshaler, _M_Submessage_OneofSizer, []interface{}{
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*M_Submessage) XXX_OneofWrappers() []interface{} {
|
||||
return []interface{}{
|
||||
(*M_Submessage_SubmessageOneofInt32)(nil),
|
||||
(*M_Submessage_SubmessageOneofInt64)(nil),
|
||||
}
|
||||
}
|
||||
|
||||
func _M_Submessage_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
|
||||
m := msg.(*M_Submessage)
|
||||
// submessage_oneof_field
|
||||
switch x := m.SubmessageOneofField.(type) {
|
||||
case *M_Submessage_SubmessageOneofInt32:
|
||||
b.EncodeVarint(1<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.SubmessageOneofInt32))
|
||||
case *M_Submessage_SubmessageOneofInt64:
|
||||
b.EncodeVarint(2<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.SubmessageOneofInt64))
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("M_Submessage.SubmessageOneofField has unexpected type %T", x)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _M_Submessage_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
|
||||
m := msg.(*M_Submessage)
|
||||
switch tag {
|
||||
case 1: // submessage_oneof_field.submessage_oneof_int32
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt32{int32(x)}
|
||||
return true, err
|
||||
case 2: // submessage_oneof_field.submessage_oneof_int64
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.SubmessageOneofField = &M_Submessage_SubmessageOneofInt64{int64(x)}
|
||||
return true, err
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func _M_Submessage_OneofSizer(msg proto.Message) (n int) {
|
||||
m := msg.(*M_Submessage)
|
||||
// submessage_oneof_field
|
||||
switch x := m.SubmessageOneofField.(type) {
|
||||
case *M_Submessage_SubmessageOneofInt32:
|
||||
n += 1 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.SubmessageOneofInt32))
|
||||
case *M_Submessage_SubmessageOneofInt64:
|
||||
n += 1 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.SubmessageOneofInt64))
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
var E_ExtensionField = &proto.ExtensionDesc{
|
||||
ExtendedType: (*M)(nil),
|
||||
ExtensionType: (*string)(nil),
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M2 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type E1 int32
|
||||
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M2 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M3 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M4 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M1 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type M2 struct {
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -19,7 +19,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type A1M1 struct {
|
||||
F *test_a_1.M1 `protobuf:"bytes,1,opt,name=f,proto3" json:"f,omitempty"`
|
||||
|
@ -19,7 +19,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type A1M2 struct {
|
||||
F *test_a_1.M2 `protobuf:"bytes,1,opt,name=f,proto3" json:"f,omitempty"`
|
||||
|
@ -22,7 +22,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type All struct {
|
||||
Am1 *test_a_1.M1 `protobuf:"bytes,1,opt,name=am1,proto3" json:"am1,omitempty"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Enum int32
|
||||
|
||||
|
2
cmd/protoc-gen-go/testdata/proto2/enum.pb.go
vendored
2
cmd/protoc-gen-go/testdata/proto2/enum.pb.go
vendored
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// EnumType1 comment.
|
||||
type EnumType1 int32
|
||||
|
339
cmd/protoc-gen-go/testdata/proto2/fields.pb.go
vendored
339
cmd/protoc-gen-go/testdata/proto2/fields.pb.go
vendored
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type FieldTestMessage_Enum int32
|
||||
|
||||
@ -1083,9 +1083,9 @@ func (m *FieldTestMessage) GetOneofTwo_2() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// XXX_OneofFuncs is for the internal use of the proto package.
|
||||
func (*FieldTestMessage) 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 _FieldTestMessage_OneofMarshaler, _FieldTestMessage_OneofUnmarshaler, _FieldTestMessage_OneofSizer, []interface{}{
|
||||
// XXX_OneofWrappers is for the internal use of the proto package.
|
||||
func (*FieldTestMessage) XXX_OneofWrappers() []interface{} {
|
||||
return []interface{}{
|
||||
(*FieldTestMessage_OneofBool)(nil),
|
||||
(*FieldTestMessage_OneofEnum)(nil),
|
||||
(*FieldTestMessage_OneofInt32)(nil),
|
||||
@ -1110,337 +1110,6 @@ func (*FieldTestMessage) XXX_OneofFuncs() (func(msg proto.Message, b *proto.Buff
|
||||
}
|
||||
}
|
||||
|
||||
func _FieldTestMessage_OneofMarshaler(msg proto.Message, b *proto.Buffer) error {
|
||||
m := msg.(*FieldTestMessage)
|
||||
// oneof_field
|
||||
switch x := m.OneofField.(type) {
|
||||
case *FieldTestMessage_OneofBool:
|
||||
t := uint64(0)
|
||||
if x.OneofBool {
|
||||
t = 1
|
||||
}
|
||||
b.EncodeVarint(601<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(t)
|
||||
case *FieldTestMessage_OneofEnum:
|
||||
b.EncodeVarint(602<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofEnum))
|
||||
case *FieldTestMessage_OneofInt32:
|
||||
b.EncodeVarint(603<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofInt32))
|
||||
case *FieldTestMessage_OneofSint32:
|
||||
b.EncodeVarint(604<<3 | proto.WireVarint)
|
||||
b.EncodeZigzag32(uint64(x.OneofSint32))
|
||||
case *FieldTestMessage_OneofUint32:
|
||||
b.EncodeVarint(605<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofUint32))
|
||||
case *FieldTestMessage_OneofInt64:
|
||||
b.EncodeVarint(606<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofInt64))
|
||||
case *FieldTestMessage_OneofSint64:
|
||||
b.EncodeVarint(607<<3 | proto.WireVarint)
|
||||
b.EncodeZigzag64(uint64(x.OneofSint64))
|
||||
case *FieldTestMessage_OneofUint64:
|
||||
b.EncodeVarint(608<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofUint64))
|
||||
case *FieldTestMessage_OneofSfixed32:
|
||||
b.EncodeVarint(609<<3 | proto.WireFixed32)
|
||||
b.EncodeFixed32(uint64(x.OneofSfixed32))
|
||||
case *FieldTestMessage_OneofFixed32:
|
||||
b.EncodeVarint(610<<3 | proto.WireFixed32)
|
||||
b.EncodeFixed32(uint64(x.OneofFixed32))
|
||||
case *FieldTestMessage_OneofFloat:
|
||||
b.EncodeVarint(611<<3 | proto.WireFixed32)
|
||||
b.EncodeFixed32(uint64(math.Float32bits(x.OneofFloat)))
|
||||
case *FieldTestMessage_OneofSfixed64:
|
||||
b.EncodeVarint(612<<3 | proto.WireFixed64)
|
||||
b.EncodeFixed64(uint64(x.OneofSfixed64))
|
||||
case *FieldTestMessage_OneofFixed64:
|
||||
b.EncodeVarint(613<<3 | proto.WireFixed64)
|
||||
b.EncodeFixed64(uint64(x.OneofFixed64))
|
||||
case *FieldTestMessage_OneofDouble:
|
||||
b.EncodeVarint(614<<3 | proto.WireFixed64)
|
||||
b.EncodeFixed64(math.Float64bits(x.OneofDouble))
|
||||
case *FieldTestMessage_OneofString:
|
||||
b.EncodeVarint(615<<3 | proto.WireBytes)
|
||||
b.EncodeStringBytes(x.OneofString)
|
||||
case *FieldTestMessage_OneofBytes:
|
||||
b.EncodeVarint(616<<3 | proto.WireBytes)
|
||||
b.EncodeRawBytes(x.OneofBytes)
|
||||
case *FieldTestMessage_Oneof_Message:
|
||||
b.EncodeVarint(617<<3 | proto.WireBytes)
|
||||
if err := b.EncodeMessage(x.Oneof_Message); err != nil {
|
||||
return err
|
||||
}
|
||||
case *FieldTestMessage_Oneofgroup:
|
||||
b.EncodeVarint(618<<3 | proto.WireStartGroup)
|
||||
if err := b.Marshal(x.Oneofgroup); err != nil {
|
||||
return err
|
||||
}
|
||||
b.EncodeVarint(618<<3 | proto.WireEndGroup)
|
||||
case *FieldTestMessage_OneofLargestTag:
|
||||
b.EncodeVarint(536870911<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofLargestTag))
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("FieldTestMessage.OneofField has unexpected type %T", x)
|
||||
}
|
||||
// oneof_two
|
||||
switch x := m.OneofTwo.(type) {
|
||||
case *FieldTestMessage_OneofTwo_1:
|
||||
b.EncodeVarint(700<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofTwo_1))
|
||||
case *FieldTestMessage_OneofTwo_2:
|
||||
b.EncodeVarint(701<<3 | proto.WireVarint)
|
||||
b.EncodeVarint(uint64(x.OneofTwo_2))
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("FieldTestMessage.OneofTwo has unexpected type %T", x)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _FieldTestMessage_OneofUnmarshaler(msg proto.Message, tag, wire int, b *proto.Buffer) (bool, error) {
|
||||
m := msg.(*FieldTestMessage)
|
||||
switch tag {
|
||||
case 601: // oneof_field.oneof_bool
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofBool{x != 0}
|
||||
return true, err
|
||||
case 602: // oneof_field.oneof_enum
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofEnum{FieldTestMessage_Enum(x)}
|
||||
return true, err
|
||||
case 603: // oneof_field.oneof_int32
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofInt32{int32(x)}
|
||||
return true, err
|
||||
case 604: // oneof_field.oneof_sint32
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeZigzag32()
|
||||
m.OneofField = &FieldTestMessage_OneofSint32{int32(x)}
|
||||
return true, err
|
||||
case 605: // oneof_field.oneof_uint32
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofUint32{uint32(x)}
|
||||
return true, err
|
||||
case 606: // oneof_field.oneof_int64
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofInt64{int64(x)}
|
||||
return true, err
|
||||
case 607: // oneof_field.oneof_sint64
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeZigzag64()
|
||||
m.OneofField = &FieldTestMessage_OneofSint64{int64(x)}
|
||||
return true, err
|
||||
case 608: // oneof_field.oneof_uint64
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofUint64{x}
|
||||
return true, err
|
||||
case 609: // oneof_field.oneof_sfixed32
|
||||
if wire != proto.WireFixed32 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed32()
|
||||
m.OneofField = &FieldTestMessage_OneofSfixed32{int32(x)}
|
||||
return true, err
|
||||
case 610: // oneof_field.oneof_fixed32
|
||||
if wire != proto.WireFixed32 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed32()
|
||||
m.OneofField = &FieldTestMessage_OneofFixed32{uint32(x)}
|
||||
return true, err
|
||||
case 611: // oneof_field.oneof_float
|
||||
if wire != proto.WireFixed32 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed32()
|
||||
m.OneofField = &FieldTestMessage_OneofFloat{math.Float32frombits(uint32(x))}
|
||||
return true, err
|
||||
case 612: // oneof_field.oneof_sfixed64
|
||||
if wire != proto.WireFixed64 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed64()
|
||||
m.OneofField = &FieldTestMessage_OneofSfixed64{int64(x)}
|
||||
return true, err
|
||||
case 613: // oneof_field.oneof_fixed64
|
||||
if wire != proto.WireFixed64 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed64()
|
||||
m.OneofField = &FieldTestMessage_OneofFixed64{x}
|
||||
return true, err
|
||||
case 614: // oneof_field.oneof_double
|
||||
if wire != proto.WireFixed64 {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeFixed64()
|
||||
m.OneofField = &FieldTestMessage_OneofDouble{math.Float64frombits(x)}
|
||||
return true, err
|
||||
case 615: // oneof_field.oneof_string
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeStringBytes()
|
||||
m.OneofField = &FieldTestMessage_OneofString{x}
|
||||
return true, err
|
||||
case 616: // oneof_field.oneof_bytes
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeRawBytes(true)
|
||||
m.OneofField = &FieldTestMessage_OneofBytes{x}
|
||||
return true, err
|
||||
case 617: // oneof_field.oneof_Message
|
||||
if wire != proto.WireBytes {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
msg := new(FieldTestMessage_Message)
|
||||
err := b.DecodeMessage(msg)
|
||||
m.OneofField = &FieldTestMessage_Oneof_Message{msg}
|
||||
return true, err
|
||||
case 618: // oneof_field.oneofgroup
|
||||
if wire != proto.WireStartGroup {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
msg := new(FieldTestMessage_OneofGroup)
|
||||
err := b.DecodeGroup(msg)
|
||||
m.OneofField = &FieldTestMessage_Oneofgroup{msg}
|
||||
return true, err
|
||||
case 536870911: // oneof_field.oneof_largest_tag
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofField = &FieldTestMessage_OneofLargestTag{int32(x)}
|
||||
return true, err
|
||||
case 700: // oneof_two.oneof_two_1
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofTwo = &FieldTestMessage_OneofTwo_1{int32(x)}
|
||||
return true, err
|
||||
case 701: // oneof_two.oneof_two_2
|
||||
if wire != proto.WireVarint {
|
||||
return true, proto.ErrInternalBadWireType
|
||||
}
|
||||
x, err := b.DecodeVarint()
|
||||
m.OneofTwo = &FieldTestMessage_OneofTwo_2{int64(x)}
|
||||
return true, err
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func _FieldTestMessage_OneofSizer(msg proto.Message) (n int) {
|
||||
m := msg.(*FieldTestMessage)
|
||||
// oneof_field
|
||||
switch x := m.OneofField.(type) {
|
||||
case *FieldTestMessage_OneofBool:
|
||||
n += 2 // tag and wire
|
||||
n += 1
|
||||
case *FieldTestMessage_OneofEnum:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofEnum))
|
||||
case *FieldTestMessage_OneofInt32:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofInt32))
|
||||
case *FieldTestMessage_OneofSint32:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64((uint32(x.OneofSint32) << 1) ^ uint32((int32(x.OneofSint32) >> 31))))
|
||||
case *FieldTestMessage_OneofUint32:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofUint32))
|
||||
case *FieldTestMessage_OneofInt64:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofInt64))
|
||||
case *FieldTestMessage_OneofSint64:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(uint64(x.OneofSint64<<1) ^ uint64((int64(x.OneofSint64) >> 63))))
|
||||
case *FieldTestMessage_OneofUint64:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofUint64))
|
||||
case *FieldTestMessage_OneofSfixed32:
|
||||
n += 2 // tag and wire
|
||||
n += 4
|
||||
case *FieldTestMessage_OneofFixed32:
|
||||
n += 2 // tag and wire
|
||||
n += 4
|
||||
case *FieldTestMessage_OneofFloat:
|
||||
n += 2 // tag and wire
|
||||
n += 4
|
||||
case *FieldTestMessage_OneofSfixed64:
|
||||
n += 2 // tag and wire
|
||||
n += 8
|
||||
case *FieldTestMessage_OneofFixed64:
|
||||
n += 2 // tag and wire
|
||||
n += 8
|
||||
case *FieldTestMessage_OneofDouble:
|
||||
n += 2 // tag and wire
|
||||
n += 8
|
||||
case *FieldTestMessage_OneofString:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofString)))
|
||||
n += len(x.OneofString)
|
||||
case *FieldTestMessage_OneofBytes:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(len(x.OneofBytes)))
|
||||
n += len(x.OneofBytes)
|
||||
case *FieldTestMessage_Oneof_Message:
|
||||
s := proto.Size(x.Oneof_Message)
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(s))
|
||||
n += s
|
||||
case *FieldTestMessage_Oneofgroup:
|
||||
n += 2 // tag and wire
|
||||
n += proto.Size(x.Oneofgroup)
|
||||
n += 2 // tag and wire
|
||||
case *FieldTestMessage_OneofLargestTag:
|
||||
n += 5 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofLargestTag))
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
// oneof_two
|
||||
switch x := m.OneofTwo.(type) {
|
||||
case *FieldTestMessage_OneofTwo_1:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofTwo_1))
|
||||
case *FieldTestMessage_OneofTwo_2:
|
||||
n += 2 // tag and wire
|
||||
n += proto.SizeVarint(uint64(x.OneofTwo_2))
|
||||
case nil:
|
||||
default:
|
||||
panic(fmt.Sprintf("proto: unexpected type %T in oneof", x))
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type FieldTestMessage_OptionalGroup struct {
|
||||
OptionalGroup *string `protobuf:"bytes,19,opt,name=optional_group,json=optionalGroup" json:"optional_group,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Layer1 struct {
|
||||
L2 *Layer1_Layer2 `protobuf:"bytes,1,opt,name=l2" json:"l2,omitempty"`
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Message struct {
|
||||
I32 *int32 `protobuf:"varint,1,opt,name=i32" json:"i32,omitempty"`
|
||||
|
2
cmd/protoc-gen-go/testdata/proto3/enum.pb.go
vendored
2
cmd/protoc-gen-go/testdata/proto3/enum.pb.go
vendored
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type Enum int32
|
||||
|
||||
|
@ -18,7 +18,7 @@ var _ = math.Inf
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
type FieldTestMessage_Enum int32
|
||||
|
||||
|
4
go.mod
4
go.mod
@ -1,9 +1,7 @@
|
||||
module github.com/golang/protobuf/v2
|
||||
|
||||
require (
|
||||
github.com/golang/protobuf v1.2.0
|
||||
github.com/golang/protobuf v1.2.1-0.20181127190454-8d0c54c12466
|
||||
github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783
|
||||
golang.org/x/net v0.0.0-20180821023952-922f4815f713 // indirect
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
|
||||
golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48
|
||||
)
|
||||
|
9
go.sum
9
go.sum
@ -1,10 +1,11 @@
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.2.1-0.20181127190454-8d0c54c12466 h1:Kz9p8XEhFbEfi4ka99LEEwIXF4jqyIdB5fuh7UbMFj4=
|
||||
github.com/golang/protobuf v1.2.1-0.20181127190454-8d0c54c12466/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
|
||||
github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783 h1:wVZ6laEGf86tNDTpR5mxFyFIclJJiXCxuJhcQKnsOHk=
|
||||
github.com/google/go-cmp v0.2.1-0.20181101181452-745b8ec83783/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
golang.org/x/net v0.0.0-20180821023952-922f4815f713 h1:rMJUcaDGbG+X967I4zGKCq5laYqcGKJmpB+3jhpOhPw=
|
||||
golang.org/x/net v0.0.0-20180821023952-922f4815f713/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48 h1:PIz+xUHW4G/jqfFWeKhQ96ZV/t2HDsXfWj923rV0bZY=
|
||||
golang.org/x/tools v0.0.0-20180904205237-0aa4b8830f48/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
|
@ -103,18 +103,21 @@ fieldLoop:
|
||||
continue fieldLoop
|
||||
}
|
||||
}
|
||||
var oneofWrappers []interface{}
|
||||
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofFuncs"); ok {
|
||||
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
|
||||
oneofLoop:
|
||||
for _, v := range vs.Interface().([]interface{}) {
|
||||
tf := reflect.TypeOf(v).Elem()
|
||||
f := tf.Field(0)
|
||||
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
||||
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
||||
n, _ := strconv.ParseUint(s, 10, 64)
|
||||
oneofFields[pref.FieldNumber(n)] = tf
|
||||
continue oneofLoop
|
||||
}
|
||||
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3].Interface().([]interface{})
|
||||
}
|
||||
if fn, ok := reflect.PtrTo(t).MethodByName("XXX_OneofWrappers"); ok {
|
||||
oneofWrappers = fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0].Interface().([]interface{})
|
||||
}
|
||||
for _, v := range oneofWrappers {
|
||||
tf := reflect.TypeOf(v).Elem()
|
||||
f := tf.Field(0)
|
||||
for _, s := range strings.Split(f.Tag.Get("protobuf"), ",") {
|
||||
if len(s) > 0 && strings.Trim(s, "0123456789") == "" {
|
||||
n, _ := strconv.ParseUint(s, 10, 64)
|
||||
oneofFields[pref.FieldNumber(n)] = tf
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user