Skip to content

Commit

Permalink
Rename errors to follow Go code-style conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperManifolds committed Mar 14, 2024
1 parent a46244b commit 74eb82e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 66 deletions.
4 changes: 2 additions & 2 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
64 changes: 32 additions & 32 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -445,20 +445,20 @@ 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) {
if len(b) > 8 && b[0] == Float64RawKind {
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
}
28 changes: 14 additions & 14 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 74eb82e

Please sign in to comment.