Procházet zdrojové kódy

goprotobuf: Text parsing compatibility fixes:
- accept semicolon and comma after fields
- accept "inf" and "-inf" for float fields

R=iant
CC=golang-dev
https://codereview.appspot.com/6885045

David Symonds před 13 roky
rodič
revize
be02a4a20f
3 změnil soubory, kde provedl 17 přidání a 3 odebrání
  1. 2 0
      proto/size_test.go
  2. 2 0
      proto/testdata/test.proto
  3. 13 3
      proto/text_parser.go

+ 2 - 0
proto/size_test.go

@@ -40,6 +40,8 @@ import (
 )
 
 var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
+
+// messageWithExtension2 is in equal_test.go.
 var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
 
 func init() {

+ 2 - 0
proto/testdata/test.proto

@@ -252,6 +252,8 @@ message MyMessage {
   // This field becomes [][]byte in the generated code.
   repeated bytes rep_bytes = 10;
 
+  optional double bigfloat = 11;
+
   extensions 100 to max;
 }
 

+ 13 - 3
proto/text_parser.go

@@ -154,7 +154,7 @@ func (p *textParser) advance() {
 	p.cur.offset, p.cur.line = p.offset, p.line
 	p.cur.unquoted = ""
 	switch p.s[0] {
-	case '<', '>', '{', '}', ':', '[', ']':
+	case '<', '>', '{', '}', ':', '[', ']', ';', ',':
 		// Single symbol
 		p.cur.value, p.s = p.s[0:1], p.s[1:len(p.s)]
 	case '"', '\'':
@@ -525,6 +525,15 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) *ParseError
 				reqCount--
 			}
 		}
+
+		// For backward compatibility, permit a semicolon or comma after a field.
+		tok = p.next()
+		if tok.err != nil {
+			return tok.err
+		}
+		if tok.value != ";" && tok.value != "," {
+			p.back()
+		}
 	}
 
 	if reqCount > 0 {
@@ -581,8 +590,9 @@ func (p *textParser) readAny(v reflect.Value, props *Properties) *ParseError {
 		}
 	case reflect.Float32, reflect.Float64:
 		v := tok.value
-		if strings.HasSuffix(v, "f") {
-			// Ignore 'f' for compatibility with output generated by C++.
+		// Ignore 'f' for compatibility with output generated by C++, but don't
+		// remove 'f' when the value is "-inf" or "inf".
+		if strings.HasSuffix(v, "f") && tok.value != "-inf" && tok.value != "inf" {
 			v = v[:len(v)-1]
 		}
 		if f, err := strconv.ParseFloat(v, fv.Type().Bits()); err == nil {