Compare commits
5 Commits
v1.3.3
...
fix-resp-err
| Author | SHA1 | Date | |
|---|---|---|---|
| e6c4e6c568 | |||
| 9eb5a63923 | |||
| 027c7539de | |||
| 9b850cc440 | |||
| 6192dca562 |
+22
-2
@@ -294,8 +294,28 @@ func ConcatArr(arr1 interface{}, arr2 interface{}) interface{} {
|
||||
}
|
||||
|
||||
// ArrAppend inserts a new pointer at a specified index in a pointer array.
|
||||
func ArrAppend(arr interface{}, value interface{}) {
|
||||
ArrPush(arr, value)
|
||||
func ArrAppend(arr interface{}, value interface{}, index int) {
|
||||
arrV := reflect.ValueOf(arr)
|
||||
if arrV.Kind() != reflect.Ptr || arrV.Elem().Kind() != reflect.Slice {
|
||||
return
|
||||
}
|
||||
|
||||
sliceV := arrV.Elem()
|
||||
|
||||
if index < 0 || index > sliceV.Len() {
|
||||
return
|
||||
}
|
||||
|
||||
valueV := reflect.ValueOf(value)
|
||||
|
||||
// 创建一个容纳新值的切片
|
||||
newSlice := reflect.Append(sliceV, reflect.Zero(sliceV.Type().Elem()))
|
||||
reflect.Copy(newSlice.Slice(index+1, newSlice.Len()), newSlice.Slice(index, newSlice.Len()-1))
|
||||
newSlice.Index(index).Set(valueV)
|
||||
|
||||
// 更新原始切片
|
||||
sliceV.Set(newSlice)
|
||||
return
|
||||
}
|
||||
|
||||
// ArrRemove removes an element from the array
|
||||
|
||||
@@ -405,6 +405,54 @@ func TestConcatArr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrAppend(t *testing.T) {
|
||||
// 测试用例1:插入中间位置
|
||||
t.Run("Append to middle of an array", func(t *testing.T) {
|
||||
numbers := []*int{new(int), new(int), new(int)}
|
||||
for i := range numbers {
|
||||
*numbers[i] = i + 1
|
||||
}
|
||||
|
||||
// 将 9 插入到索引 1
|
||||
valueToInsert := new(int)
|
||||
*valueToInsert = 9
|
||||
|
||||
// 期望的结果
|
||||
expected := []*int{new(int), new(int), new(int), new(int)}
|
||||
*expected[0], *expected[1], *expected[2], *expected[3] = 1, 9, 2, 3
|
||||
|
||||
defer func() {
|
||||
if !reflect.DeepEqual(numbers, expected) {
|
||||
t.Errorf("Expected %+v, but got %+v", expected, numbers)
|
||||
}
|
||||
}()
|
||||
|
||||
ArrAppend(&numbers, valueToInsert, 1)
|
||||
})
|
||||
|
||||
// 测试用例2: 尝试在越界处插入
|
||||
t.Run("Index out of bounds", func(t *testing.T) {
|
||||
numbers := []*int{new(int), new(int), new(int)}
|
||||
for i := range numbers {
|
||||
*numbers[i] = i + 1
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Defer 检查:越界情况下,数组应保持不变
|
||||
expected := []*int{new(int), new(int), new(int)}
|
||||
*expected[0], *expected[1], *expected[2] = 1, 2, 3
|
||||
if !reflect.DeepEqual(numbers, expected) {
|
||||
t.Errorf("Index out of bounds should not modify array, but got %+v", numbers)
|
||||
}
|
||||
}()
|
||||
|
||||
valueToInsert := new(int)
|
||||
*valueToInsert = 9
|
||||
ArrAppend(&numbers, valueToInsert, 10) // 超出范围
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestArrRemove(t *testing.T) {
|
||||
// Create test data for string pointer array
|
||||
str1 := "A"
|
||||
|
||||
+7
-1
@@ -20,6 +20,9 @@ type ResponseError interface {
|
||||
BaseError
|
||||
GetRetryAfter() *int64
|
||||
GetStatusCode() *int
|
||||
GetAccessDeniedDetail() map[string]interface{}
|
||||
GetDescription() *string
|
||||
GetData() map[string]interface{}
|
||||
}
|
||||
|
||||
// SDKError struct is used save error code and message
|
||||
@@ -62,6 +65,9 @@ func TeaSDKError(err error) error {
|
||||
"code": StringValue(respErr.GetCode()),
|
||||
"statusCode": IntValue(respErr.GetStatusCode()),
|
||||
"message": respErr.Error(),
|
||||
"description": StringValue(respErr.GetDescription()),
|
||||
"data": respErr.GetData(),
|
||||
"accessDeniedDetail": respErr.GetAccessDeniedDetail(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -84,7 +90,7 @@ func NewSDKError(obj map[string]interface{}) *SDKError {
|
||||
} else if val, ok := obj["code"].(string); ok {
|
||||
err.Code = String(val)
|
||||
}
|
||||
|
||||
|
||||
if obj["message"] != nil {
|
||||
err.Message = String(obj["message"].(string))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user