Skip to content

Commit

Permalink
Merge pull request #3 from lestrrat-go/remove-errors
Browse files Browse the repository at this point in the history
remove the use github.com/pkg/errors
  • Loading branch information
lestrrat authored Mar 29, 2022
2 parents 628869e + 1edc44c commit 0253cc4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
21 changes: 10 additions & 11 deletions arrayiter/arrayiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package arrayiter

import (
"context"
"fmt"
"reflect"
"sync"

"github.com/pkg/errors"
)

func Iterate(ctx context.Context, a interface{}) (Iterator, error) {
Expand All @@ -14,7 +13,7 @@ func Iterate(ctx context.Context, a interface{}) (Iterator, error) {
switch arv.Kind() {
case reflect.Array, reflect.Slice:
default:
return nil, errors.Errorf(`argument must be an array/slice (%s)`, arv.Type())
return nil, fmt.Errorf(`argument must be an array/slice (%s)`, arv.Type())
}

ch := make(chan *Pair)
Expand Down Expand Up @@ -119,7 +118,7 @@ func Walk(ctx context.Context, s Source, v Visitor) error {
for i := s.Iterate(ctx); i.Next(ctx); {
pair := i.Pair()
if err := v.Visit(pair.Index, pair.Value); err != nil {
return errors.Wrapf(err, `failed to visit index %d`, pair.Index)
return fmt.Errorf(`failed to visit index %d: %w`, pair.Index, err)
}
}
return nil
Expand All @@ -131,13 +130,13 @@ func AsArray(ctx context.Context, s interface{}, v interface{}) error {
case reflect.Array, reflect.Slice:
x, err := Iterate(ctx, s)
if err != nil {
return errors.Wrap(err, `failed to iterate over array/slice type`)
return fmt.Errorf(`failed to iterate over array/slice type: %w`, err)
}
iter = x
default:
ssrc, ok := s.(Source)
if !ok {
return errors.Errorf(`cannot iterate over %T: not a arrayiter.Source type`, s)
return fmt.Errorf(`cannot iterate over %T: not a arrayiter.Source type`, s)
}
iter = ssrc.Iterate(ctx)
}
Expand All @@ -146,14 +145,14 @@ func AsArray(ctx context.Context, s interface{}, v interface{}) error {

// dst MUST be a pointer to a array type
if kind := dst.Kind(); kind != reflect.Ptr {
return errors.Errorf(`dst must be a pointer to a array (%s)`, dst.Type())
return fmt.Errorf(`dst must be a pointer to a array (%s)`, dst.Type())
}

dst = dst.Elem()
switch dst.Kind() {
case reflect.Array, reflect.Slice:
default:
return errors.Errorf(`dst must be a pointer to an array or slice (%s)`, dst.Type())
return fmt.Errorf(`dst must be a pointer to an array or slice (%s)`, dst.Type())
}

var pairs []*Pair
Expand All @@ -165,7 +164,7 @@ func AsArray(ctx context.Context, s interface{}, v interface{}) error {
switch dst.Kind() {
case reflect.Array:
if len(pairs) < dst.Len() {
return errors.Errorf(`dst array does not have enough space for elements (%d, want %d)`, dst.Len(), len(pairs))
return fmt.Errorf(`dst array does not have enough space for elements (%d, want %d)`, dst.Len(), len(pairs))
}
case reflect.Slice:
if dst.IsNil() {
Expand All @@ -175,15 +174,15 @@ func AsArray(ctx context.Context, s interface{}, v interface{}) error {

// dst must be assignable
if !dst.CanSet() {
return errors.New(`dst is not writeable`)
return fmt.Errorf(`dst is not writeable`)
}

elemtyp := dst.Type().Elem()
for _, pair := range pairs {
rvvalue := reflect.ValueOf(pair.Value)

if !rvvalue.Type().AssignableTo(elemtyp) {
return errors.Errorf(`cannot assign key of type %s to map key of type %s`, rvvalue.Type(), elemtyp)
return fmt.Errorf(`cannot assign key of type %s to map key of type %s`, rvvalue.Type(), elemtyp)
}

dst.Index(pair.Index).Set(rvvalue)
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/lestrrat-go/iter

go 1.13

require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
)
require github.com/stretchr/testify v1.5.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
21 changes: 10 additions & 11 deletions mapiter/mapiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package mapiter

import (
"context"
"fmt"
"reflect"
"sync"

"github.com/pkg/errors"
)

// Iterate creates an iterator from arbitrary map types. This is not
Expand All @@ -17,7 +16,7 @@ func Iterate(ctx context.Context, m interface{}) (Iterator, error) {
mrv := reflect.ValueOf(m)

if mrv.Kind() != reflect.Map {
return nil, errors.Errorf(`argument must be a map (%s)`, mrv.Type())
return nil, fmt.Errorf(`argument must be a map (%s)`, mrv.Type())
}

ch := make(chan *Pair)
Expand Down Expand Up @@ -121,7 +120,7 @@ func Walk(ctx context.Context, s Source, v Visitor) error {
for i := s.Iterate(ctx); i.Next(ctx); {
pair := i.Pair()
if err := v.Visit(pair.Key, pair.Value); err != nil {
return errors.Wrapf(err, `failed to visit key %s`, pair.Key)
return fmt.Errorf(`failed to visit key %s: %w`, pair.Key, err)
}
}
return nil
Expand All @@ -134,13 +133,13 @@ func AsMap(ctx context.Context, s interface{}, v interface{}) error {
case reflect.Map:
x, err := Iterate(ctx, s)
if err != nil {
return errors.Wrap(err, `failed to iterate over map type`)
return fmt.Errorf(`failed to iterate over map type: %w`, err)
}
iter = x
default:
ssrc, ok := s.(Source)
if !ok {
return errors.Errorf(`cannot iterate over %T: not a mapiter.Source type`, s)
return fmt.Errorf(`cannot iterate over %T: not a mapiter.Source type`, s)
}
iter = ssrc.Iterate(ctx)
}
Expand All @@ -149,12 +148,12 @@ func AsMap(ctx context.Context, s interface{}, v interface{}) error {

// dst MUST be a pointer to a map type
if kind := dst.Kind(); kind != reflect.Ptr {
return errors.Errorf(`dst must be a pointer to a map (%s)`, dst.Type())
return fmt.Errorf(`dst must be a pointer to a map (%s)`, dst.Type())
}

dst = dst.Elem()
if dst.Kind() != reflect.Map {
return errors.Errorf(`dst must be a pointer to a map (%s)`, dst.Type())
return fmt.Errorf(`dst must be a pointer to a map (%s)`, dst.Type())
}

if dst.IsNil() {
Expand All @@ -163,7 +162,7 @@ func AsMap(ctx context.Context, s interface{}, v interface{}) error {

// dst must be assignable
if !dst.CanSet() {
return errors.New(`dst is not writeable`)
return fmt.Errorf(`dst is not writeable`)
}

keytyp := dst.Type().Key()
Expand All @@ -176,7 +175,7 @@ func AsMap(ctx context.Context, s interface{}, v interface{}) error {
rvvalue := reflect.ValueOf(pair.Value)

if !rvkey.Type().AssignableTo(keytyp) {
return errors.Errorf(`cannot assign key of type %s to map key of type %s`, rvkey.Type(), keytyp)
return fmt.Errorf(`cannot assign key of type %s to map key of type %s`, rvkey.Type(), keytyp)
}

switch rvvalue.Kind() {
Expand All @@ -185,7 +184,7 @@ func AsMap(ctx context.Context, s interface{}, v interface{}) error {
rvvalue = reflect.New(valtyp).Elem()
default:
if !rvvalue.Type().AssignableTo(valtyp) {
return errors.Errorf(`cannot assign value of type %s to map value of type %s`, rvvalue.Type(), valtyp)
return fmt.Errorf(`cannot assign value of type %s to map value of type %s`, rvvalue.Type(), valtyp)
}
}

Expand Down

0 comments on commit 0253cc4

Please sign in to comment.