|
@@ -18,13 +18,13 @@ func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
if g.PrintLeadingComments(oneof.Location) {
|
|
if g.PrintLeadingComments(oneof.Location) {
|
|
|
g.P("//")
|
|
g.P("//")
|
|
|
}
|
|
}
|
|
|
- g.P("// Types that are valid to be assigned to ", oneof.GoName, ":")
|
|
|
|
|
|
|
+ g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":")
|
|
|
for _, field := range oneof.Fields {
|
|
for _, field := range oneof.Fields {
|
|
|
g.PrintLeadingComments(field.Location)
|
|
g.PrintLeadingComments(field.Location)
|
|
|
g.P("//\t*", fieldOneofType(field))
|
|
g.P("//\t*", fieldOneofType(field))
|
|
|
}
|
|
}
|
|
|
- g.Annotate(message.GoIdent.GoName+"."+oneof.GoName, oneof.Location)
|
|
|
|
|
- g.P(oneof.GoName, " ", oneofInterfaceName(message, oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`")
|
|
|
|
|
|
|
+ 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,
|
|
// genOneofTypes generates the interface type used for a oneof field,
|
|
@@ -33,7 +33,7 @@ func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
// It also generates the getter method for the parent oneof field
|
|
// It also generates the getter method for the parent oneof field
|
|
|
// (but not the member fields).
|
|
// (but not the member fields).
|
|
|
func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
|
func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) {
|
|
|
- ifName := oneofInterfaceName(message, oneof)
|
|
|
|
|
|
|
+ ifName := oneofInterfaceName(oneof)
|
|
|
g.P("type ", ifName, " interface {")
|
|
g.P("type ", ifName, " interface {")
|
|
|
g.P(ifName, "()")
|
|
g.P(ifName, "()")
|
|
|
g.P("}")
|
|
g.P("}")
|
|
@@ -58,15 +58,25 @@ func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
|
|
g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location)
|
|
|
g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {")
|
|
g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {")
|
|
|
g.P("if m != nil {")
|
|
g.P("if m != nil {")
|
|
|
- g.P("return m.", oneof.GoName)
|
|
|
|
|
|
|
+ g.P("return m.", oneofFieldName(oneof))
|
|
|
g.P("}")
|
|
g.P("}")
|
|
|
g.P("return nil")
|
|
g.P("return nil")
|
|
|
g.P("}")
|
|
g.P("}")
|
|
|
g.P()
|
|
g.P()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func oneofInterfaceName(message *protogen.Message, oneof *protogen.Oneof) string {
|
|
|
|
|
- return fmt.Sprintf("is%s_%s", message.GoIdent.GoName, oneof.GoName)
|
|
|
|
|
|
|
+// 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.
|
|
// genOneofFuncs generates the XXX_OneofFuncs method for a message.
|
|
@@ -104,7 +114,7 @@ func genOneofFuncs(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
g.P("m := msg.(*", message.GoIdent, ")")
|
|
g.P("m := msg.(*", message.GoIdent, ")")
|
|
|
for _, oneof := range message.Oneofs {
|
|
for _, oneof := range message.Oneofs {
|
|
|
g.P("// ", oneof.Desc.Name())
|
|
g.P("// ", oneof.Desc.Name())
|
|
|
- g.P("switch x := m.", oneof.GoName, ".(type) {")
|
|
|
|
|
|
|
+ g.P("switch x := m.", oneofFieldName(oneof), ".(type) {")
|
|
|
for _, field := range oneof.Fields {
|
|
for _, field := range oneof.Fields {
|
|
|
genOneofFieldMarshal(g, field)
|
|
genOneofFieldMarshal(g, field)
|
|
|
}
|
|
}
|
|
@@ -113,7 +123,7 @@ func genOneofFuncs(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
g.P("return ", protogen.GoIdent{
|
|
g.P("return ", protogen.GoIdent{
|
|
|
GoImportPath: "fmt",
|
|
GoImportPath: "fmt",
|
|
|
GoName: "Errorf",
|
|
GoName: "Errorf",
|
|
|
- }, `("`, message.GoIdent.GoName, ".", oneof.GoName, ` has unexpected type %T", x)`)
|
|
|
|
|
|
|
+ }, `("`, message.GoIdent.GoName, ".", oneofFieldName(oneof), ` has unexpected type %T", x)`)
|
|
|
g.P("}")
|
|
g.P("}")
|
|
|
}
|
|
}
|
|
|
g.P("return nil")
|
|
g.P("return nil")
|
|
@@ -140,7 +150,7 @@ func genOneofFuncs(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo,
|
|
|
g.P("m := msg.(*", message.GoIdent, ")")
|
|
g.P("m := msg.(*", message.GoIdent, ")")
|
|
|
for _, oneof := range message.Oneofs {
|
|
for _, oneof := range message.Oneofs {
|
|
|
g.P("// ", oneof.Desc.Name())
|
|
g.P("// ", oneof.Desc.Name())
|
|
|
- g.P("switch x := m.", oneof.GoName, ".(type) {")
|
|
|
|
|
|
|
+ g.P("switch x := m.", oneofFieldName(oneof), ".(type) {")
|
|
|
for _, field := range oneof.Fields {
|
|
for _, field := range oneof.Fields {
|
|
|
genOneofFieldSizer(g, field)
|
|
genOneofFieldSizer(g, field)
|
|
|
}
|
|
}
|
|
@@ -238,11 +248,11 @@ func genOneofFieldUnmarshal(g *protogen.GeneratedFile, field *protogen.Field) {
|
|
|
case protoreflect.BoolKind:
|
|
case protoreflect.BoolKind:
|
|
|
checkTag("WireVarint")
|
|
checkTag("WireVarint")
|
|
|
g.P("x, err := b.DecodeVarint()")
|
|
g.P("x, err := b.DecodeVarint()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{x != 0}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x != 0}")
|
|
|
case protoreflect.EnumKind:
|
|
case protoreflect.EnumKind:
|
|
|
checkTag("WireVarint")
|
|
checkTag("WireVarint")
|
|
|
g.P("x, err := b.DecodeVarint()")
|
|
g.P("x, err := b.DecodeVarint()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{", field.EnumType.GoIdent, "(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", field.EnumType.GoIdent, "(x)}")
|
|
|
case protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
|
|
case protoreflect.Int32Kind, protoreflect.Uint32Kind, protoreflect.Int64Kind, protoreflect.Uint64Kind:
|
|
|
checkTag("WireVarint")
|
|
checkTag("WireVarint")
|
|
|
g.P("x, err := b.DecodeVarint()")
|
|
g.P("x, err := b.DecodeVarint()")
|
|
@@ -250,63 +260,63 @@ func genOneofFieldUnmarshal(g *protogen.GeneratedFile, field *protogen.Field) {
|
|
|
if goType, _ := fieldGoType(g, field); goType != "uint64" {
|
|
if goType, _ := fieldGoType(g, field); goType != "uint64" {
|
|
|
x = goType + "(x)"
|
|
x = goType + "(x)"
|
|
|
}
|
|
}
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{", x, "}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", x, "}")
|
|
|
case protoreflect.Sint32Kind:
|
|
case protoreflect.Sint32Kind:
|
|
|
checkTag("WireVarint")
|
|
checkTag("WireVarint")
|
|
|
g.P("x, err := b.DecodeZigzag32()")
|
|
g.P("x, err := b.DecodeZigzag32()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{int32(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int32(x)}")
|
|
|
case protoreflect.Sint64Kind:
|
|
case protoreflect.Sint64Kind:
|
|
|
checkTag("WireVarint")
|
|
checkTag("WireVarint")
|
|
|
g.P("x, err := b.DecodeZigzag64()")
|
|
g.P("x, err := b.DecodeZigzag64()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{int64(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int64(x)}")
|
|
|
case protoreflect.Sfixed32Kind:
|
|
case protoreflect.Sfixed32Kind:
|
|
|
checkTag("WireFixed32")
|
|
checkTag("WireFixed32")
|
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{int32(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int32(x)}")
|
|
|
case protoreflect.Fixed32Kind:
|
|
case protoreflect.Fixed32Kind:
|
|
|
checkTag("WireFixed32")
|
|
checkTag("WireFixed32")
|
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{uint32(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{uint32(x)}")
|
|
|
case protoreflect.FloatKind:
|
|
case protoreflect.FloatKind:
|
|
|
checkTag("WireFixed32")
|
|
checkTag("WireFixed32")
|
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
g.P("x, err := b.DecodeFixed32()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{", protogen.GoIdent{
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", protogen.GoIdent{
|
|
|
GoImportPath: "math",
|
|
GoImportPath: "math",
|
|
|
GoName: "Float32frombits",
|
|
GoName: "Float32frombits",
|
|
|
}, "(uint32(x))}")
|
|
}, "(uint32(x))}")
|
|
|
case protoreflect.Sfixed64Kind:
|
|
case protoreflect.Sfixed64Kind:
|
|
|
checkTag("WireFixed64")
|
|
checkTag("WireFixed64")
|
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{int64(x)}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{int64(x)}")
|
|
|
case protoreflect.Fixed64Kind:
|
|
case protoreflect.Fixed64Kind:
|
|
|
checkTag("WireFixed64")
|
|
checkTag("WireFixed64")
|
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{x}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
|
|
case protoreflect.DoubleKind:
|
|
case protoreflect.DoubleKind:
|
|
|
checkTag("WireFixed64")
|
|
checkTag("WireFixed64")
|
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
g.P("x, err := b.DecodeFixed64()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{", protogen.GoIdent{
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{", protogen.GoIdent{
|
|
|
GoImportPath: "math",
|
|
GoImportPath: "math",
|
|
|
GoName: "Float64frombits",
|
|
GoName: "Float64frombits",
|
|
|
}, "(x)}")
|
|
}, "(x)}")
|
|
|
case protoreflect.StringKind:
|
|
case protoreflect.StringKind:
|
|
|
checkTag("WireBytes")
|
|
checkTag("WireBytes")
|
|
|
g.P("x, err := b.DecodeStringBytes()")
|
|
g.P("x, err := b.DecodeStringBytes()")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{x}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
|
|
case protoreflect.BytesKind:
|
|
case protoreflect.BytesKind:
|
|
|
checkTag("WireBytes")
|
|
checkTag("WireBytes")
|
|
|
g.P("x, err := b.DecodeRawBytes(true)")
|
|
g.P("x, err := b.DecodeRawBytes(true)")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{x}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{x}")
|
|
|
case protoreflect.MessageKind:
|
|
case protoreflect.MessageKind:
|
|
|
checkTag("WireBytes")
|
|
checkTag("WireBytes")
|
|
|
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
|
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
|
|
g.P("err := b.DecodeMessage(msg)")
|
|
g.P("err := b.DecodeMessage(msg)")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{msg}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{msg}")
|
|
|
case protoreflect.GroupKind:
|
|
case protoreflect.GroupKind:
|
|
|
checkTag("WireStartGroup")
|
|
checkTag("WireStartGroup")
|
|
|
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
|
g.P("msg := new(", field.MessageType.GoIdent, ")")
|
|
|
g.P("err := b.DecodeGroup(msg)")
|
|
g.P("err := b.DecodeGroup(msg)")
|
|
|
- g.P("m.", oneof.GoName, " = &", fieldOneofType(field), "{msg}")
|
|
|
|
|
|
|
+ g.P("m.", oneofFieldName(oneof), " = &", fieldOneofType(field), "{msg}")
|
|
|
}
|
|
}
|
|
|
g.P("return true, err")
|
|
g.P("return true, err")
|
|
|
}
|
|
}
|