Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b1c1d1d20c | |||
| fdbb29ef15 | |||
| a4d8c8f7f5 | |||
| 4820a881e8 |
+30
-5
@@ -150,6 +150,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
|
|||||||
if runtime["httpClient"] != nil {
|
if runtime["httpClient"] != nil {
|
||||||
runtimeObject.HttpClient = runtime["httpClient"].(HttpClient)
|
runtimeObject.HttpClient = runtime["httpClient"].(HttpClient)
|
||||||
}
|
}
|
||||||
|
if runtime["retryOptions"] != nil {
|
||||||
|
runtimeObject.RetryOptions = runtime["retryOptions"].(*RetryOptions)
|
||||||
|
}
|
||||||
return runtimeObject
|
return runtimeObject
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -639,8 +642,23 @@ func isNil(a interface{}) bool {
|
|||||||
return vi.IsNil()
|
return vi.IsNil()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isNilOrZero(value interface{}) bool {
|
||||||
|
if value == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
v := reflect.ValueOf(value)
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
|
||||||
|
return v.IsNil()
|
||||||
|
default:
|
||||||
|
// Check for zero value
|
||||||
|
return reflect.DeepEqual(value, reflect.Zero(v.Type()).Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Default(inputValue interface{}, defaultValue interface{}) (_result interface{}) {
|
func Default(inputValue interface{}, defaultValue interface{}) (_result interface{}) {
|
||||||
if IsNil(inputValue) {
|
if isNilOrZero(inputValue) {
|
||||||
_result = defaultValue
|
_result = defaultValue
|
||||||
return _result
|
return _result
|
||||||
}
|
}
|
||||||
@@ -711,10 +729,17 @@ func ToMap(args ...interface{}) map[string]interface{} {
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
val := reflect.ValueOf(obj)
|
val := reflect.ValueOf(obj)
|
||||||
res := structToMap(val)
|
if val.Kind().String() == "map" {
|
||||||
for key, value := range res {
|
tmp := val.MapKeys()
|
||||||
if value != nil {
|
for _, key := range tmp {
|
||||||
finalArg[key] = value
|
finalArg[key.String()] = val.MapIndex(key).Interface()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res := structToMap(val)
|
||||||
|
for key, value := range res {
|
||||||
|
if value != nil {
|
||||||
|
finalArg[key] = value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-1
@@ -30,6 +30,12 @@ var runtimeObj = map[string]interface{}{
|
|||||||
"listener": &Progresstest{},
|
"listener": &Progresstest{},
|
||||||
"tracker": &utils.ReaderTracker{CompletedBytes: int64(10)},
|
"tracker": &utils.ReaderTracker{CompletedBytes: int64(10)},
|
||||||
"logger": utils.NewLogger("info", "", &bytes.Buffer{}, "{time}"),
|
"logger": utils.NewLogger("info", "", &bytes.Buffer{}, "{time}"),
|
||||||
|
"retryOptions": &RetryOptions{
|
||||||
|
Retryable: true,
|
||||||
|
RetryCondition: []*RetryCondition{
|
||||||
|
{MaxAttempts: 3, Exception: []string{"AErr"}, ErrorCode: []string{"A1Err"}},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var key = `-----BEGIN RSA PRIVATE KEY-----
|
var key = `-----BEGIN RSA PRIVATE KEY-----
|
||||||
@@ -193,6 +199,10 @@ func TestRuntimeObject(t *testing.T) {
|
|||||||
|
|
||||||
runtimeobject = NewRuntimeObject(runtimeObj)
|
runtimeobject = NewRuntimeObject(runtimeObj)
|
||||||
utils.AssertEqual(t, false, BoolValue(runtimeobject.IgnoreSSL))
|
utils.AssertEqual(t, false, BoolValue(runtimeobject.IgnoreSSL))
|
||||||
|
|
||||||
|
utils.AssertEqual(t, true, runtimeobject.RetryOptions.Retryable)
|
||||||
|
|
||||||
|
utils.AssertEqual(t, 1, len(runtimeobject.RetryOptions.RetryCondition))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSDKError(t *testing.T) {
|
func TestSDKError(t *testing.T) {
|
||||||
@@ -351,11 +361,27 @@ type Test struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestToMap(t *testing.T) {
|
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{
|
in := map[string]*string{
|
||||||
"tea": String("test"),
|
"tea": String("test"),
|
||||||
"nil": nil,
|
"nil": nil,
|
||||||
}
|
}
|
||||||
result := ToMap(in)
|
result = ToMap(in)
|
||||||
utils.AssertEqual(t, "test", result["tea"])
|
utils.AssertEqual(t, "test", result["tea"])
|
||||||
utils.AssertNil(t, result["nil"])
|
utils.AssertNil(t, result["nil"])
|
||||||
|
|
||||||
@@ -985,6 +1011,33 @@ func Test_Default(t *testing.T) {
|
|||||||
|
|
||||||
b := ToInt32(a)
|
b := ToInt32(a)
|
||||||
utils.AssertEqual(t, Int32Value(b), int32(10))
|
utils.AssertEqual(t, Int32Value(b), int32(10))
|
||||||
|
|
||||||
|
// Testing Default with nil values
|
||||||
|
if result := Default(nil, "default"); result != "default" {
|
||||||
|
t.Errorf("expected 'default', got '%v'", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Testing Default with zero values
|
||||||
|
if result := Default("", "default"); result != "default" {
|
||||||
|
t.Errorf("expected 'default', got '%v'", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := Default(0, 42); result != 42 {
|
||||||
|
t.Errorf("expected 42, got %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := Default(false, true); result != true {
|
||||||
|
t.Errorf("expected true, got %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Testing Default with non-zero values
|
||||||
|
if result := Default("value", "default"); result != "value" {
|
||||||
|
t.Errorf("expected 'value', got '%v'", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := Default([]int{1, 2, 3}, []int{}); !reflect.DeepEqual(result, []int{1, 2, 3}) {
|
||||||
|
t.Errorf("expected [1 2 3], got '%v'", result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToBytes(t *testing.T) {
|
func TestToBytes(t *testing.T) {
|
||||||
|
|||||||
+23
-3
@@ -6,6 +6,7 @@ import (
|
|||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/modern-go/reflect2"
|
"github.com/modern-go/reflect2"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -332,9 +333,28 @@ func (decoder *nullableFuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jso
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stringify(m interface{}) string {
|
func Stringify(a interface{}) string {
|
||||||
byt, _ := json.Marshal(m)
|
switch v := a.(type) {
|
||||||
return string(byt)
|
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 ParseJSON(a string) interface{} {
|
func ParseJSON(a string) interface{} {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package dara
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alibabacloud-go/tea/utils"
|
"github.com/alibabacloud-go/tea/utils"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@@ -878,3 +879,42 @@ func TestUnmarshalWithDefaultDecoders(t *testing.T) {
|
|||||||
err = jsoniter.Unmarshal(from, toUint64)
|
err = jsoniter.Unmarshal(from, toUint64)
|
||||||
utils.AssertNotNil(t, err)
|
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