Sfoglia il codice sorgente

goprotobuf: Fix parsing of repeated enum fields.

R=r
CC=golang-dev
http://codereview.appspot.com/5239052
David Symonds 14 anni fa
parent
commit
ef8f0e8c3e
3 ha cambiato i file con 24 aggiunte e 4 eliminazioni
  1. 7 0
      proto/testdata/test.proto
  2. 1 1
      proto/text_parser.go
  3. 16 3
      proto/text_parser_test.go

+ 7 - 0
proto/testdata/test.proto

@@ -287,3 +287,10 @@ message Defaults {
   optional float F_Ninf = 16 [default=-inf];
   optional float F_Nan = 17 [default=nan];
 }
+
+message RepeatedEnum {
+  enum Color {
+    RED = 1;
+  }
+  repeated Color color = 1;
+}

+ 1 - 1
proto/text_parser.go

@@ -369,7 +369,7 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 
 		// Read one.
 		p.back()
-		return p.readAny(fv.Index(flen), nil) // TODO: pass properties?
+		return p.readAny(fv.Index(flen), props)
 	case reflect.Bool:
 		// Either "true", "false", 1 or 0.
 		switch tok.value {

+ 16 - 3
proto/text_parser_test.go

@@ -252,9 +252,8 @@ func TestUnmarshalText(t *testing.T) {
 			if err != nil {
 				t.Errorf("Test %d: Unexpected error: %v", i, err)
 			} else if !reflect.DeepEqual(pb, test.out) {
-				t.Errorf("Test %d: Incorrect populated \n"+
-					"Have: %v\nWant: %v",
-					i, CompactTextString(pb), CompactTextString(test.out))
+				t.Errorf("Test %d: Incorrect populated \nHave: %v\nWant: %v",
+					i, pb, test.out)
 			}
 		} else {
 			// We do expect failure.
@@ -268,6 +267,20 @@ func TestUnmarshalText(t *testing.T) {
 	}
 }
 
+// Regression test; this caused a panic.
+func TestRepeatedEnum(t *testing.T) {
+	pb := new(RepeatedEnum)
+	if err := UnmarshalText("color: RED", pb); err != nil {
+		t.Fatal(err)
+	}
+	exp := &RepeatedEnum{
+		Color: []RepeatedEnum_Color{RepeatedEnum_RED},
+	}
+	if !reflect.DeepEqual(pb, exp) {
+		t.Errorf("Incorrect populated \nHave: %v\nWant: %v", pb, exp)
+	}
+}
+
 var benchInput string
 
 func init() {