diff --git a/buffer_test.go b/buffer_test.go index 13a5968..1f29cf4 100644 --- a/buffer_test.go +++ b/buffer_test.go @@ -406,7 +406,7 @@ func TestNilSlice(t *testing.T) { assert.Equal(t, uint32(len(s)), j) j, err = d.Slice(StringKind) - assert.ErrorIs(t, err, InvalidSlice) + assert.ErrorIs(t, err, ErrInvalidSlice) assert.Zero(t, j) } @@ -420,7 +420,7 @@ func TestError(t *testing.T) { d := Decoder(p.Bytes()) _, err := d.String() - assert.ErrorIs(t, err, InvalidString) + assert.ErrorIs(t, err, ErrInvalidString) val, err := d.Error() assert.NoError(t, err) diff --git a/decode.go b/decode.go index 69f689e..5548906 100644 --- a/decode.go +++ b/decode.go @@ -30,20 +30,20 @@ const ( ) var ( - InvalidSlice = errors.New("invalid slice encoding") - InvalidMap = errors.New("invalid map encoding") - InvalidBytes = errors.New("invalid bytes encoding") - InvalidString = errors.New("invalid string encoding") - InvalidError = errors.New("invalid error encoding") - InvalidBool = errors.New("invalid bool encoding") - InvalidUint8 = errors.New("invalid uint8 encoding") - InvalidUint16 = errors.New("invalid uint16 encoding") - InvalidUint32 = errors.New("invalid uint32 encoding") - InvalidUint64 = errors.New("invalid uint64 encoding") - InvalidInt32 = errors.New("invalid int32 encoding") - InvalidInt64 = errors.New("invalid int64 encoding") - InvalidFloat32 = errors.New("invalid float32 encoding") - InvalidFloat64 = errors.New("invalid float64 encoding") + ErrInvalidSlice = errors.New("invalid slice encoding") + ErrInvalidMap = errors.New("invalid map encoding") + ErrInvalidBytes = errors.New("invalid bytes encoding") + ErrInvalidString = errors.New("invalid string encoding") + ErrInvalidError = errors.New("invalid error encoding") + ErrInvalidBool = errors.New("invalid bool encoding") + ErrInvalidUint8 = errors.New("invalid uint8 encoding") + ErrInvalidUint16 = errors.New("invalid uint16 encoding") + ErrInvalidUint32 = errors.New("invalid uint32 encoding") + ErrInvalidUint64 = errors.New("invalid uint64 encoding") + ErrInvalidInt32 = errors.New("invalid int32 encoding") + ErrInvalidInt64 = errors.New("invalid int64 encoding") + ErrInvalidFloat32 = errors.New("invalid float32 encoding") + ErrInvalidFloat64 = errors.New("invalid float64 encoding") ) func decodeNil(b []byte) ([]byte, bool) { @@ -59,11 +59,11 @@ func decodeMap(b []byte, keyKind, valueKind Kind) ([]byte, uint32, error) { var err error b, size, err = decodeUint32(b[3:]) if err != nil { - return b, 0, InvalidMap + return b, 0, ErrInvalidMap } return b, size, nil } - return b, 0, InvalidMap + return b, 0, ErrInvalidMap } func decodeSlice(b []byte, kind Kind) ([]byte, uint32, error) { @@ -72,11 +72,11 @@ func decodeSlice(b []byte, kind Kind) ([]byte, uint32, error) { var err error b, size, err = decodeUint32(b[2:]) if err != nil { - return b, 0, InvalidSlice + return b, 0, ErrInvalidSlice } return b, size, nil } - return b, 0, InvalidSlice + return b, 0, ErrInvalidSlice } func decodeBytes(b []byte, ret []byte) ([]byte, []byte, error) { @@ -120,7 +120,7 @@ func decodeBytes(b []byte, ret []byte) ([]byte, []byte, error) { return b[size+offset:], append(ret[:0], b[offset:size+offset]...), nil } } - return b, nil, InvalidBytes + return b, nil, ErrInvalidBytes } func decodeString(b []byte) ([]byte, string, error) { @@ -129,13 +129,13 @@ func decodeString(b []byte) ([]byte, string, error) { var err error b, size, err = decodeUint32(b[1:]) if err != nil { - return b, emptyString, InvalidString + return b, emptyString, ErrInvalidString } if len(b) > int(size)-1 { return b[size:], string(b[:size]), nil } } - return b, emptyString, InvalidString + return b, emptyString, ErrInvalidString } func decodeError(b []byte) ([]byte, error, error) { @@ -144,11 +144,11 @@ func decodeError(b []byte) ([]byte, error, error) { var err error b, val, err = decodeString(b[1:]) if err != nil { - return b, nil, InvalidError + return b, nil, ErrInvalidError } return b, Error(val), nil } - return b, nil, InvalidError + return b, nil, ErrInvalidError } func decodeBool(b []byte) ([]byte, bool, error) { @@ -160,14 +160,14 @@ func decodeBool(b []byte) ([]byte, bool, error) { return b[2:], false, nil } } - return b, false, InvalidBool + return b, false, ErrInvalidBool } func decodeUint8(b []byte) ([]byte, uint8, error) { if len(b) > 1 && b[0] == Uint8RawKind { return b[2:], b[1], nil } - return b, 0, InvalidUint8 + return b, 0, ErrInvalidUint8 } func decodeUint16(b []byte) ([]byte, uint16, error) { @@ -189,7 +189,7 @@ func decodeUint16(b []byte) ([]byte, uint16, error) { return b[4:], x | (cb << 14), nil } } - return b, 0, InvalidUint16 + return b, 0, ErrInvalidUint16 } func decodeUint32(b []byte) ([]byte, uint32, error) { @@ -223,7 +223,7 @@ func decodeUint32(b []byte) ([]byte, uint32, error) { return b[6:], x | (cb << 28), nil } } - return b, 0, InvalidUint32 + return b, 0, ErrInvalidUint32 } func decodeUint64(b []byte) ([]byte, uint64, error) { @@ -287,7 +287,7 @@ func decodeUint64(b []byte) ([]byte, uint64, error) { return b[11:], x | (cb << 63), nil } } - return b, 0, InvalidUint64 + return b, 0, ErrInvalidUint64 } func decodeInt32(b []byte) ([]byte, int32, error) { @@ -341,7 +341,7 @@ func decodeInt32(b []byte) ([]byte, int32, error) { return b[6:], int32(x >> 1), nil } } - return b, 0, InvalidInt32 + return b, 0, ErrInvalidInt32 } func decodeInt64(b []byte) ([]byte, int64, error) { @@ -445,14 +445,14 @@ func decodeInt64(b []byte) ([]byte, int64, error) { return b[11:], int64(x >> 1), nil } } - return b, 0, InvalidInt64 + return b, 0, ErrInvalidInt64 } func decodeFloat32(b []byte) ([]byte, float32, error) { if len(b) > 4 && b[0] == Float32RawKind { return b[5:], math.Float32frombits(uint32(b[4]) | uint32(b[3])<<8 | uint32(b[2])<<16 | uint32(b[1])<<24), nil } - return b, 0, InvalidFloat32 + return b, 0, ErrInvalidFloat32 } func decodeFloat64(b []byte) ([]byte, float64, error) { @@ -460,5 +460,5 @@ func decodeFloat64(b []byte) ([]byte, float64, error) { return b[9:], math.Float64frombits(uint64(b[8]) | uint64(b[7])<<8 | uint64(b[6])<<16 | uint64(b[5])<<24 | uint64(b[4])<<32 | uint64(b[3])<<40 | uint64(b[2])<<48 | uint64(b[1])<<56), nil } - return b, 0, InvalidFloat64 + return b, 0, ErrInvalidFloat64 } diff --git a/decode_test.go b/decode_test.go index 273f82e..2af19d4 100644 --- a/decode_test.go +++ b/decode_test.go @@ -66,10 +66,10 @@ func TestDecodeMap(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeMap((p.Bytes())[1:], StringKind, Uint32Kind) - assert.ErrorIs(t, err, InvalidMap) + assert.ErrorIs(t, err, ErrInvalidMap) _, _, err = decodeMap(p.Bytes(), StringKind, Float64Kind) - assert.ErrorIs(t, err, InvalidMap) + assert.ErrorIs(t, err, ErrInvalidMap) remaining, size, err = decodeMap(p.Bytes(), StringKind, Uint32Kind) assert.NoError(t, err) @@ -100,7 +100,7 @@ func TestDecodeBytes(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, value, err = decodeBytes((p.Bytes())[1:], value) - assert.ErrorIs(t, err, InvalidBytes) + assert.ErrorIs(t, err, ErrInvalidBytes) remaining, value, err = decodeBytes(p.Bytes(), value) assert.NoError(t, err) @@ -163,7 +163,7 @@ func TestDecodeString(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeString((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidString) + assert.ErrorIs(t, err, ErrInvalidString) remaining, value, err = decodeString(p.Bytes()) assert.NoError(t, err) @@ -219,7 +219,7 @@ func TestDecodeError(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeError((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidError) + assert.ErrorIs(t, err, ErrInvalidError) remaining, value, err = decodeError(p.Bytes()) assert.NoError(t, err) @@ -273,7 +273,7 @@ func TestDecodeBool(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeBool((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidBool) + assert.ErrorIs(t, err, ErrInvalidBool) remaining, value, err = decodeBool(p.Bytes()) assert.NoError(t, err) @@ -329,7 +329,7 @@ func TestDecodeUint8(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeUint8((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidUint8) + assert.ErrorIs(t, err, ErrInvalidUint8) remaining, value, err = decodeUint8(p.Bytes()) assert.NoError(t, err) @@ -363,7 +363,7 @@ func TestDecodeUint16(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeUint16((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidUint16) + assert.ErrorIs(t, err, ErrInvalidUint16) remaining, value, err = decodeUint16(p.Bytes()) assert.NoError(t, err) @@ -397,7 +397,7 @@ func TestDecodeUint32(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeUint32((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidUint32) + assert.ErrorIs(t, err, ErrInvalidUint32) remaining, value, err = decodeUint32(p.Bytes()) assert.NoError(t, err) @@ -432,7 +432,7 @@ func TestDecodeUint64(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeUint64((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidUint64) + assert.ErrorIs(t, err, ErrInvalidUint64) remaining, value, err = decodeUint64(p.Bytes()) assert.NoError(t, err) @@ -475,7 +475,7 @@ func TestDecodeInt32(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeInt32((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidInt32) + assert.ErrorIs(t, err, ErrInvalidInt32) remaining, value, err = decodeInt32(p.Bytes()) assert.NoError(t, err) @@ -519,7 +519,7 @@ func TestDecodeInt64(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeInt64((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidInt64) + assert.ErrorIs(t, err, ErrInvalidInt64) remaining, value, err = decodeInt64(p.Bytes()) assert.NoError(t, err) @@ -554,7 +554,7 @@ func TestDecodeFloat32(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeFloat32((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidFloat32) + assert.ErrorIs(t, err, ErrInvalidFloat32) remaining, value, err = decodeFloat32(p.Bytes()) assert.NoError(t, err) @@ -588,7 +588,7 @@ func TestDecodeFloat64(t *testing.T) { assert.Equal(t, 0, len(remaining)) _, _, err = decodeFloat64((p.Bytes())[1:]) - assert.ErrorIs(t, err, InvalidFloat64) + assert.ErrorIs(t, err, ErrInvalidFloat64) remaining, value, err = decodeFloat64(p.Bytes()) assert.NoError(t, err) diff --git a/decoder_test.go b/decoder_test.go index 7fe9017..bf42524 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -78,7 +78,7 @@ func TestDecoderMap(t *testing.T) { assert.Equal(t, m, mv) size, err = d.Map(StringKind, Uint32Kind) - assert.ErrorIs(t, err, InvalidMap) + assert.ErrorIs(t, err, ErrInvalidMap) assert.Equal(t, uint32(0), size) p.Reset() @@ -123,7 +123,7 @@ func TestDecoderSlice(t *testing.T) { assert.Equal(t, m, mv) size, err = d.Slice(StringKind) - assert.ErrorIs(t, err, InvalidSlice) + assert.ErrorIs(t, err, ErrInvalidSlice) assert.Equal(t, uint32(0), size) p.Reset() @@ -156,7 +156,7 @@ func TestDecoderBytes(t *testing.T) { assert.Equal(t, v, value) value, err = d.Bytes(value) - assert.ErrorIs(t, err, InvalidBytes) + assert.ErrorIs(t, err, ErrInvalidBytes) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -183,7 +183,7 @@ func TestDecoderString(t *testing.T) { assert.Equal(t, v, value) value, err = d.String() - assert.ErrorIs(t, err, InvalidString) + assert.ErrorIs(t, err, ErrInvalidString) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -210,7 +210,7 @@ func TestDecoderError(t *testing.T) { assert.ErrorIs(t, value, v) value, err = d.Error() - assert.ErrorIs(t, err, InvalidError) + assert.ErrorIs(t, err, ErrInvalidError) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -235,7 +235,7 @@ func TestDecoderBool(t *testing.T) { assert.True(t, value) value, err = d.Bool() - assert.ErrorIs(t, err, InvalidBool) + assert.ErrorIs(t, err, ErrInvalidBool) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -262,7 +262,7 @@ func TestDecoderUint8(t *testing.T) { assert.Equal(t, v, value) value, err = d.Uint8() - assert.ErrorIs(t, err, InvalidUint8) + assert.ErrorIs(t, err, ErrInvalidUint8) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -289,7 +289,7 @@ func TestDecoderUint16(t *testing.T) { assert.Equal(t, v, value) value, err = d.Uint16() - assert.ErrorIs(t, err, InvalidUint16) + assert.ErrorIs(t, err, ErrInvalidUint16) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -316,7 +316,7 @@ func TestDecoderUint32(t *testing.T) { assert.Equal(t, v, value) value, err = d.Uint32() - assert.ErrorIs(t, err, InvalidUint32) + assert.ErrorIs(t, err, ErrInvalidUint32) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -343,7 +343,7 @@ func TestDecoderUint64(t *testing.T) { assert.Equal(t, v, value) value, err = d.Uint64() - assert.ErrorIs(t, err, InvalidUint64) + assert.ErrorIs(t, err, ErrInvalidUint64) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -370,7 +370,7 @@ func TestDecoderInt32(t *testing.T) { assert.Equal(t, v, value) value, err = d.Int32() - assert.ErrorIs(t, err, InvalidInt32) + assert.ErrorIs(t, err, ErrInvalidInt32) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -397,7 +397,7 @@ func TestDecoderInt64(t *testing.T) { assert.Equal(t, v, value) value, err = d.Int64() - assert.ErrorIs(t, err, InvalidInt64) + assert.ErrorIs(t, err, ErrInvalidInt64) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -424,7 +424,7 @@ func TestDecoderFloat32(t *testing.T) { assert.Equal(t, v, value) value, err = d.Float32() - assert.ErrorIs(t, err, InvalidFloat32) + assert.ErrorIs(t, err, ErrInvalidFloat32) p.Reset() n := testing.AllocsPerRun(100, func() { @@ -451,7 +451,7 @@ func TestDecoderFloat64(t *testing.T) { assert.Equal(t, v, value) value, err = d.Float64() - assert.ErrorIs(t, err, InvalidFloat64) + assert.ErrorIs(t, err, ErrInvalidFloat64) p.Reset() n := testing.AllocsPerRun(100, func() { diff --git a/generator/golang/templates/decode.templ b/generator/golang/templates/decode.templ index 84843b4..1563772 100644 --- a/generator/golang/templates/decode.templ +++ b/generator/golang/templates/decode.templ @@ -1,7 +1,7 @@ {{define "decode"}} func (x *{{ CamelCase .FullName }}) Decode (b []byte) error { if x == nil { - return NilDecode + return ErrDecodeNil } return x.decode(polyglot.Decoder(b)) } @@ -90,4 +90,4 @@ var err error {{end -}} return nil } -{{end}} \ No newline at end of file +{{end}} diff --git a/generator/golang/templates/errors.templ b/generator/golang/templates/errors.templ index 1539649..4062997 100644 --- a/generator/golang/templates/errors.templ +++ b/generator/golang/templates/errors.templ @@ -1,5 +1,5 @@ {{define "errors"}} var ( - NilDecode = errors.New("cannot decode into a nil root struct") + ErrDecodeNil = errors.New("cannot decode into a nil root struct") ) -{{end}} \ No newline at end of file +{{end}}