Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge master back to develop #243

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.14.0 (October 26, 2018)

BREAKING CHANGE:
- Switch default encoding of unsigned ints (`int`, `int32`, `int64`) to be on par with [proto3's] variable length
encoding (of `int32`, `int64`) ([#237])

[proto3's]: https://developers.google.com/protocol-buffers/docs/proto#scalar
[#237]: https://github.com/tendermint/go-amino/issues/237

## 0.13.0 (October 15, 2018)

BREAKING CHANGE:
Expand Down
34 changes: 27 additions & 7 deletions binary-decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package amino
import (
"errors"
"fmt"
"math"
"reflect"
"time"

Expand All @@ -12,6 +13,16 @@ import (
//----------------------------------------
// cdc.decodeReflectBinary

var (
ErrOverflowInt = errors.New("encoded integer value overflows int(32)")
)

const (
// architecture dependent int limits:
maxInt = int(^uint(0) >> 1)
minInt = -maxInt - 1
)

// This is the main entrypoint for decoding all types from binary form. This
// function calls decodeReflectBinary*, and generally those functions should
// only call this one, for the prefix bytes are consumed here when present.
Expand Down Expand Up @@ -115,11 +126,12 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Valu
}
rv.SetInt(num)
} else {
num, _n, err = DecodeVarint(bz)
var u64 uint64
u64, _n, err = DecodeUvarint(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
rv.SetInt(num)
rv.SetInt(int64(u64))
}
return

Expand All @@ -132,11 +144,15 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Valu
}
rv.SetInt(int64(num))
} else {
var num int64
num, _n, err = DecodeVarint(bz)
var num uint64
num, _n, err = DecodeUvarint(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
if int64(num) > math.MaxInt32 || int64(num) < math.MinInt32 {
err = ErrOverflowInt
return
}
rv.SetInt(int64(num))
}
return
Expand All @@ -160,12 +176,16 @@ func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo, rv reflect.Valu
return

case reflect.Int:
var num int64
num, _n, err = DecodeVarint(bz)
var num uint64
num, _n, err = DecodeUvarint(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
rv.SetInt(num)
if int64(num) > int64(maxInt) || int64(num) < int64(minInt) {
err = ErrOverflowInt
return
}
rv.SetInt(int64(num))
return

//----------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions binary-encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Va
if fopts.BinFixed64 {
err = EncodeInt64(w, rv.Int())
} else {
err = EncodeVarint(w, rv.Int())
err = EncodeUvarint(w, uint64(rv.Int()))
}

case reflect.Int32:
if fopts.BinFixed32 {
err = EncodeInt32(w, int32(rv.Int()))
} else {
err = EncodeVarint(w, rv.Int())
err = EncodeUvarint(w, uint64(rv.Int()))
}

case reflect.Int16:
Expand All @@ -103,7 +103,7 @@ func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Va
err = EncodeInt8(w, int8(rv.Int()))

case reflect.Int:
err = EncodeVarint(w, rv.Int())
err = EncodeUvarint(w, uint64(rv.Int()))

//----------------------------------------
// Unsigned
Expand Down
2 changes: 1 addition & 1 deletion binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestStructSlice(t *testing.T) {

bz, err := cdc.MarshalBinaryBare(f)
assert.NoError(t, err)
assert.Equal(t, "0A0608C80110CA010A0608CC0110CE01", fmt.Sprintf("%X", bz))
assert.Equal(t, "0A04086410650A0408661067", fmt.Sprintf("%X", bz))
t.Log(bz)
var f2 Foos
cdc.UnmarshalBinaryBare(bz, &f2)
Expand Down
3 changes: 3 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func EncodeUint64(w io.Writer, u uint64) (err error) {
return
}

// EncodeUvarint is used to encode golang's int, int32, int64 by default. unless specified differently by the
// `binary:"fixed32"`, `binary:"fixed64"`, or `binary:"zigzag32"` `binary:"zigzag64"` tags.
// It matches protobufs varint encoding.
func EncodeUvarint(w io.Writer, u uint64) (err error) {
var buf [10]byte
n := binary.PutUvarint(buf[:], u)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Example() {
fmt.Printf("Decoded successfully: %v\n", *bm == *bm2)

// Output:
// Encoded: 0C740613650A0341424310C801 (err: <nil>)
// Encoded: 0B740613650A034142431064 (err: <nil>)
// Decoded: &{ABC 100} (err: <nil>)
// Decoded successfully: true
}
119 changes: 64 additions & 55 deletions tests/proto3/proto/compat.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/proto3/proto/compat.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ message TestInt32 {
int32 Int32 = 1;
}

message TestInt64 {
int64 Int64 = 1;

message TestInts {
int32 Int32 = 1;
int64 Int64 = 2;
}
Loading