aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Symonds <dsymonds@golang.org>2013-06-11 08:48:47 +1000
committerDavid Symonds <dsymonds@golang.org>2013-06-11 08:48:47 +1000
commit6677c3e01836e09b5768997bb132cafed188705d (patch)
treeb1bc907296909b2d04ac385bcbde4cf0bea94afe
parent0c1184e3238b40eca8cbccd32849fb11ea08c603 (diff)
downloadprotobuf-6677c3e01836e09b5768997bb132cafed188705d.tar.gz
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
-rw-r--r--proto/size.go4
-rw-r--r--proto/size_test.go1
-rw-r--r--proto/testdata/test.pb.go4
-rw-r--r--proto/testdata/test.proto4
4 files changed, 9 insertions, 4 deletions
diff --git a/proto/size.go b/proto/size.go
index 0768cfe..7feec30 100644
--- a/proto/size.go
+++ b/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" {
diff --git a/proto/size_test.go b/proto/size_test.go
index 1d1dcec..9b735b7 100644
--- a/proto/size_test.go
+++ b/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},
diff --git a/proto/testdata/test.pb.go b/proto/testdata/test.pb.go
index da1d0e1..c49520a 100644
--- a/proto/testdata/test.pb.go
+++ b/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:"-"`
}
diff --git a/proto/testdata/test.proto b/proto/testdata/test.proto
index 5009664..b921961 100644
--- a/proto/testdata/test.proto
+++ b/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;
}