Skip to content

Commit

Permalink
bump go to 1.20; drop pkg/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Oct 1, 2023
1 parent b4a6856 commit db22828
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go: [1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18]
go: [1.15, 1.16, 1.17, 1.18, 1.19, "1.20", 1.21]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand All @@ -29,4 +29,4 @@ jobs:
run: go test ./...

- name: Race
run: go test -race ./...
run: go test -race -count 10 ./...
33 changes: 16 additions & 17 deletions decode.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ubjson

import (
"errors"
"fmt"
"io"
"reflect"

"github.com/pkg/errors"
)

// MaxCollectionAlloc is the default maximum collection capacity allocation.
Expand Down Expand Up @@ -48,7 +47,7 @@ func (d *Decoder) DecodeValue(v Value) error {
// decodeValue asserts a value's type marker, then decodes the data.
func (d *Decoder) decodeValue(m Marker, decodeData func(*Decoder) error) error {
if r, err := d.readValType(); err != nil {
return errors.Wrapf(err, "failed trying to read type '%s'", m)
return fmt.Errorf("failed trying to read type '%s': %w", m, err)
} else if r != m {
return errWrongTypeRead(m, r)
}
Expand All @@ -59,7 +58,7 @@ func (d *Decoder) decodeValue(m Marker, decodeData func(*Decoder) error) error {
func (d *Decoder) assertType(m Marker) error {
r, err := d.readMarker()
if err != nil {
return errors.Wrapf(err, "failed trying to read type '%s'", m)
return fmt.Errorf("failed trying to read type '%s': %w", m, err)
}
if r != m {
return errWrongTypeRead(m, r)
Expand Down Expand Up @@ -587,7 +586,7 @@ func (d *Decoder) Decode(v interface{}) error {

value := reflect.ValueOf(v)
if value.Kind() != reflect.Ptr {
return errors.Errorf("can only decode into pointers, not: %s", value.Type())
return fmt.Errorf("can only decode into pointers, not: %s", value.Type())
}
// Containers
switch value.Elem().Kind() {
Expand Down Expand Up @@ -618,7 +617,7 @@ func arrayToArray(arrayPtr reflect.Value) func(*ArrayDecoder) error {
elemType := arrayValue.Type().Elem()
if ad.Len > 0 {
if ad.Len >= 0 && ad.Len != arrayValue.Len() {
return errors.Errorf("unable to decode data length %d into array of length %d", ad.Len, arrayValue.Len())
return fmt.Errorf("unable to decode data length %d into array of length %d", ad.Len, arrayValue.Len())
}
}

Expand Down Expand Up @@ -649,7 +648,7 @@ func arrayToSlice(slicePtr reflect.Value) func(*ArrayDecoder) error {
sliceValue = reflect.Append(sliceValue, elemPtr.Elem())
}
} else if ad.Len > ad.MaxCollectionAlloc {
return errors.Errorf("collection exceeds max allocation limit of %d: %d", ad.MaxCollectionAlloc, ad.Len)
return fmt.Errorf("collection exceeds max allocation limit of %d: %d", ad.MaxCollectionAlloc, ad.Len)
} else {
sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), ad.Len, ad.Len))

Expand All @@ -672,18 +671,18 @@ func objectIntoStruct(structPtr reflect.Value) func(*ObjectDecoder) error {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return errors.Wrapf(err, "failed to decode key with call #%d", o.count)
return fmt.Errorf("failed to decode key with call #%d: %w", o.count, err)
}
structValue := structPtr.Elem()
f := fieldByName(structValue, k)
if f == zeroValue {
// Discard value with no matching field.
// TODO could be more efficient with custom discardValue() method
if _, err := o.decodeInterface(); err != nil {
return errors.Wrapf(err, "failed to discard value for %q with call #%d", k, o.count)
return fmt.Errorf("failed to discard value for %q with call #%d: %w", k, o.count, err)
}
} else if err := o.Decode(f.Addr().Interface()); err != nil {
return errors.Wrapf(err, "failed to decode value for %q with call #%d", k, o.count)
return fmt.Errorf("failed to decode value for %q with call #%d: %w", k, o.count, err)
}
}
return o.End()
Expand All @@ -703,7 +702,7 @@ func fieldByName(structValue reflect.Value, k string) reflect.Value {
func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
return func(o *ObjectDecoder) error {
if o.Len > o.MaxCollectionAlloc {
return errors.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
return fmt.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
}
mapValue := mapPtr.Elem()
mapValue.Set(makeMap(mapValue.Type(), o.Len))
Expand All @@ -712,13 +711,13 @@ func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return errors.Wrapf(err, "failed to decode key #%d", o.count)
return fmt.Errorf("failed to decode key #%d: %w", o.count, err)
}

valPtr := reflect.New(elemType)

if err := o.Decode(valPtr.Interface()); err != nil {
return errors.Wrapf(err, "failed to decode value #%d", o.count)
return fmt.Errorf("failed to decode value #%d: %w", o.count, err)
}

mapValue.SetMapIndex(reflect.ValueOf(k), valPtr.Elem())
Expand All @@ -731,7 +730,7 @@ func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
// either interface{} or a stricter type if the object is strongly typed.
func objectAsInterface(o *ObjectDecoder) (interface{}, error) {
if o.Len > o.MaxCollectionAlloc {
return nil, errors.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
return nil, fmt.Errorf("collection exceeds max allocation limit of %d: %d", o.MaxCollectionAlloc, o.Len)
}
if o.ValType == NoOpMarker {
return nil, errors.New("No-Op (N) is not a legal strong type")
Expand All @@ -742,11 +741,11 @@ func objectAsInterface(o *ObjectDecoder) (interface{}, error) {
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
return nil, errors.Wrapf(err, "failed to decode key #%d", o.count)
return nil, fmt.Errorf("failed to decode key #%d: %w", o.count, err)
}
valPtr := reflect.New(mapType.Elem())
if err := o.Decode(valPtr.Interface()); err != nil {
return nil, errors.Wrapf(err, "failed to decode value #%d", o.count)
return nil, fmt.Errorf("failed to decode value #%d: %w", o.count, err)
}

mapValue.SetMapIndex(reflect.ValueOf(k), valPtr.Elem())
Expand Down Expand Up @@ -774,7 +773,7 @@ func arrayAsInterface(a *ArrayDecoder) (interface{}, error) {
sliceValue = reflect.Append(sliceValue, elemPtr.Elem())
}
} else if a.Len > a.MaxCollectionAlloc {
return "", errors.Errorf("collection exceeds max allocation limit of %d: %d", a.MaxCollectionAlloc, a.Len)
return "", fmt.Errorf("collection exceeds max allocation limit of %d: %d", a.MaxCollectionAlloc, a.Len)
} else {
sliceValue = reflect.MakeSlice(sliceType, a.Len, a.Len)

Expand Down
24 changes: 12 additions & 12 deletions encode.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ubjson

import (
"errors"
"fmt"
"io"
"reflect"

"github.com/pkg/errors"
)

// Encoder provides methods for encoding UBJSON data types.
Expand Down Expand Up @@ -115,7 +115,7 @@ func (e *Encoder) EncodeInt(v int) error {
case Int64Marker:
return e.EncodeInt64(int64(v))
default:
return errors.Errorf("unsupported marker: %s", string(m))
return fmt.Errorf("unsupported marker: %s", string(m))
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ func (a *ArrayEncoder) End() error {
return err
}
} else if a.len != a.count {
return errors.Errorf("unable to end array of length %d after %d elements", a.len, a.count)
return fmt.Errorf("unable to end array of length %d after %d elements", a.len, a.count)
}

return a.Flush()
Expand Down Expand Up @@ -339,7 +339,7 @@ func (o *ObjectEncoder) End() error {
return err
}
} else if 2*o.len != o.count {
return errors.Errorf("unable to end map of %d entries after %d", o.len, o.count/2)
return fmt.Errorf("unable to end map of %d entries after %d", o.len, o.count/2)
}

return o.Flush()
Expand Down Expand Up @@ -464,7 +464,7 @@ func (e *Encoder) Encode(v interface{}) error {

case reflect.Map:
if k := value.Type().Key().Kind(); k != reflect.String {
return errors.Errorf("unable to encode map of type %s: key reflect.Kind must be reflect.String but is %s", value.Type(), k)
return fmt.Errorf("unable to encode map of type %s: key reflect.Kind must be reflect.String but is %s", value.Type(), k)
}
return e.encode(ObjectStartMarker, encodeMap(value))

Expand All @@ -478,7 +478,7 @@ func (e *Encoder) Encode(v interface{}) error {
return e.Encode(value.Elem().Interface())
}

return errors.Errorf("unable to encode value: %v", v)
return fmt.Errorf("unable to encode value: %v", v)
}

func encodeArray(arrayValue reflect.Value) func(*Encoder) error {
Expand All @@ -501,7 +501,7 @@ func encodeArray(arrayValue reflect.Value) func(*Encoder) error {

for i := 0; i < arrayValue.Len(); i++ {
if err := ae.Encode(arrayValue.Index(i).Interface()); err != nil {
return errors.Wrapf(err, "failed to encode array element %d", i)
return fmt.Errorf("failed to encode array element %d: %w", i, err)
}
}

Expand Down Expand Up @@ -533,10 +533,10 @@ func encodeMap(mapValue reflect.Value) func(*Encoder) error {

for _, key := range keys {
if err := o.EncodeKey(key.String()); err != nil {
return errors.Wrapf(err, "failed to encode key %q", key.String())
return fmt.Errorf("failed to encode key %q: %w", key.String(), err)
}
if err := o.Encode(mapValue.MapIndex(key).Interface()); err != nil {
return errors.Wrapf(err, "failed to encode value for key %q", key.String())
return fmt.Errorf("failed to encode value for key %q: %w", key.String(), err)
}
}

Expand All @@ -562,11 +562,11 @@ func encodeStruct(structValue reflect.Value) func(*Encoder) error {
panic("invalid cached type info: no index for field " + name)
}
if err := o.EncodeKey(name); err != nil {
return errors.Wrapf(err, "failed to encode key %q", name)
return fmt.Errorf("failed to encode key %q: %w", name, err)
}
val := structValue.Field(i).Interface()
if err := o.Encode(val); err != nil {
return errors.Wrapf(err, "failed to encode value for key %q", name)
return fmt.Errorf("failed to encode value for key %q: %w", name, err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package ubjson

import "github.com/pkg/errors"
import "fmt"

func errTooMany(len int) error {
return errors.Errorf("too many calls for container with len %d", len)
return fmt.Errorf("too many calls for container with len %d", len)
}

func errWrongTypeWrite(exp, got Marker) error {
return errors.Errorf("unable to write element type '%s' to container type '%s'", got, exp)
return fmt.Errorf("unable to write element type '%s' to container type '%s'", got, exp)
}

func errWrongTypeRead(exp, got Marker) error {
return errors.Errorf("tried to read type '%s' but found type '%s'", exp, got)
return fmt.Errorf("tried to read type '%s' but found type '%s'", exp, got)
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/jmank88/ubjson

go 1.18

require github.com/pkg/errors v0.8.1
go 1.20
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
29 changes: 14 additions & 15 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package ubjson
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"strconv"

"github.com/pkg/errors"
)

// A reader reads UBJSON types.
Expand Down Expand Up @@ -85,7 +84,7 @@ func readContainer(r reader) (Marker, int, error) {
return 0, 0, err
}
if l < 0 {
return 0, 0, errors.Errorf("illegal negative container length: %d", l)
return 0, 0, fmt.Errorf("illegal negative container length: %d", l)
}
return m, l, nil

Expand All @@ -98,7 +97,7 @@ func readContainer(r reader) (Marker, int, error) {
return 0, 0, err
}
if l < 0 {
return 0, 0, errors.Errorf("illegal negative container length: %d", l)
return 0, 0, fmt.Errorf("illegal negative container length: %d", l)
}
return 0, l, nil

Expand Down Expand Up @@ -209,20 +208,20 @@ func (r *binaryReader) readFloat64() (float64, error) {
func (r *binaryReader) readString(max int) (string, error) {
l, err := readInt(r)
if err != nil {
return "", errors.Wrap(err, "failed to read string length prefix")
return "", fmt.Errorf("failed to read string length prefix: %w", err)
}
switch {
case l == 0:
return "", nil
case l < 0:
return "", errors.Errorf("illegal string length prefix: %d", l)
return "", fmt.Errorf("illegal string length prefix: %d", l)
case l > max:
return "", errors.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
return "", fmt.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
}
b := make([]byte, l)
n, err := r.Read(b)
if err != nil {
return "", errors.Wrap(err, "failed to read string bytes")
return "", fmt.Errorf("failed to read string bytes: %w", err)
}
if n != l {
return "", fmt.Errorf("failed to read full string length (%d), instead got: %s", l, string(b[:n]))
Expand Down Expand Up @@ -265,7 +264,7 @@ func (r *blockReader) nextBlock() (string, error) {
// The readBlock method reads the next block.
func (r *blockReader) readBlock() (string, error) {
if _, err := r.ReadBytes('['); err != nil {
return "", errors.Wrap(err, "failed to read block start")
return "", fmt.Errorf("failed to read block start: %w", err)
}
s, err := r.ReadString(']')
if err != nil {
Expand Down Expand Up @@ -318,7 +317,7 @@ func (r *blockReader) readUInt8() (uint8, error) {
}
i, err := strconv.ParseUint(s, 10, 8)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse uint8")
return 0, fmt.Errorf("failed to parse uint8: %w", err)
}
return uint8(i), nil
}
Expand All @@ -340,7 +339,7 @@ func (r *blockReader) readBlockedInt(bitSize int) (int64, error) {
}
i, err := strconv.ParseInt(s, 10, bitSize)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse int%d", bitSize)
return 0, fmt.Errorf("failed to parse int%d: %w", bitSize, err)
}
return i, nil
}
Expand Down Expand Up @@ -376,19 +375,19 @@ func (r *blockReader) readFloat64() (float64, error) {
func (r *blockReader) readString(max int) (string, error) {
l, err := readInt(r)
if err != nil {
return "", errors.Wrap(err, "failed to read string length prefix")
return "", fmt.Errorf("failed to read string length prefix: %w", err)
}
switch {
case l == 0:
return "", nil
case l < 0:
return "", errors.Errorf("illegal string length prefix: %d", l)
return "", fmt.Errorf("illegal string length prefix: %d", l)
case l > max:
return "", errors.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
return "", fmt.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
}
s, err := r.nextBlock()
if err != nil {
return "", errors.Wrap(err, "failed to read string block")
return "", fmt.Errorf("failed to read string block: %w", err)
}
if len(s) != l {
return "", fmt.Errorf("string length %d but prefix %d", len(s), l)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go test fuzz v1
[]byte("[$d#U\x190000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go test fuzz v1
[]byte("[$C#U\x010")
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
go test fuzz v1
string("[[][$][C][#][U][1][0]")
Loading

0 comments on commit db22828

Please sign in to comment.