فهرست منبع

jsonpb: Disable Marshaler.EnumsAsString; it is now the default.

Later this week I will remove EnumsAsString entirely.
David Symonds 10 سال پیش
والد
کامیت
9ebc6c4ed9
2فایلهای تغییر یافته به همراه6 افزوده شده و 8 حذف شده
  1. 2 3
      jsonpb/jsonpb.go
  2. 4 5
      jsonpb/jsonpb_test.go

+ 2 - 3
jsonpb/jsonpb.go

@@ -62,8 +62,7 @@ type Marshaler struct {
 	// Whether to render enum values as integers, as opposed to string values.
 	EnumsAsInts bool
 
-	// Use string values for enums (as opposed to integer values)
-	// This is DEPRECATED and will become the default.
+	// EnumsAsString is DEPRECATED and does nothing.
 	EnumsAsString bool
 
 	// A string to indent each level by. The presence of this field will
@@ -192,7 +191,7 @@ func (m *Marshaler) marshalValue(out *errWriter, v reflect.Value,
 
 	// Handle enumerations.
 	protoInfo := structField.Tag.Get("protobuf")
-	if m.EnumsAsString && !m.EnumsAsInts && strings.Contains(protoInfo, ",enum=") {
+	if !m.EnumsAsInts && strings.Contains(protoInfo, ",enum=") {
 		// Unknown enum values will are stringified by the proto library as their
 		// value. Such values should _not_ be quoted or they will be interpreted
 		// as an enum string instead of their value.

+ 4 - 5
jsonpb/jsonpb_test.go

@@ -44,8 +44,7 @@ var (
 	marshaler = Marshaler{}
 
 	marshalerAllOptions = Marshaler{
-		EnumsAsString: true,
-		Indent:        "  ",
+		Indent: "  ",
 	}
 
 	simpleObject = &pb.Simple{
@@ -182,8 +181,8 @@ var (
 		RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
 	}
 
-	complexObjectJSON = `{"color":1,` +
-		`"r_color":[0,1,2],` +
+	complexObjectJSON = `{"color":"GREEN",` +
+		`"r_color":["RED","GREEN","BLUE"],` +
 		`"simple":{"o_int32":-32},` +
 		`"r_simple":[{"o_int32":-32},{"o_int64":"25"}],` +
 		`"repeats":{"r_string":["roses","red"]},` +
@@ -269,7 +268,7 @@ var marshalingTests = []struct {
 	{"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
 	{"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
 	{"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
-	{"enum-string flat object", Marshaler{EnumsAsString: true},
+	{"enum-string flat object", Marshaler{},
 		&pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
 	{"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
 		&pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},