Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c2e107f1af | |||
| 5ff9708f43 |
+4
-11
@@ -729,17 +729,10 @@ func ToMap(args ...interface{}) map[string]interface{} {
|
||||
}
|
||||
default:
|
||||
val := reflect.ValueOf(obj)
|
||||
if val.Kind().String() == "map" {
|
||||
tmp := val.MapKeys()
|
||||
for _, key := range tmp {
|
||||
finalArg[key.String()] = val.MapIndex(key).Interface()
|
||||
}
|
||||
} else {
|
||||
res := structToMap(val)
|
||||
for key, value := range res {
|
||||
if value != nil {
|
||||
finalArg[key] = value
|
||||
}
|
||||
res := structToMap(val)
|
||||
for key, value := range res {
|
||||
if value != nil {
|
||||
finalArg[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-17
@@ -361,27 +361,11 @@ type Test struct {
|
||||
}
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
inStr := map[string]string{
|
||||
"tea": "test",
|
||||
"test": "test2",
|
||||
}
|
||||
result := ToMap(inStr)
|
||||
utils.AssertEqual(t, "test", result["tea"])
|
||||
utils.AssertEqual(t, "test2", result["test"])
|
||||
|
||||
inInt := map[string]int{
|
||||
"tea": 12,
|
||||
"test": 13,
|
||||
}
|
||||
result = ToMap(inInt)
|
||||
utils.AssertEqual(t, 12, result["tea"])
|
||||
utils.AssertEqual(t, 13, result["test"])
|
||||
|
||||
in := map[string]*string{
|
||||
"tea": String("test"),
|
||||
"nil": nil,
|
||||
}
|
||||
result = ToMap(in)
|
||||
result := ToMap(in)
|
||||
utils.AssertEqual(t, "test", result["tea"])
|
||||
utils.AssertNil(t, result["nil"])
|
||||
|
||||
|
||||
+3
-23
@@ -6,7 +6,6 @@ import (
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/modern-go/reflect2"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -333,28 +332,9 @@ func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jso
|
||||
}
|
||||
}
|
||||
|
||||
func Stringify(a interface{}) string {
|
||||
switch v := a.(type) {
|
||||
case *string:
|
||||
return StringValue(v)
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case io.Reader:
|
||||
byt, err := ioutil.ReadAll(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(byt)
|
||||
}
|
||||
byt := bytes.NewBuffer([]byte{})
|
||||
jsonEncoder := json.NewEncoder(byt)
|
||||
jsonEncoder.SetEscapeHTML(false)
|
||||
if err := jsonEncoder.Encode(a); err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(bytes.TrimSpace(byt.Bytes()))
|
||||
func Stringify(m interface{}) string {
|
||||
byt, _ := json.Marshal(m)
|
||||
return string(byt)
|
||||
}
|
||||
|
||||
func ParseJSON(a string) interface{} {
|
||||
|
||||
@@ -3,7 +3,6 @@ package dara
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"strings"
|
||||
|
||||
"github.com/alibabacloud-go/tea/utils"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
@@ -879,42 +878,3 @@ func TestUnmarshalWithDefaultDecoders(t *testing.T) {
|
||||
err = jsoniter.Unmarshal(from, toUint64)
|
||||
utils.AssertNotNil(t, err)
|
||||
}
|
||||
|
||||
func Test_Stringify(t *testing.T) {
|
||||
// interface
|
||||
str := Stringify(map[string]interface{}{"test": "ok"})
|
||||
utils.AssertEqual(t, `{"test":"ok"}`, str)
|
||||
// string
|
||||
str = Stringify("test")
|
||||
utils.AssertEqual(t, "test", str)
|
||||
// []byte
|
||||
str = Stringify([]byte("test"))
|
||||
utils.AssertEqual(t, "test", str)
|
||||
// io.Reader
|
||||
str = Stringify(strings.NewReader("test"))
|
||||
utils.AssertEqual(t, "test", str)
|
||||
|
||||
str = Stringify("test")
|
||||
utils.AssertEqual(t, "test", str)
|
||||
}
|
||||
|
||||
|
||||
func Test_ParseJSON(t *testing.T) {
|
||||
obj := ParseJSON(`{"test":"ok"}`).(map[string]interface{})
|
||||
utils.AssertEqual(t, "ok", obj["test"])
|
||||
|
||||
obj1 := ParseJSON(`["test1", "test2", "test3"]`).([]interface{})
|
||||
utils.AssertEqual(t, "test2", obj1[1])
|
||||
|
||||
num := ParseJSON(`10`).(int)
|
||||
utils.AssertEqual(t, 10, num)
|
||||
|
||||
boolVal := ParseJSON(`true`).(bool)
|
||||
utils.AssertEqual(t, true, boolVal)
|
||||
|
||||
float64Val := ParseJSON(`1.00`).(float64)
|
||||
utils.AssertEqual(t, 1.00, float64Val)
|
||||
|
||||
null := ParseJSON(`}}}}`)
|
||||
utils.AssertEqual(t, nil, null)
|
||||
}
|
||||
Reference in New Issue
Block a user