Browse Source

use errUnableToCast error instead of fmt.Errorf

pull/64/head
Yan Mikhaylov 5 years ago
parent
commit
25e9ec8a70
3 changed files with 53 additions and 90 deletions
  1. +53
    -47
      caste.go
  2. +0
    -30
      errors.go
  3. +0
    -13
      errors_test.go

+ 53
- 47
caste.go View File

@@ -7,6 +7,7 @@ package cast

import (
"encoding/json"
"errors"
"fmt"
"html/template"
"reflect"
@@ -15,6 +16,11 @@ import (
"time"
)

var (
errNegativeNotAllowed = errors.New("unable to cast negative value")
errUnableToCast = errors.New("unable to cast")
)

// ToTimeE casts an interface to a time.Time type.
func ToTimeE(i interface{}) (tim time.Time, err error) {
i = indirect(i)
@@ -37,7 +43,7 @@ func ToTimeE(i interface{}) (tim time.Time, err error) {
case uint32:
return time.Unix(int64(v), 0), nil
default:
return time.Time{}, newError("unable to cast %#v of type %T to Time", i, i)
return time.Time{}, errUnableToCast
}
}

@@ -62,7 +68,7 @@ func ToDurationE(i interface{}) (d time.Duration, err error) {
}
return
default:
err = newError("unable to cast %#v of type %T to Duration", i, i)
err = errUnableToCast
return
}
}
@@ -84,7 +90,7 @@ func ToBoolE(i interface{}) (bool, error) {
case string:
return strconv.ParseBool(i.(string))
default:
return false, newError("unable to cast %#v of type %T to bool", i, i)
return false, errUnableToCast
}
}

@@ -122,14 +128,14 @@ func ToFloat64E(i interface{}) (float64, error) {
if err == nil {
return v, nil
}
return 0, newError("unable to cast %#v of type %T to float64", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
}
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to float64", i, i)
return 0, errUnableToCast
}
}

@@ -167,14 +173,14 @@ func ToFloat32E(i interface{}) (float32, error) {
if err == nil {
return float32(v), nil
}
return 0, newError("unable to cast %#v of type %T to float32", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
}
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to float32", i, i)
return 0, errUnableToCast
}
}

