Skip to content
Open
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
23 changes: 21 additions & 2 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cast

import (
"errors"
"encoding/json"
"fmt"
"html/template"
"testing"
Expand Down Expand Up @@ -599,6 +600,19 @@ func TestToStringE(t *testing.T) {
}
key := &Key{"foo"}

type Stru struct {
K string
}
stru := &Stru{K:"foo"}

struStr, _ := json.Marshal(stru)

dic := map[string]interface{}{
"obj": map[string]int{"key": 123},
}

dicStr, _ := json.Marshal(dic)

tests := []struct {
input interface{}
expect string
Expand All @@ -620,15 +634,19 @@ func TestToStringE(t *testing.T) {
{false, "false", false},
{nil, "", false},
{[]byte("one time"), "one time", false},
{[]byte("你好!"), "你好!", false},
{[]rune("你好!"), "你好!", false},
{"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},
{template.CSS("a"), "a", false},
{template.HTMLAttr("a"), "a", false},
{dic, string(dicStr), false},
{stru, string(struStr), false},
// errors
{testing.T{}, "", true},
{key, "", true},
{testing.T{}, "{}", false},
{key, "{}", false},
}

for i, test := range tests {
Expand Down Expand Up @@ -1212,6 +1230,7 @@ func TestToTimeEE(t *testing.T) {
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC), false},
{"1600285405", time.Date(2020, 9, 16, 19, 43, 25, 0, time.UTC), false},
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
Expand Down
29 changes: 29 additions & 0 deletions caste.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ func ToStringE(i interface{}) (string, error) {
return strconv.FormatUint(uint64(s), 10), nil
case []byte:
return string(s), nil
case []rune:
return string(s), nil
case template.HTML:
return string(s), nil
case template.URL:
Expand All @@ -847,6 +849,11 @@ func ToStringE(i interface{}) (string, error) {
case error:
return s.Error(), nil
default:
jsonContent, err := json.Marshal(s)
if err == nil {
return string(jsonContent), nil
}

return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}
}
Expand Down Expand Up @@ -1268,6 +1275,28 @@ func StringToDate(s string) (time.Time, error) {
}

func parseDateWith(s string, dates []string) (d time.Time, e error) {
if len(s) == len("1499979655583057426") { // 19
// nano-seconds
if nanoSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
return time.Unix(0, nanoSecs), nil

}
} else if len(s) == len("1499979795437000") { // 16
// micro-seconds
if microSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
return time.Unix(0, microSecs*1000), nil

}
} else if len(s) == len("1332151919000") { // 13
if miliSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
return time.Unix(0, miliSecs*1000*1000), nil
}
} else if len(s) == len("1332151919") { //10
if secs, err := strconv.ParseInt(s, 10, 64); err == nil {
return time.Unix(secs, 0), nil
}
}

for _, dateType := range dates {
if d, e = time.Parse(dateType, s); e == nil {
return
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/spf13/cast

go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down