gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 16474fe1387b5a841f8dd2324a745b4559bbf70e
parent 83efa1992ee4590a67ef36821854b169147c341f
Author: francoispqt <francois@parquet.ninja>
Date:   Thu, 24 May 2018 23:52:30 +0800

return error when uint cannot be unmarshal because number is negative

Diffstat:
Mdecode_number_uint.go | 10+++++-----
Mdecode_number_uint_test.go | 12++++++++++++
Mdecode_object_test.go | 2+-
Mdecode_test.go | 8++++----
Mdecode_unsafe_test.go | 4++--
5 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/decode_number_uint.go b/decode_number_uint.go @@ -26,12 +26,12 @@ func (dec *Decoder) decodeUint8(v *uint8) error { } *v = val return nil - case '-': // if negative, we just set it to 0 + case '-': // if negative, we just set it to 0 and set error + dec.err = dec.makeInvalidUnmarshalErr(v) err := dec.skipData() if err != nil { return err } - *v = 0 return nil case 'n': dec.cursor++ @@ -96,11 +96,11 @@ func (dec *Decoder) decodeUint16(v *uint16) error { *v = val return nil case '-': + dec.err = dec.makeInvalidUnmarshalErr(v) err := dec.skipData() if err != nil { return err } - *v = 0 return nil case 'n': dec.cursor++ @@ -165,11 +165,11 @@ func (dec *Decoder) decodeUint32(v *uint32) error { *v = val return nil case '-': + dec.err = dec.makeInvalidUnmarshalErr(v) err := dec.skipData() if err != nil { return err } - *v = 0 return nil case 'n': dec.cursor++ @@ -233,11 +233,11 @@ func (dec *Decoder) decodeUint64(v *uint64) error { *v = val return nil case '-': + dec.err = dec.makeInvalidUnmarshalErr(v) err := dec.skipData() if err != nil { return err } - *v = 0 return nil case 'n': dec.cursor++ diff --git a/decode_number_uint_test.go b/decode_number_uint_test.go @@ -31,6 +31,7 @@ func TestDecoderUint64(t *testing.T) { name: "basic-negative", json: "-2", expectedResult: 0, + err: true, }, { name: "basic-null", @@ -78,6 +79,7 @@ func TestDecoderUint64(t *testing.T) { name: "basic-negative2", json: "-2349557", expectedResult: 0, + err: true, }, { name: "basic-float", @@ -88,6 +90,7 @@ func TestDecoderUint64(t *testing.T) { name: "basic-float2", json: "-7.8876", expectedResult: 0, + err: true, }, { name: "error1", @@ -183,6 +186,7 @@ func TestDecoderUint32(t *testing.T) { name: "basic-negative", json: "-2", expectedResult: 0, + err: true, }, { name: "basic-null", @@ -207,6 +211,7 @@ func TestDecoderUint32(t *testing.T) { name: "basic-negative2", json: "-2349557", expectedResult: 0, + err: true, }, { name: "basic-big", @@ -240,6 +245,7 @@ func TestDecoderUint32(t *testing.T) { name: "basic-float2", json: "-7.8876", expectedResult: 0, + err: true, }, { name: "error", @@ -343,6 +349,7 @@ func TestDecoderUint16(t *testing.T) { name: "basic-negative", json: "-2", expectedResult: 0, + err: true, }, { name: "basic-null", @@ -381,6 +388,7 @@ func TestDecoderUint16(t *testing.T) { name: "basic-negative2", json: "-24467", expectedResult: 0, + err: true, }, { name: "basic-big", @@ -420,6 +428,7 @@ func TestDecoderUint16(t *testing.T) { name: "basic-float2", json: "-7.8876", expectedResult: 0, + err: true, }, { name: "error", @@ -531,6 +540,7 @@ func TestDecoderUint8(t *testing.T) { name: "basic-negative", json: "-2", expectedResult: 0, + err: true, }, { name: "basic-null", @@ -555,6 +565,7 @@ func TestDecoderUint8(t *testing.T) { name: "basic-negative2", json: "-234", expectedResult: 0, + err: true, }, { name: "basic-big", @@ -596,6 +607,7 @@ func TestDecoderUint8(t *testing.T) { name: "basic-float2", json: "-7.8876", expectedResult: 0, + err: true, }, { name: "error", diff --git a/decode_object_test.go b/decode_object_test.go @@ -450,7 +450,7 @@ func TestDecodeObjectBasic(t *testing.T) { testUint32: 343443, testUint64: 545665757, }, - err: false, + err: true, }, } diff --git a/decode_test.go b/decode_test.go @@ -99,7 +99,7 @@ func TestUnmarshalAllTypes(t *testing.T) { name: "test decode uint64 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint64) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint64(0), *vt, "v must be equal to 1") }, }, @@ -129,7 +129,7 @@ func TestUnmarshalAllTypes(t *testing.T) { name: "test decode uint32 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint32) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint32(0), *vt, "v must be equal to 1") }, }, @@ -337,7 +337,7 @@ func TestDecodeAllTypes(t *testing.T) { name: "test decode uint64 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint64) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint64(0), *vt, "v must be equal to 1") }, }, @@ -367,7 +367,7 @@ func TestDecodeAllTypes(t *testing.T) { name: "test decode uint32 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint32) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint32(0), *vt, "v must be equal to 1") }, }, diff --git a/decode_unsafe_test.go b/decode_unsafe_test.go @@ -101,7 +101,7 @@ func TestUnmarshalUnsafeAllTypes(t *testing.T) { name: "test decode uint64 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint64) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint64(0), *vt, "v must be equal to 1") }, }, @@ -131,7 +131,7 @@ func TestUnmarshalUnsafeAllTypes(t *testing.T) { name: "test decode uint32 negative", expectations: func(err error, v interface{}, t *testing.T) { vt := v.(*uint32) - assert.Nil(t, err, "err must be nil") + assert.NotNil(t, err, "err must not be nil") assert.Equal(t, uint32(0), *vt, "v must be equal to 1") }, },