gojay

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

commit 174d4d21ea2502f3d8c723c5b716dc46c5bfbc9e
parent 2baaa31d2ee81a87ca28bb2e057b67a98264b296
Author: francoispqt <francois@parquet.ninja>
Date:   Sun, 28 Oct 2018 22:59:52 +0800

fix error message on unknown type when encoding

Diffstat:
Mencode_interface.go | 18+++++++-----------
Mencode_interface_test.go | 4++--
Merrors.go | 2+-
3 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/encode_interface.go b/encode_interface.go @@ -2,7 +2,6 @@ package gojay import ( "fmt" - "reflect" ) // Encode encodes a value to JSON. @@ -45,7 +44,7 @@ func (enc *Encoder) Encode(v interface{}) error { case *EmbeddedJSON: return enc.EncodeEmbeddedJSON(vt) default: - return InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, reflect.TypeOf(vt).String())) + return InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt)) } } @@ -81,9 +80,8 @@ func (enc *Encoder) AddInterface(value interface{}) { case float32: enc.AddFloat32(vt) default: - t := reflect.TypeOf(vt) - if t != nil { - enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, t.String())) + if vt != nil { + enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt)) return } return @@ -124,9 +122,8 @@ func (enc *Encoder) AddInterfaceKey(key string, value interface{}) { case float32: enc.AddFloat32Key(key, vt) default: - t := reflect.TypeOf(vt) - if t != nil { - enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, t.String())) + if vt != nil { + enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt)) return } return @@ -167,9 +164,8 @@ func (enc *Encoder) AddInterfaceKeyOmitEmpty(key string, v interface{}) { case float32: enc.AddFloat32KeyOmitEmpty(key, vt) default: - t := reflect.TypeOf(vt) - if t != nil { - enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, t.String())) + if vt != nil { + enc.err = InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, vt)) return } return diff --git a/encode_interface_test.go b/encode_interface_test.go @@ -2,7 +2,6 @@ package gojay import ( "fmt" - "reflect" "strings" "testing" @@ -127,7 +126,8 @@ var encoderTestCases = []struct { expectations: func(t *testing.T, b string, err error) { assert.NotNil(t, err, "err should be nil") assert.IsType(t, InvalidMarshalError(""), err, "err should be of type InvalidMarshalError") - assert.Equal(t, fmt.Sprintf(invalidMarshalErrorMsg, reflect.TypeOf(&struct{}{}).String()), err.Error(), "err message should be equal to invalidMarshalErrorMsg") + var s = struct{}{} + assert.Equal(t, fmt.Sprintf(invalidMarshalErrorMsg, &s), err.Error(), "err message should be equal to invalidMarshalErrorMsg") }, }, } diff --git a/errors.go b/errors.go @@ -49,7 +49,7 @@ func (dec *Decoder) makeInvalidUnmarshalErr(v interface{}) error { ) } -const invalidMarshalErrorMsg = "Invalid type %s provided to Marshal" +const invalidMarshalErrorMsg = "Invalid type %T provided to Marshal" // InvalidMarshalError is a type representing an error returned when // Encoding did not find the proper way to encode