Просмотр исходного кода

goprotobuf: Remove MarshalJSON methods from generated enum types.

This will results in JSON output using numbers for enum values, which
is consistent with the default behaviour of other languages.
UnmarshalJSON is left to handle both numeric and symbolic values.

R=r
CC=golang-dev
https://codereview.appspot.com/14601045
David Symonds 12 лет назад
Родитель
Сommit
4af5f1ff2d

+ 3 - 3
proto/all_test.go

@@ -1382,7 +1382,7 @@ func TestJSON(t *testing.T) {
 		},
 		Bikeshed: MyMessage_GREEN.Enum(),
 	}
-	const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
+	const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
 
 	b, err := json.Marshal(m)
 	if err != nil {
@@ -1401,8 +1401,8 @@ func TestJSON(t *testing.T) {
 		t.Fatalf("got %s, want %s", received, m)
 	}
 
-	// Test unmarshalling of older json wire format.
-	const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
+	// Test unmarshalling of JSON with symbolic enum name.
+	const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
 	received.Reset()
 	if err := json.Unmarshal([]byte(old), received); err != nil {
 		t.Fatalf("json.Unmarshal failed: %v", err)

+ 1 - 3
proto/lib.go

@@ -404,9 +404,7 @@ func EnumName(m map[int32]string, v int32) string {
 // names to its int values, and a byte buffer containing the JSON-encoded
 // value, it returns an int32 that can be cast to the enum type by the caller.
 //
-// The function can deal with older JSON representations, which represented
-// enums directly by their int32 values, or with newer representations, which
-// use the symbolic name as a string.
+// The function can deal with both JSON representations, numeric and symbolic.
 func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error) {
 	if data[0] == '"' {
 		// New style: enums are strings.

+ 0 - 15
proto/testdata/test.pb.go

@@ -34,9 +34,6 @@ func (x FOO) Enum() *FOO {
 func (x FOO) String() string {
 	return proto.EnumName(FOO_name, int32(x))
 }
-func (x FOO) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *FOO) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(FOO_value, data, "FOO")
 	if err != nil {
@@ -108,9 +105,6 @@ func (x GoTest_KIND) Enum() *GoTest_KIND {
 func (x GoTest_KIND) String() string {
 	return proto.EnumName(GoTest_KIND_name, int32(x))
 }
-func (x GoTest_KIND) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *GoTest_KIND) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(GoTest_KIND_value, data, "GoTest_KIND")
 	if err != nil {
@@ -147,9 +141,6 @@ func (x MyMessage_Color) Enum() *MyMessage_Color {
 func (x MyMessage_Color) String() string {
 	return proto.EnumName(MyMessage_Color_name, int32(x))
 }
-func (x MyMessage_Color) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *MyMessage_Color) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(MyMessage_Color_value, data, "MyMessage_Color")
 	if err != nil {
@@ -186,9 +177,6 @@ func (x Defaults_Color) Enum() *Defaults_Color {
 func (x Defaults_Color) String() string {
 	return proto.EnumName(Defaults_Color_name, int32(x))
 }
-func (x Defaults_Color) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Defaults_Color) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Defaults_Color_value, data, "Defaults_Color")
 	if err != nil {
@@ -219,9 +207,6 @@ func (x RepeatedEnum_Color) Enum() *RepeatedEnum_Color {
 func (x RepeatedEnum_Color) String() string {
 	return proto.EnumName(RepeatedEnum_Color_name, int32(x))
 }
-func (x RepeatedEnum_Color) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *RepeatedEnum_Color) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(RepeatedEnum_Color_value, data, "RepeatedEnum_Color")
 	if err != nil {

+ 0 - 12
protoc-gen-go/descriptor/descriptor.pb.go

@@ -92,9 +92,6 @@ func (x FieldDescriptorProto_Type) Enum() *FieldDescriptorProto_Type {
 func (x FieldDescriptorProto_Type) String() string {
 	return proto.EnumName(FieldDescriptorProto_Type_name, int32(x))
 }
-func (x FieldDescriptorProto_Type) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *FieldDescriptorProto_Type) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(FieldDescriptorProto_Type_value, data, "FieldDescriptorProto_Type")
 	if err != nil {
@@ -132,9 +129,6 @@ func (x FieldDescriptorProto_Label) Enum() *FieldDescriptorProto_Label {
 func (x FieldDescriptorProto_Label) String() string {
 	return proto.EnumName(FieldDescriptorProto_Label_name, int32(x))
 }
-func (x FieldDescriptorProto_Label) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *FieldDescriptorProto_Label) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(FieldDescriptorProto_Label_value, data, "FieldDescriptorProto_Label")
 	if err != nil {
@@ -173,9 +167,6 @@ func (x FileOptions_OptimizeMode) Enum() *FileOptions_OptimizeMode {
 func (x FileOptions_OptimizeMode) String() string {
 	return proto.EnumName(FileOptions_OptimizeMode_name, int32(x))
 }
-func (x FileOptions_OptimizeMode) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *FileOptions_OptimizeMode) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(FileOptions_OptimizeMode_value, data, "FileOptions_OptimizeMode")
 	if err != nil {
@@ -213,9 +204,6 @@ func (x FieldOptions_CType) Enum() *FieldOptions_CType {
 func (x FieldOptions_CType) String() string {
 	return proto.EnumName(FieldOptions_CType_name, int32(x))
 }
-func (x FieldOptions_CType) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *FieldOptions_CType) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(FieldOptions_CType_value, data, "FieldOptions_CType")
 	if err != nil {

+ 0 - 6
protoc-gen-go/generator/generator.go

@@ -1201,12 +1201,6 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
 	g.Out()
 	g.P("}")
 
-	g.P("func (x ", ccTypeName, ") MarshalJSON() ([]byte, error) {")
-	g.In()
-	g.P("return json.Marshal(x.String())")
-	g.Out()
-	g.P("}")
-
 	g.P("func (x *", ccTypeName, ") UnmarshalJSON(data []byte) error {")
 	g.In()
 	g.P("value, err := ", g.Pkg["proto"], ".UnmarshalJSONEnum(", ccTypeName, `_value, data, "`, ccTypeName, `")`)

+ 0 - 12
protoc-gen-go/testdata/my_test/test.pb.go

@@ -40,9 +40,6 @@ func (x HatType) Enum() *HatType {
 func (x HatType) String() string {
 	return proto.EnumName(HatType_name, int32(x))
 }
-func (x HatType) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *HatType) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(HatType_value, data, "HatType")
 	if err != nil {
@@ -80,9 +77,6 @@ func (x Days) Enum() *Days {
 func (x Days) String() string {
 	return proto.EnumName(Days_name, int32(x))
 }
-func (x Days) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Days) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Days_value, data, "Days")
 	if err != nil {
@@ -119,9 +113,6 @@ func (x Request_Color) Enum() *Request_Color {
 func (x Request_Color) String() string {
 	return proto.EnumName(Request_Color_name, int32(x))
 }
-func (x Request_Color) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Request_Color) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Request_Color_value, data, "Request_Color")
 	if err != nil {
@@ -155,9 +146,6 @@ func (x Reply_Entry_Game) Enum() *Reply_Entry_Game {
 func (x Reply_Entry_Game) String() string {
 	return proto.EnumName(Reply_Entry_Game_name, int32(x))
 }
-func (x Reply_Entry_Game) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Reply_Entry_Game) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Reply_Entry_Game_value, data, "Reply_Entry_Game")
 	if err != nil {

+ 0 - 12
protoc-gen-go/testdata/my_test/test.pb.go.golden

@@ -40,9 +40,6 @@ func (x HatType) Enum() *HatType {
 func (x HatType) String() string {
 	return proto.EnumName(HatType_name, int32(x))
 }
-func (x HatType) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *HatType) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(HatType_value, data, "HatType")
 	if err != nil {
@@ -80,9 +77,6 @@ func (x Days) Enum() *Days {
 func (x Days) String() string {
 	return proto.EnumName(Days_name, int32(x))
 }
-func (x Days) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Days) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Days_value, data, "Days")
 	if err != nil {
@@ -119,9 +113,6 @@ func (x Request_Color) Enum() *Request_Color {
 func (x Request_Color) String() string {
 	return proto.EnumName(Request_Color_name, int32(x))
 }
-func (x Request_Color) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Request_Color) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Request_Color_value, data, "Request_Color")
 	if err != nil {
@@ -155,9 +146,6 @@ func (x Reply_Entry_Game) Enum() *Reply_Entry_Game {
 func (x Reply_Entry_Game) String() string {
 	return proto.EnumName(Reply_Entry_Game_name, int32(x))
 }
-func (x Reply_Entry_Game) MarshalJSON() ([]byte, error) {
-	return json.Marshal(x.String())
-}
 func (x *Reply_Entry_Game) UnmarshalJSON(data []byte) error {
 	value, err := proto.UnmarshalJSONEnum(Reply_Entry_Game_value, data, "Reply_Entry_Game")
 	if err != nil {