Browse Source

Add proper support for error -> string and Stringer -> string.

tags/v1.0.0
spf13 9 years ago
parent
commit
421411cf35
2 changed files with 36 additions and 11 deletions
  1. +31
    -1
      cast_test.go
  2. +5
    -10
      caste.go

+ 31
- 1
cast_test.go View File

@@ -8,8 +8,9 @@ package cast
import (
"testing"

"github.com/stretchr/testify/assert"
"html/template"

"github.com/stretchr/testify/assert"
)

func TestToInt(t *testing.T) {
@@ -40,6 +41,35 @@ func TestToString(t *testing.T) {
assert.Equal(t, ToString(nil), "")
}

type foo struct {
val string
}

func (x foo) String() string {
return x.val
}

func TestStringerToString(t *testing.T) {

var x foo
x.val = "bar"
assert.Equal(t, "bar", ToString(x))
}

type fu struct {
val string
}

func (x fu) Error() string {
return x.val
}

func TestErrorToString(t *testing.T) {
var x fu
x.val = "bar"
assert.Equal(t, "bar", ToString(x))
}

func TestMaps(t *testing.T) {
var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
var stringMapBool = map[interface{}]interface{}{"v1": true, "v2": false}


+ 5
- 10
caste.go View File

@@ -119,10 +119,6 @@ func ToIntE(i interface{}) (int, error) {
}
}

var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)

func ToStringE(i interface{}) (string, error) {
jww.DEBUG.Println("ToStringE called on type:", reflect.TypeOf(i))
@@ -140,13 +136,12 @@ func ToStringE(i interface{}) (string, error) {
return string(s), nil
case nil:
return "", nil
case fmt.Stringer:
return s.String(), nil
case error:
return s.Error(), nil
default:
// shamelessly borrowed from html/template
v := reflect.ValueOf(i)
for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return fmt.Sprint(v), nil
return "", fmt.Errorf("Unable to Cast %#v to string", i)
}
}



Loading…
Cancel
Save