Compare commits

..

5 Commits

Author SHA1 Message Date
page d879fa53be Merge branch 'master' into compatible-sdk-error 2025-03-10 18:54:06 +08:00
peze ca54bcb809 fix the io writer 2025-03-10 18:41:06 +08:00
peze db0543e442 fix the readSSE 2025-02-25 11:19:43 +08:00
peze 3704038763 compatible with old sdk error 2025-02-17 16:33:57 +08:00
peze abae7f41c3 fix to reader 2025-02-12 20:41:32 +08:00
4 changed files with 19 additions and 83 deletions
+2 -22
View File
@@ -294,28 +294,8 @@ 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{}, 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
func ArrAppend(arr interface{}, value interface{}) {
ArrPush(arr, value)
}
// ArrRemove removes an element from the array
-48
View File
@@ -405,54 +405,6 @@ 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"
+16 -13
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/alibabacloud-go/tea/tea"
"net/http"
"reflect"
"strconv"
@@ -41,38 +42,40 @@ type CastError struct {
Message *string
}
func TeaSDKError(err error) error {
if(err == nil) {
func TeaSDKError(err error) *tea.SDKError {
if err == nil {
return nil
}
if te, ok := err.(*SDKError); ok {
return tea.NewSDKError(map[string]interface{}{
"code": StringValue(te.Code),
"statusCode": IntValue(te.StatusCode),
"message": StringValue(te.Message),
"data": te.Data,
"description": StringValue(te.Description),
"code": StringValue(te.Code),
"statusCode": IntValue(te.StatusCode),
"message": StringValue(te.Message),
"data": te.Data,
"description": StringValue(te.Description),
"accessDeniedDetail": te.AccessDeniedDetail,
})
}
if respErr, ok := err.(ResponseError); ok {
if respErr, ok := err.(ResponseError); ok {
return tea.NewSDKError(map[string]interface{}{
"code": StringValue(respErr.GetCode()),
"code": StringValue(respErr.GetCode()),
"statusCode": IntValue(respErr.GetStatusCode()),
"message": respErr.Error(),
"message": respErr.Error(),
})
}
if baseErr, ok := err.(BaseError); ok {
if baseErr, ok := err.(BaseError); ok {
return tea.NewSDKError(map[string]interface{}{
"code": StringValue(baseErr.GetCode()),
"code": StringValue(baseErr.GetCode()),
"message": baseErr.Error(),
})
}
return err
return tea.NewSDKError(map[string]interface{}{
"message": err.Error(),
})
}
// NewSDKError is used for shortly create SDKError object
+1
View File
@@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"