mirror of https://github.com/spf13/cast.git
feat(map[string]time.Duration): Ability to take a string
This commit is contained in:
parent
f499056ddf
commit
6f66a555e3
|
@ -952,6 +952,8 @@ func TestToStringMapTimeDuration(t *testing.T) {
|
||||||
var stringMapInterface = map[string]interface{}{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
var stringMapInterface = map[string]interface{}{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
||||||
var interfaceMapString = map[interface{}]string{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
var interfaceMapString = map[interface{}]string{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
||||||
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}
|
||||||
|
var jsonString = `{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"}`
|
||||||
|
var invalidJsonString = `{"key 1": "5m", "key 2": "10h", "key 3": "1h15m30.918273645s"`
|
||||||
var emptyString = ""
|
var emptyString = ""
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -963,11 +965,13 @@ func TestToStringMapTimeDuration(t *testing.T) {
|
||||||
{stringMapInterface, expectResult, false},
|
{stringMapInterface, expectResult, false},
|
||||||
{interfaceMapString, expectResult, false},
|
{interfaceMapString, expectResult, false},
|
||||||
{interfaceMapInterface, expectResult, false},
|
{interfaceMapInterface, expectResult, false},
|
||||||
|
{jsonString, expectResult, false},
|
||||||
|
|
||||||
// errors
|
// errors
|
||||||
{nil, nil, true},
|
{nil, nil, true},
|
||||||
{testing.T{}, nil, true},
|
{testing.T{}, nil, true},
|
||||||
{emptyString, nil, true},
|
{emptyString, nil, true},
|
||||||
|
{invalidJsonString, nil, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
@ -975,7 +979,7 @@ func TestToStringMapTimeDuration(t *testing.T) {
|
||||||
|
|
||||||
v, err := ToStringMapTimeDurationE(test.input)
|
v, err := ToStringMapTimeDurationE(test.input)
|
||||||
if test.iserr {
|
if test.iserr {
|
||||||
assert.Error(t, err, errmsg)
|
assert.Error(t, err, errmsg, test)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
caste.go
6
caste.go
|
@ -1103,6 +1103,12 @@ func ToStringMapTimeDurationE(i interface{}) (map[string]time.Duration, error) {
|
||||||
m[ToString(k)] = ToDuration(val)
|
m[ToString(k)] = ToDuration(val)
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
|
case string:
|
||||||
|
m2, err := ToStringMapStringE(i)
|
||||||
|
if err != nil || m2 == nil {
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
|
return ToStringMapTimeDurationE(m2)
|
||||||
default:
|
default:
|
||||||
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]time.Duration", i, i)
|
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]time.Duration", i, i)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue