Compare commits

...

2 Commits

Author SHA1 Message Date
peze c2e107f1af fix default condition 2025-04-01 19:14:52 +08:00
peze 5ff9708f43 add retry options config 2025-03-31 16:36:54 +08:00
2 changed files with 56 additions and 1 deletions
+19 -1
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
}
+37
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) {
@@ -985,6 +995,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) {