Compare commits

...

3 Commits

Author SHA1 Message Date
peze fdbb29ef15 fix the toMap when input is basic map 2025-04-07 17:32:23 +08:00
peze a4d8c8f7f5 fix default condition 2025-04-07 17:32:23 +08:00
peze 4820a881e8 add retry options config 2025-04-07 17:32:23 +08:00
2 changed files with 84 additions and 6 deletions
+30 -5
View File
@@ -150,6 +150,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
if runtime["httpClient"] != nil {
runtimeObject.HttpClient = runtime["httpClient"].(HttpClient)
}
if runtime["retryOptions"] != nil {
runtimeObject.RetryOptions = runtime["retryOptions"].(*RetryOptions)
}
return runtimeObject
}
@@ -639,8 +642,23 @@ func isNil(a interface{}) bool {
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{}) {
if IsNil(inputValue) {
if isNilOrZero(inputValue) {
_result = defaultValue
return _result
}
@@ -711,10 +729,17 @@ func ToMap(args ...interface{}) map[string]interface{} {
}
default:
val := reflect.ValueOf(obj)
res := structToMap(val)
for key, value := range res {
if value != nil {
finalArg[key] = value
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
}
}
}
}
+54 -1
View File
@@ -30,6 +30,12 @@ var runtimeObj = map[string]interface{}{
"listener": &Progresstest{},
"tracker": &utils.ReaderTracker{CompletedBytes: int64(10)},
"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-----
@@ -193,6 +199,10 @@ func TestRuntimeObject(t *testing.T) {
runtimeobject = NewRuntimeObject(runtimeObj)
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) {
@@ -351,11 +361,27 @@ 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"])
@@ -985,6 +1011,33 @@ func Test_Default(t *testing.T) {
b := ToInt32(a)
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) {