@@ -212,7 +218,7 @@ func ToInt64E(i interface{}) (int64, error) {
if err == nil {
return v, nil
}
return 0, newError("unable to cast %#v of type %T to int64", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
@@ -221,7 +227,7 @@ func ToInt64E(i interface{}) (int64, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to int64", i, i)
return 0, errUnableToCast
}
}

@@ -259,7 +265,7 @@ func ToInt32E(i interface{}) (int32, error) {
if err == nil {
return int32(v), nil
}
return 0, newError("unable to cast %#v of type %T to int32", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
@@ -268,7 +274,7 @@ func ToInt32E(i interface{}) (int32, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to int32", i, i)
return 0, errUnableToCast
}
}

@@ -306,7 +312,7 @@ func ToInt16E(i interface{}) (int16, error) {
if err == nil {
return int16(v), nil
}
return 0, newError("unable to cast %#v of type %T to int16", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
@@ -315,7 +321,7 @@ func ToInt16E(i interface{}) (int16, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to int16", i, i)
return 0, errUnableToCast
}
}

@@ -353,7 +359,7 @@ func ToInt8E(i interface{}) (int8, error) {
if err == nil {
return int8(v), nil
}
return 0, newError("unable to cast %#v of type %T to int8", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
@@ -362,7 +368,7 @@ func ToInt8E(i interface{}) (int8, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to int8", i, i)
return 0, errUnableToCast
}
}

@@ -400,7 +406,7 @@ func ToIntE(i interface{}) (int, error) {
if err == nil {
return int(v), nil
}
return 0, newError("unable to cast %#v of type %T to int", i, i)
return 0, errUnableToCast
case bool:
if s {
return 1, nil
@@ -409,7 +415,7 @@ func ToIntE(i interface{}) (int, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to int", i, i)
return 0, errUnableToCast
}
}

@@ -423,7 +429,7 @@ func ToUintE(i interface{}) (uint, error) {
if err == nil {
return uint(v), nil
}
return 0, newError("unable to cast %#v to uint: %s", i, err)
return 0, errUnableToCast
case int:
if s < 0 {
return 0, errNegativeNotAllowed
@@ -477,7 +483,7 @@ func ToUintE(i interface{}) (uint, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to uint", i, i)
return 0, errUnableToCast
}
}

@@ -491,7 +497,7 @@ func ToUint64E(i interface{}) (uint64, error) {
if err == nil {
return v, nil
}
return 0, newError("unable to cast %#v to uint64: %s", i, err)
return 0, errUnableToCast
case int:
if s < 0 {
return 0, errNegativeNotAllowed
@@ -545,7 +551,7 @@ func ToUint64E(i interface{}) (uint64, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to uint64", i, i)
return 0, errUnableToCast
}
}

@@ -559,7 +565,7 @@ func ToUint32E(i interface{}) (uint32, error) {
if err == nil {
return uint32(v), nil
}
return 0, newError("unable to cast %#v to uint32: %s", i, err)
return 0, errUnableToCast
case int:
if s < 0 {
return 0, errNegativeNotAllowed
@@ -613,7 +619,7 @@ func ToUint32E(i interface{}) (uint32, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to uint32", i, i)
return 0, errUnableToCast
}
}

@@ -627,7 +633,7 @@ func ToUint16E(i interface{}) (uint16, error) {
if err == nil {
return uint16(v), nil
}
return 0, newError("unable to cast %#v to uint16: %s", i, err)
return 0, errUnableToCast
case int:
if s < 0 {
return 0, errNegativeNotAllowed
@@ -681,7 +687,7 @@ func ToUint16E(i interface{}) (uint16, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to uint16", i, i)
return 0, errUnableToCast
}
}

@@ -695,7 +701,7 @@ func ToUint8E(i interface{}) (uint8, error) {
if err == nil {
return uint8(v), nil
}
return 0, newError("unable to cast %#v to uint8: %s", i, err)
return 0, errUnableToCast
case int:
if s < 0 {
return 0, errNegativeNotAllowed
@@ -749,7 +755,7 @@ func ToUint8E(i interface{}) (uint8, error) {
case nil:
return 0, nil
default:
return 0, newError("unable to cast %#v of type %T to uint8", i, i)
return 0, errUnableToCast
}
}

@@ -844,7 +850,7 @@ func ToStringE(i interface{}) (string, error) {
case error:
return s.Error(), nil
default:
return "", newError("unable to cast %#v of type %T to string", i, i)
return "", errUnableToCast
}
}

@@ -874,7 +880,7 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
return m, newError("unable to cast %#v of type %T to map[string]string", i, i)
return m, errUnableToCast
}
}

@@ -925,11 +931,11 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
for k, val := range v {
key, err := ToStringE(k)
if err != nil {
return m, newError("unable to cast %#v of type %T to map[string][]string", i, i)
return m, errUnableToCast
}
value, err := ToStringSliceE(val)
if err != nil {
return m, newError("unable to cast %#v of type %T to map[string][]string", i, i)
return m, errUnableToCast
}
m[key] = value
}
@@ -937,7 +943,7 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
return m, newError("unable to cast %#v of type %T to map[string][]string", i, i)
return m, errUnableToCast
}
return m, nil
}
@@ -963,7 +969,7 @@ func ToStringMapBoolE(i interface{}) (map[string]bool, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
return m, newError("unable to cast %#v of type %T to map[string]bool", i, i)
return m, errUnableToCast
}
}

@@ -983,7 +989,7 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
return m, newError("unable to cast %#v of type %T to map[string]interface{}", i, i)
return m, errUnableToCast
}
}

@@ -1000,14 +1006,14 @@ func ToSliceE(i interface{}) ([]interface{}, error) {
}
return s, nil
default:
return s, newError("unable to cast %#v of type %T to []interface{}", i, i)
return s, errUnableToCast
}
}

