mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-04-17 11:42:38 +00:00
cmd/protoc-gen-go: generate Enum method even on proto3
A proto2 message can reference a proto3 enum. It is currently cumbersome to set proto3 enums in proto2 messages. Add the Enum method in all situations to support this situation. It also removes yet another point of divergence between proto2 and proto3. We could consider removing this method if Go ever gets generics, but that hypothetical future is long ways away. Change-Id: Ib83bc87e46b49f3271b90bacb2893bb8e278e4f2 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177257 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
c37adefdac
commit
dbab6c06ee
@ -266,14 +266,17 @@ func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum
|
||||
}
|
||||
|
||||
// Enum method.
|
||||
if enum.Desc.Syntax() != protoreflect.Proto3 {
|
||||
g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
|
||||
g.P("p := new(", enum.GoIdent, ")")
|
||||
g.P("*p = x")
|
||||
g.P("return p")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
//
|
||||
// NOTE: A pointer value is needed to represent presence in proto2.
|
||||
// Since a proto2 message can reference a proto3 enum, it is useful to
|
||||
// always generate this method (even on proto3 enums) to support that case.
|
||||
g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
|
||||
g.P("p := new(", enum.GoIdent, ")")
|
||||
g.P("*p = x")
|
||||
g.P("return p")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// String method.
|
||||
g.P("func (x ", enum.GoIdent, ") String() string {")
|
||||
g.P("return ", protoimplPackage.Ident("X"), ".EnumStringOf(x.Descriptor(), ", protoreflectPackage.Ident("EnumNumber"), "(x))")
|
||||
|
@ -28,6 +28,12 @@ var DeprecatedEnum_value = map[string]int32{
|
||||
"DEPRECATED": 0,
|
||||
}
|
||||
|
||||
func (x DeprecatedEnum) Enum() *DeprecatedEnum {
|
||||
p := new(DeprecatedEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x DeprecatedEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -30,6 +30,12 @@ var Enum_value = map[string]int32{
|
||||
"ZERO": 0,
|
||||
}
|
||||
|
||||
func (x Enum) Enum() *Enum {
|
||||
p := new(Enum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Enum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -29,6 +29,12 @@ var E1_value = map[string]int32{
|
||||
"E1_ZERO": 0,
|
||||
}
|
||||
|
||||
func (x E1) Enum() *E1 {
|
||||
p := new(E1)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x E1) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
6
cmd/protoc-gen-go/testdata/proto3/enum.pb.go
vendored
6
cmd/protoc-gen-go/testdata/proto3/enum.pb.go
vendored
@ -34,6 +34,12 @@ var Enum_value = map[string]int32{
|
||||
"TWO": 2,
|
||||
}
|
||||
|
||||
func (x Enum) Enum() *Enum {
|
||||
p := new(Enum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Enum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -29,6 +29,12 @@ var FieldTestMessage_Enum_value = map[string]int32{
|
||||
"ZERO": 0,
|
||||
}
|
||||
|
||||
func (x FieldTestMessage_Enum) Enum() *FieldTestMessage_Enum {
|
||||
p := new(FieldTestMessage_Enum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x FieldTestMessage_Enum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -38,6 +38,12 @@ var Enum_value = map[string]int32{
|
||||
"TEN": 10,
|
||||
}
|
||||
|
||||
func (x Enum) Enum() *Enum {
|
||||
p := new(Enum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Enum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -85,6 +91,12 @@ var Enums_NestedEnum_value = map[string]int32{
|
||||
"DIEZ": 10,
|
||||
}
|
||||
|
||||
func (x Enums_NestedEnum) Enum() *Enums_NestedEnum {
|
||||
p := new(Enums_NestedEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Enums_NestedEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -41,6 +41,12 @@ var WireFormat_value = map[string]int32{
|
||||
"TEXT_FORMAT": 4,
|
||||
}
|
||||
|
||||
func (x WireFormat) Enum() *WireFormat {
|
||||
p := new(WireFormat)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x WireFormat) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -102,6 +108,12 @@ var TestCategory_value = map[string]int32{
|
||||
"TEXT_FORMAT_TEST": 5,
|
||||
}
|
||||
|
||||
func (x TestCategory) Enum() *TestCategory {
|
||||
p := new(TestCategory)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x TestCategory) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -41,6 +41,12 @@ var ForeignEnum_value = map[string]int32{
|
||||
"FOREIGN_BAZ": 2,
|
||||
}
|
||||
|
||||
func (x ForeignEnum) Enum() *ForeignEnum {
|
||||
p := new(ForeignEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x ForeignEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -88,6 +94,12 @@ var TestAllTypesProto3_NestedEnum_value = map[string]int32{
|
||||
"NEG": -1,
|
||||
}
|
||||
|
||||
func (x TestAllTypesProto3_NestedEnum) Enum() *TestAllTypesProto3_NestedEnum {
|
||||
p := new(TestAllTypesProto3_NestedEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x TestAllTypesProto3_NestedEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -141,6 +153,12 @@ var TestAllTypesProto3_AliasedEnum_value = map[string]int32{
|
||||
"bAz": 2,
|
||||
}
|
||||
|
||||
func (x TestAllTypesProto3_AliasedEnum) Enum() *TestAllTypesProto3_AliasedEnum {
|
||||
p := new(TestAllTypesProto3_AliasedEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x TestAllTypesProto3_AliasedEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -38,6 +38,12 @@ var ForeignEnum_value = map[string]int32{
|
||||
"FOREIGN_BAZ": 6,
|
||||
}
|
||||
|
||||
func (x ForeignEnum) Enum() *ForeignEnum {
|
||||
p := new(ForeignEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x ForeignEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -85,6 +91,12 @@ var TestAllTypes_NestedEnum_value = map[string]int32{
|
||||
"NEG": -1,
|
||||
}
|
||||
|
||||
func (x TestAllTypes_NestedEnum) Enum() *TestAllTypes_NestedEnum {
|
||||
p := new(TestAllTypes_NestedEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x TestAllTypes_NestedEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -29,6 +29,12 @@ var ImportEnum_value = map[string]int32{
|
||||
"IMPORT_ZERO": 0,
|
||||
}
|
||||
|
||||
func (x ImportEnum) Enum() *ImportEnum {
|
||||
p := new(ImportEnum)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x ImportEnum) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -34,6 +34,12 @@ var NullValue_value = map[string]int32{
|
||||
"NULL_VALUE": 0,
|
||||
}
|
||||
|
||||
func (x NullValue) Enum() *NullValue {
|
||||
p := new(NullValue)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x NullValue) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
@ -37,6 +37,12 @@ var Syntax_value = map[string]int32{
|
||||
"SYNTAX_PROTO3": 1,
|
||||
}
|
||||
|
||||
func (x Syntax) Enum() *Syntax {
|
||||
p := new(Syntax)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Syntax) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -149,6 +155,12 @@ var Field_Kind_value = map[string]int32{
|
||||
"TYPE_SINT64": 18,
|
||||
}
|
||||
|
||||
func (x Field_Kind) Enum() *Field_Kind {
|
||||
p := new(Field_Kind)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Field_Kind) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
@ -201,6 +213,12 @@ var Field_Cardinality_value = map[string]int32{
|
||||
"CARDINALITY_REPEATED": 3,
|
||||
}
|
||||
|
||||
func (x Field_Cardinality) Enum() *Field_Cardinality {
|
||||
p := new(Field_Cardinality)
|
||||
*p = x
|
||||
return p
|
||||
}
|
||||
|
||||
func (x Field_Cardinality) String() string {
|
||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user