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

goprotobuf: Make text formatting of unknown enum values compatible with C++.

Also remove a registry map that isn't needed.

R=r
CC=golang-dev
https://codereview.appspot.com/9084044
David Symonds 12 лет назад
Родитель
Сommit
f8a1fcc20a
4 измененных файлов с 17 добавлено и 37 удалено
  1. 1 1
      proto/lib.go
  2. 3 5
      proto/properties.go
  3. 3 31
      proto/text.go
  4. 10 0
      proto/text_test.go

+ 1 - 1
proto/lib.go

@@ -409,7 +409,7 @@ func EnumName(m map[int32]string, v int32) string {
 	if ok {
 		return s
 	}
-	return "unknown_enum_" + strconv.Itoa(int(v))
+	return strconv.Itoa(int(v))
 }
 
 // UnmarshalJSONEnum is a helper function to simplify recovering enum int values

+ 3 - 5
proto/properties.go

@@ -594,15 +594,13 @@ func getbase(pb Message) (t reflect.Type, b structPointer, err error) {
 // A global registry of enum types.
 // The generated code will register the generated maps by calling RegisterEnum.
 
-var enumNameMaps = make(map[string]map[int32]string)
 var enumValueMaps = make(map[string]map[string]int32)
 
 // RegisterEnum is called from the generated code to install the enum descriptor
-// maps into the global table to aid parsing ASCII protocol buffers.
-func RegisterEnum(typeName string, nameMap map[int32]string, valueMap map[string]int32) {
-	if _, ok := enumNameMaps[typeName]; ok {
+// maps into the global table to aid parsing text format protocol buffers.
+func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
+	if _, ok := enumValueMaps[typeName]; ok {
 		panic("proto: duplicate enum registered: " + typeName)
 	}
-	enumNameMaps[typeName] = nameMap
 	enumValueMaps[typeName] = valueMap
 }

+ 3 - 31
proto/text.go

@@ -246,18 +246,9 @@ func writeStruct(w *textWriter, sv reflect.Value) error {
 			continue
 		}
 
-		var written bool
-		var err error
-		if props.Enum != "" {
-			written, err = tryWriteEnum(w, props.Enum, fv)
-			if err != nil {
-				return err
-			}
-		}
-		if !written {
-			if err := writeAny(w, fv, props); err != nil {
-				return err
-			}
+		// Enums have a String method, so writeAny will work fine.
+		if err := writeAny(w, fv, props); err != nil {
+			return err
 		}
 
 		if err := w.WriteByte('\n'); err != nil {
@@ -297,25 +288,6 @@ func writeRaw(w *textWriter, b []byte) error {
 	return nil
 }
 
-// tryWriteEnum attempts to write an enum value as a symbolic constant.
-// If the enum is unregistered, nothing is written and false is returned.
-func tryWriteEnum(w *textWriter, enum string, v reflect.Value) (bool, error) {
-	v = reflect.Indirect(v)
-	if v.Type().Kind() != reflect.Int32 {
-		return false, nil
-	}
-	m, ok := enumNameMaps[enum]
-	if !ok {
-		return false, nil
-	}
-	str, ok := m[int32(v.Int())]
-	if !ok {
-		return false, nil
-	}
-	_, err := fmt.Fprintf(w, str)
-	return true, err
-}
-
 // writeAny writes an arbitrary field.
 func writeAny(w *textWriter, v reflect.Value, props *Properties) error {
 	v = reflect.Indirect(v)

+ 10 - 0
proto/text_test.go

@@ -166,6 +166,16 @@ func TestMarshalTextNil(t *testing.T) {
 	}
 }
 
+func TestMarshalTextUnknownEnum(t *testing.T) {
+	// The Color enum only specifies values 0-2.
+	m := &pb.MyMessage{Bikeshed: pb.MyMessage_Color(3).Enum()}
+	got := m.String()
+	const want = `bikeshed:3 `
+	if got != want {
+		t.Errorf("\n got %q\nwant %q", got, want)
+	}
+}
+
 func BenchmarkMarshalTextBuffered(b *testing.B) {
 	buf := new(bytes.Buffer)
 	m := newTestMessage()