// ToBoolSliceE casts an interface to a []bool type.
func ToBoolSliceE(i interface{}) ([]bool, error) {
if i == nil {
return []bool{}, newError("unable to cast %#v of type %T to []bool", i, i)
return []bool{}, errUnableToCast
}

switch v := i.(type) {
@@ -1023,13 +1029,13 @@ func ToBoolSliceE(i interface{}) ([]bool, error) {
for j := 0; j < s.Len(); j++ {
val, err := ToBoolE(s.Index(j).Interface())
if err != nil {
return []bool{}, newError("unable to cast %#v of type %T to []bool", i, i)
return []bool{}, errUnableToCast
}
a[j] = val
}
return a, nil
default:
return []bool{}, newError("unable to cast %#v of type %T to []bool", i, i)
return []bool{}, errUnableToCast
}
}

@@ -1050,18 +1056,18 @@ func ToStringSliceE(i interface{}) ([]string, error) {
case interface{}:
str, err := ToStringE(v)
if err != nil {
return a, newError("unable to cast %#v of type %T to []string", i, i)
return a, errUnableToCast
}
return []string{str}, nil
default:
return a, newError("unable to cast %#v of type %T to []string", i, i)
return a, errUnableToCast
}
}

// ToIntSliceE casts an interface to a []int type.
func ToIntSliceE(i interface{}) ([]int, error) {
if i == nil {
return []int{}, newError("unable to cast %#v of type %T to []int", i, i)
return []int{}, errUnableToCast
}

switch v := i.(type) {
@@ -1077,20 +1083,20 @@ func ToIntSliceE(i interface{}) ([]int, error) {
for j := 0; j < s.Len(); j++ {
val, err := ToIntE(s.Index(j).Interface())
if err != nil {
return []int{}, newError("unable to cast %#v of type %T to []int", i, i)
return []int{}, errUnableToCast
}
a[j] = val
}
return a, nil
default:
return []int{}, newError("unable to cast %#v of type %T to []int", i, i)
return []int{}, errUnableToCast
}
}

// ToDurationSliceE casts an interface to a []time.Duration type.
func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
if i == nil {
return []time.Duration{}, newError("unable to cast %#v of type %T to []time.Duration", i, i)
return []time.Duration{}, errUnableToCast
}

switch v := i.(type) {
@@ -1106,13 +1112,13 @@ func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
for j := 0; j < s.Len(); j++ {
val, err := ToDurationE(s.Index(j).Interface())
if err != nil {
return []time.Duration{}, newError("unable to cast %#v of type %T to []time.Duration", i, i)
return []time.Duration{}, errUnableToCast
}
a[j] = val
}
return a, nil
default:
return []time.Duration{}, newError("unable to cast %#v of type %T to []time.Duration", i, i)
return []time.Duration{}, errUnableToCast
}
}

@@ -1152,7 +1158,7 @@ func parseDateWith(s string, dates []string) (d time.Time, e error) {
return
}
}
return d, newError("unable to parse date: %s", s)
return d, errUnableToCast
}

// jsonStringToObject attempts to unmarshall a string as JSON into


+ 0
- 30
errors.go View File

@@ -1,30 +0,0 @@
package cast

import (
"errors"
"fmt"
"sync"
)

var errNegativeNotAllowed = errors.New("unable to cast negative value")

type lazyError struct {
format string
args []interface{}
once sync.Once
result string
}

func (e *lazyError) Error() string {
e.once.Do(func() {
e.result = fmt.Sprintf(e.format, e.args...)
})
return e.result
}

func newError(format string, args ...interface{}) error {
return &lazyError{
format: format,
args: args,
}
}

+ 0
- 13
errors_test.go View File

@@ -1,13 +0,0 @@
package cast

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestLazyError_Error(t *testing.T) {
err := newError("foo")

assert.NotNil(t, err)
assert.Equal(t, err.Error(), "foo")
}

Loading…
Cancel
Save