فهرست منبع

goprotobuf: Fix Size for groups.

The previous code did not account for groups at all;
the test data was getting lucky.

Fixes #38.

R=r
CC=golang-dev
https://codereview.appspot.com/10147043
David Symonds 12 سال پیش
والد
کامیت
6677c3e018
4فایلهای تغییر یافته به همراه9 افزوده شده و 4 حذف شده
  1. 4 0
      proto/size.go
  2. 1 0
      proto/size_test.go
  3. 2 2
      proto/testdata/test.pb.go
  4. 2 2
      proto/testdata/test.proto

+ 4 - 0
proto/size.go

@@ -156,6 +156,10 @@ func sizeField(x reflect.Value, prop *Properties) (n int) {
 		return sizeVarint(uint64(n)) + n
 	case reflect.Struct:
 		nb := sizeStruct(x)
+		if prop.Wire == "group" {
+			// Groups have start and end tags instead of a start tag and a length.
+			return nb + len(prop.tagcode)
+		}
 		return sizeVarint(uint64(nb)) + nb
 	case reflect.Uint32, reflect.Uint64:
 		if prop.Wire == "varint" {

+ 1 - 0
proto/size_test.go

@@ -89,6 +89,7 @@ var SizeTests = []struct {
 	{"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
 	// Nested.
 	{"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
+	{"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
 	// Other things.
 	{"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
 	{"extension (unencoded)", messageWithExtension1},

+ 2 - 2
proto/testdata/test.pb.go

@@ -1709,7 +1709,7 @@ func (m *MoreRepeated) GetFixeds() []uint32 {
 }
 
 type GroupOld struct {
-	G                *GroupOld_G `protobuf:"group,1,opt" json:"g,omitempty"`
+	G                *GroupOld_G `protobuf:"group,101,opt" json:"g,omitempty"`
 	XXX_unrecognized []byte      `json:"-"`
 }
 
@@ -1741,7 +1741,7 @@ func (m *GroupOld_G) GetX() int32 {
 }
 
 type GroupNew struct {
-	G                *GroupNew_G `protobuf:"group,1,opt" json:"g,omitempty"`
+	G                *GroupNew_G `protobuf:"group,101,opt" json:"g,omitempty"`
 	XXX_unrecognized []byte      `json:"-"`
 }
 

+ 2 - 2
proto/testdata/test.proto

@@ -401,13 +401,13 @@ message MoreRepeated {
 // GroupNew has a new field inside a group.
 
 message GroupOld {
-  optional group G = 1 {
+  optional group G = 101 {
     optional int32 x = 2;
   }
 }
 
 message GroupNew {
-  optional group G = 1 {
+  optional group G = 101 {
     optional int32 x = 2;
     optional int32 y = 3;
   }