From 4b1e5dfe94d7cc203b2af405f18ee6a7746fba05 Mon Sep 17 00:00:00 2001 From: Baodong Date: Sat, 10 Oct 2020 11:41:53 +0800 Subject: [PATCH] feat(ToStringMapStringE): Add support for non string JOSN value --- cast_test.go | 3 +++ caste.go | 7 +++++-- go.mod | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cast_test.go b/cast_test.go index cc4ebce..c6f31d6 100644 --- a/cast_test.go +++ b/cast_test.go @@ -907,6 +907,8 @@ func TestToStringMapStringE(t *testing.T) { var jsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}` var invalidJsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"` var emptyString = "" + var jsonStringWithNonString = `{"key 1": "100", "key 2": -200, "key 3": 3.1415926}` + var mapStringWithNonString = map[string]string{"key 1": "100", "key 2": "-200", "key 3": "3.1415926"} tests := []struct { input interface{} @@ -918,6 +920,7 @@ func TestToStringMapStringE(t *testing.T) { {interfaceMapString, stringMapString, false}, {interfaceMapInterface, stringMapString, false}, {jsonString, stringMapString, false}, + {jsonStringWithNonString, mapStringWithNonString, false}, // errors {nil, nil, true}, diff --git a/caste.go b/caste.go index 9ac1015..6e6f352 100644 --- a/caste.go +++ b/caste.go @@ -874,8 +874,11 @@ func ToStringMapStringE(i interface{}) (map[string]string, error) { } return m, nil case string: - err := jsonStringToObject(v, &m) - return m, err + var temp = map[string]interface{}{} + if err := jsonStringToObject(v, &temp); err != nil { + return nil, err + } + return ToStringMapStringE(temp) default: return m, fmt.Errorf("unable to cast %#v of type %T to map[string]string", i, i) } diff --git a/go.mod b/go.mod index c1c0232..d632f76 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/spf13/cast +go 1.15 + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect