commit 3d991e95a8197d6bc05e10dff748d8a0464784e9
parent b4dd8c5c12a5e401713da86d27bc8355d352595a
Author: francoispqt <francois@parquet.ninja>
Date: Thu, 26 Apr 2018 20:14:56 +0800
unify error return from API
Diffstat:
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/decode.go b/decode.go
@@ -18,10 +18,13 @@ func UnmarshalArray(data []byte, v UnmarshalerArray) error {
dec.length = len(data)
_, err := dec.DecodeArray(v)
dec.addToPool()
+ if err != nil {
+ return err
+ }
if dec.err != nil {
return dec.err
}
- return err
+ return nil
}
// UnmarshalObject parses the JSON-encoded data and stores the result in the value pointed to by v.
@@ -36,7 +39,13 @@ func UnmarshalObject(data []byte, v UnmarshalerObject) error {
dec.length = len(data)
_, err := dec.DecodeObject(v)
dec.addToPool()
- return err
+ if err != nil {
+ return err
+ }
+ if dec.err != nil {
+ return dec.err
+ }
+ return nil
}
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
diff --git a/decode_object.go b/decode_object.go
@@ -54,7 +54,7 @@ func (dec *Decoder) DecodeObject(j UnmarshalerObject) (int, error) {
// can't unmarshall to struct
dec.err = InvalidTypeError(
fmt.Sprintf(
- "Cannot unmarshall to struct, wrong char '%s' found at pos %d",
+ "Cannot unmarshal to struct, wrong char '%s' found at pos %d",
string(dec.data[dec.cursor]),
dec.cursor,
),
diff --git a/decode_object_test.go b/decode_object_test.go
@@ -204,7 +204,8 @@ func (j *jsonObjectComplex) NKeys() int {
func TestDecodeObjComplex(t *testing.T) {
result := jsonObjectComplex{}
err := UnmarshalObject(jsonComplex, &result)
- assert.Nil(t, err, "err should be nil")
+ assert.NotNil(t, err, "err should not be as invalid type as been encountered nil")
+ assert.Equal(t, `Cannot unmarshal to struct, wrong char '"' found at pos 460`, err.Error(), "err should not be as invalid type as been encountered nil")
assert.Equal(t, `{"test":"1","test1":2}`, result.Test, "result.Test is not expected value")
assert.Equal(t, `\\\\\\n`, result.Test2, "result.Test2 is not expected value")
assert.Equal(t, 1, result.Test3, "result.test3 is not expected value")