8000 modify function ToStringE to support type redefinition by dablelv · Pull Request #109 · spf13/cast · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

modify function ToStringE to support type redefinition #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ import (
"github.com/stretchr/testify/assert"
)

type (
intAlias int
int8Alias int8
int16Alias int16
int32Alias int32
int64Alias int64
uintAlias uint
uint8Alias uint8
uint16Alias uint16
uint32Alias uint32
uint64Alias uint64
float32Alias float32
float64Alias float64
boolAlias bool
stringAlias string
)

func TestToUintE(t *testing.T) {
tests := []struct {
input interface{}
Expand Down Expand Up @@ -605,22 +622,37 @@ func TestToStringE(t *testing.T) {
iserr bool
}{
{int(8), "8", false},
{intAlias(8), "8", false},
{int8(8), "8", false},
{int8Alias(8), "8", false},
{int16(8), "8", false},
{int16Alias(8), "8", false},
{int32(8), "8", false},
{int32Alias(8), "8", false},
{int64(8), "8", false},
{int64Alias(8), "8", false},
{uint(8), "8", false},
{uintAlias(8), "8", false},
{uint8(8), "8", false},
{uint8Alias(8), "8", false},
{uint16(8), "8", false},
{uint16Alias(8), "8", false},
{uint32(8), "8", false},
{uint32Alias(8), "8", false},
{uint64(8), "8", false},
{uint64Alias(8), "8", false},
{float32(8.31), "8.31", false},
{float32Alias(8.31), "8.31", false},
{float64(8.31), "8.31", false},
{float64Alias(8.31), "8.31", false},
{true, "true", false},
{boolAlias(true), "true", false},
{false, "false", false},
{boolAlias(false), "false", false},
{nil, "", false},
{[]byte("one time"), "one time", false},
{"one more time", "one more time", false},
{stringAlias("one more time"), "one more time", false},
{template.HTML("one time"), "one time", false},
{template.URL("http://somehost.foo"), "http://somehost.foo", false},
{template.JS("(1+2)"), "(1+2)", false},
Expand Down
20 changes: 18 additions & 2 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,25 @@ func ToStringE(i interface{}) (string, error) {
return s.String(), nil
case error:
return s.Error(), nil
default:
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}

// support type redefinition
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.String:
return v.String(), nil
case reflect.Bool:
return strconv.FormatBool(v.Bool()), nil
case reflect.Float32:
return strconv.FormatFloat(v.Float(), 'f', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(v.Float(), 'f', -1, 64), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10), nil
}
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}

// ToStringMapStringE casts an interface to a map[string]string type.
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)

go 1.13
0