Skip to content

Commit

Permalink
Merge pull request #833 from unihorn/99
Browse files Browse the repository at this point in the history
bump(code.google.com/p/gogoprotobuf): 7fd1620f09
  • Loading branch information
xiang90 committed Jun 6, 2014
2 parents fbcfe8e + 9424a10 commit 3455431
Show file tree
Hide file tree
Showing 18 changed files with 1,527 additions and 1,229 deletions.
630 changes: 315 additions & 315 deletions third_party/code.google.com/p/gogoprotobuf/proto/all_test.go

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions third_party/code.google.com/p/gogoprotobuf/proto/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ func mergeStruct(out, in reflect.Value) {
mergeAny(out.Field(i), in.Field(i))
}

if emIn, ok := in.Addr().Interface().(extendableProto); ok {
emOut := out.Addr().Interface().(extendableProto)
if emIn, ok := in.Addr().Interface().(extensionsMap); ok {
emOut := out.Addr().Interface().(extensionsMap)
mergeExtension(emOut.ExtensionMap(), emIn.ExtensionMap())
} else if emIn, ok := in.Addr().Interface().(extensionsBytes); ok {
emOut := out.Addr().Interface().(extensionsBytes)
bIn := emIn.GetExtensions()
bOut := emOut.GetExtensions()
*bOut = append(*bOut, *bIn...)
}

uf := in.FieldByName("XXX_unrecognized")
Expand Down
42 changes: 21 additions & 21 deletions third_party/code.google.com/p/gogoprotobuf/proto/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ import (
)

var cloneTestMessage = &pb.MyMessage{
Count: proto.Int32(42),
Name: proto.String("Dave"),
Pet: []string{"bunny", "kitty", "horsey"},
Count: proto.Int32(42),
Name: proto.String("Dave"),
Pet: []string{"bunny", "kitty", "horsey"},
Inner: &pb.InnerMessage{
Host: proto.String("niles"),
Port: proto.Int32(9099),
Connected: proto.Bool(true),
Host: proto.String("niles"),
Port: proto.Int32(9099),
Connected: proto.Bool(true),
},
Others: []*pb.OtherMessage{
{
Expand All @@ -56,7 +56,7 @@ var cloneTestMessage = &pb.MyMessage{
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: proto.Int32(6),
},
RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
}

func init() {
Expand Down Expand Up @@ -99,17 +99,17 @@ var mergeTests = []struct {
Name: proto.String("Dave"),
},
want: &pb.MyMessage{
Count: proto.Int32(42),
Name: proto.String("Dave"),
Count: proto.Int32(42),
Name: proto.String("Dave"),
},
},
{
src: &pb.MyMessage{
Inner: &pb.InnerMessage{
Host: proto.String("hey"),
Connected: proto.Bool(true),
Host: proto.String("hey"),
Connected: proto.Bool(true),
},
Pet: []string{"horsey"},
Pet: []string{"horsey"},
Others: []*pb.OtherMessage{
{
Value: []byte("some bytes"),
Expand All @@ -118,10 +118,10 @@ var mergeTests = []struct {
},
dst: &pb.MyMessage{
Inner: &pb.InnerMessage{
Host: proto.String("niles"),
Port: proto.Int32(9099),
Host: proto.String("niles"),
Port: proto.Int32(9099),
},
Pet: []string{"bunny", "kitty"},
Pet: []string{"bunny", "kitty"},
Others: []*pb.OtherMessage{
{
Key: proto.Int64(31415926535),
Expand All @@ -134,11 +134,11 @@ var mergeTests = []struct {
},
want: &pb.MyMessage{
Inner: &pb.InnerMessage{
Host: proto.String("hey"),
Connected: proto.Bool(true),
Port: proto.Int32(9099),
Host: proto.String("hey"),
Connected: proto.Bool(true),
Port: proto.Int32(9099),
},
Pet: []string{"bunny", "kitty", "horsey"},
Pet: []string{"bunny", "kitty", "horsey"},
Others: []*pb.OtherMessage{
{
Key: proto.Int64(31415926535),
Expand All @@ -158,13 +158,13 @@ var mergeTests = []struct {
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: proto.Int32(6),
},
RepBytes: [][]byte{[]byte("sham")},
RepBytes: [][]byte{[]byte("sham")},
},
want: &pb.MyMessage{
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: proto.Int32(6),
},
RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
},
},
}
Expand Down
17 changes: 8 additions & 9 deletions third_party/code.google.com/p/gogoprotobuf/proto/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer,

ptr := structPointer_Bytes(base, unrecField)

if *ptr == nil {
// This is the first skipped element,
// allocate a new buffer.
*ptr = o.bufalloc()
}

// Add the skipped field to struct field
obuf := o.buf

Expand Down Expand Up @@ -381,9 +375,14 @@ func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group
if prop.extendable {
if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
if err = o.skip(st, tag, wire); err == nil {
ext := e.ExtensionMap()[int32(tag)] // may be missing
ext.enc = append(ext.enc, o.buf[oi:o.index]...)
e.ExtensionMap()[int32(tag)] = ext
if ee, ok := e.(extensionsMap); ok {
ext := ee.ExtensionMap()[int32(tag)] // may be missing
ext.enc = append(ext.enc, o.buf[oi:o.index]...)
ee.ExtensionMap()[int32(tag)] = ext
} else if ee, ok := e.(extensionsBytes); ok {
ext := ee.GetExtensions()
*ext = append(*ext, o.buf[oi:o.index]...)
}
}
continue
}
Expand Down
65 changes: 37 additions & 28 deletions third_party/code.google.com/p/gogoprotobuf/proto/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func Marshal(pb Message) ([]byte, error) {
if err != nil && !state.shouldContinue(err, nil) {
return nil, err
}
if p.buf == nil && err == nil {
// Return a non-nil slice on success.
return []byte{}, nil
}
return p.buf, err
}

Expand Down Expand Up @@ -400,23 +404,8 @@ func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
return nil
}

// need the length before we can write out the message itself,
// so marshal into a separate byte buffer first.
obuf := o.buf
o.buf = o.bufalloc()

err := o.enc_struct(p.stype, p.sprop, structp)

nbuf := o.buf
o.buf = obuf
if err != nil && !state.shouldContinue(err, nil) {
o.buffree(nbuf)
return err
}
o.buf = append(o.buf, p.tagcode...)
o.EncodeRawBytes(nbuf)
o.buffree(nbuf)
return state.err
return o.enc_len_struct(p.stype, p.sprop, structp, &state)
}

func size_struct_message(p *Properties, base structPointer) int {
Expand Down Expand Up @@ -748,24 +737,14 @@ func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) err
continue
}

obuf := o.buf
o.buf = o.bufalloc()

err := o.enc_struct(p.stype, p.sprop, structp)

nbuf := o.buf
o.buf = obuf
o.buf = append(o.buf, p.tagcode...)
err := o.enc_len_struct(p.stype, p.sprop, structp, &state)
if err != nil && !state.shouldContinue(err, nil) {
o.buffree(nbuf)
if err == ErrNil {
return ErrRepeatedHasNil
}
return err
}
o.buf = append(o.buf, p.tagcode...)
o.EncodeRawBytes(nbuf)

o.buffree(nbuf)
}
return state.err
}
Expand Down Expand Up @@ -923,6 +902,36 @@ func size_struct(t reflect.Type, prop *StructProperties, base structPointer) (n
return
}

var zeroes [20]byte // longer than any conceivable sizeVarint

// Encode a struct, preceded by its encoded length (as a varint).
func (o *Buffer) enc_len_struct(t reflect.Type, prop *StructProperties, base structPointer, state *errorState) error {
iLen := len(o.buf)
o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
iMsg := len(o.buf)
err := o.enc_struct(t, prop, base)
if err != nil && !state.shouldContinue(err, nil) {
return err
}
lMsg := len(o.buf) - iMsg
lLen := sizeVarint(uint64(lMsg))
switch x := lLen - (iMsg - iLen); {
case x > 0: // actual length is x bytes larger than the space we reserved
// Move msg x bytes right.
o.buf = append(o.buf, zeroes[:x]...)
copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
case x < 0: // actual length is x bytes smaller than the space we reserved
// Move msg x bytes left.
copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
o.buf = o.buf[:len(o.buf)+x] // x is negative
}
// Encode the length in the reserved space.
o.buf = o.buf[:iLen]
o.EncodeVarint(uint64(lMsg))
o.buf = o.buf[:len(o.buf)+lMsg]
return state.err
}

// errorState maintains the first error that occurs and updates that error
// with additional context.
type errorState struct {
Expand Down
50 changes: 22 additions & 28 deletions third_party/code.google.com/p/gogoprotobuf/proto/encode_gogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ type Sizer interface {
Size() int
}

func (o *Buffer) enc_ext_slice_byte(p *Properties, base structPointer) error {
s := *structPointer_Bytes(base, p.field)
if s == nil {
return ErrNil
}
o.buf = append(o.buf, s...)
return nil
}

func size_ext_slice_byte(p *Properties, base structPointer) (n int) {
s := *structPointer_Bytes(base, p.field)
if s == nil {
return 0
}
n += len(s)
return
}

// Encode a reference to bool pointer.
func (o *Buffer) enc_ref_bool(p *Properties, base structPointer) error {
v := structPointer_RefBool(base, p.field)
Expand Down Expand Up @@ -156,23 +174,8 @@ func (o *Buffer) enc_ref_struct_message(p *Properties, base structPointer) error
return nil
}

// need the length before we can write out the message itself,
// so marshal into a separate byte buffer first.
obuf := o.buf
o.buf = o.bufalloc()

err := o.enc_struct(p.stype, p.sprop, structp)

nbuf := o.buf
o.buf = obuf
if err != nil && !state.shouldContinue(err, nil) {
o.buffree(nbuf)
return err
}
o.buf = append(o.buf, p.tagcode...)
o.EncodeRawBytes(nbuf)
o.buffree(nbuf)
return nil
return o.enc_len_struct(p.stype, p.sprop, structp, &state)
}

//TODO this is only copied, please fix this
Expand Down Expand Up @@ -222,26 +225,17 @@ func (o *Buffer) enc_slice_ref_struct_message(p *Properties, base structPointer)
continue
}

obuf := o.buf
o.buf = o.bufalloc()

err := o.enc_struct(p.stype, p.sprop, structp)

nbuf := o.buf
o.buf = obuf
o.buf = append(o.buf, p.tagcode...)
err := o.enc_len_struct(p.stype, p.sprop, structp, &state)
if err != nil && !state.shouldContinue(err, nil) {
o.buffree(nbuf)
if err == ErrNil {
return ErrRepeatedHasNil
}
return err
}
o.buf = append(o.buf, p.tagcode...)
o.EncodeRawBytes(nbuf)

o.buffree(nbuf)
}
return nil
return state.err
}

//TODO this is only copied, please fix this
Expand Down
10 changes: 5 additions & 5 deletions third_party/code.google.com/p/gogoprotobuf/proto/equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func init() {
}

var EqualTests = []struct {
desc string
a, b Message
exp bool
desc string
a, b Message
exp bool
}{
{"different types", &pb.GoEnum{}, &pb.GoTestField{}, false},
{"equal empty", &pb.GoEnum{}, &pb.GoEnum{}, true},
Expand Down Expand Up @@ -142,13 +142,13 @@ var EqualTests = []struct {
{
"message with group",
&pb.MyMessage{
Count: Int32(1),
Count: Int32(1),
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: Int32(5),
},
},
&pb.MyMessage{
Count: Int32(1),
Count: Int32(1),
Somegroup: &pb.MyMessage_SomeGroup{
GroupField: Int32(5),
},
Expand Down
Loading

0 comments on commit 3455431

Please sign in to comment.