Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d879fa53be | |||
| ca54bcb809 | |||
| db0543e442 | |||
| 3704038763 | |||
| abae7f41c3 |
+28
-2
@@ -24,8 +24,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alibabacloud-go/debug/debug"
|
"github.com/alibabacloud-go/debug/debug"
|
||||||
"github.com/alibabacloud-go/tea/utils"
|
|
||||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||||
|
"github.com/alibabacloud-go/tea/utils"
|
||||||
|
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
@@ -202,7 +202,7 @@ func getDaraClient(tag string) *daraClient {
|
|||||||
|
|
||||||
// DoRequest is used send request to server
|
// DoRequest is used send request to server
|
||||||
func DoRequest(request *Request, runtimeObject *RuntimeObject) (response *Response, err error) {
|
func DoRequest(request *Request, runtimeObject *RuntimeObject) (response *Response, err error) {
|
||||||
if(runtimeObject == nil) {
|
if runtimeObject == nil {
|
||||||
runtimeObject = &RuntimeObject{}
|
runtimeObject = &RuntimeObject{}
|
||||||
}
|
}
|
||||||
fieldMap := make(map[string]string)
|
fieldMap := make(map[string]string)
|
||||||
@@ -429,6 +429,32 @@ func ToReader(obj interface{}) io.Reader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToWriter(obj interface{}) io.Writer {
|
||||||
|
switch obj.(type) {
|
||||||
|
case string:
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.WriteString(obj.(string))
|
||||||
|
return &buf
|
||||||
|
case *string:
|
||||||
|
var buf bytes.Buffer
|
||||||
|
tmp := obj.(*string)
|
||||||
|
buf.WriteString(*tmp)
|
||||||
|
return &buf
|
||||||
|
case []byte:
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.Write(obj.([]byte))
|
||||||
|
return &buf
|
||||||
|
case io.Writer:
|
||||||
|
return obj.(io.Writer)
|
||||||
|
case *bytes.Buffer:
|
||||||
|
return obj.(*bytes.Buffer)
|
||||||
|
case *os.File:
|
||||||
|
return obj.(*os.File)
|
||||||
|
default:
|
||||||
|
panic("Invalid Writer. Please provide a valid Writer.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func ToString(val interface{}) string {
|
func ToString(val interface{}) string {
|
||||||
switch v := val.(type) {
|
switch v := val.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
|
|||||||
@@ -735,6 +735,42 @@ func Test_ToReader(t *testing.T) {
|
|||||||
utils.AssertEqual(t, "", string(byt))
|
utils.AssertEqual(t, "", string(byt))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_ToWriter(t *testing.T) {
|
||||||
|
str := "abc"
|
||||||
|
writer := ToWriter(str).(*bytes.Buffer)
|
||||||
|
utils.AssertEqual(t, "abc", writer.String())
|
||||||
|
|
||||||
|
strPtr := new(string)
|
||||||
|
*strPtr = "def"
|
||||||
|
writer = ToWriter(strPtr).(*bytes.Buffer)
|
||||||
|
utils.AssertEqual(t, "def", writer.String())
|
||||||
|
|
||||||
|
bytesData := []byte("ghi")
|
||||||
|
writer = ToWriter(bytesData).(*bytes.Buffer)
|
||||||
|
utils.AssertEqual(t, "ghi", writer.String())
|
||||||
|
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
writer = ToWriter(buffer).(*bytes.Buffer)
|
||||||
|
utils.AssertEqual(t, buffer, writer)
|
||||||
|
|
||||||
|
fileWriter := ToWriter(os.Stdout)
|
||||||
|
utils.AssertEqual(t, os.Stdout, fileWriter)
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
writer2 := ToWriter(&buf)
|
||||||
|
writer2.Write([]byte("test"))
|
||||||
|
utils.AssertEqual(t, "test", buf.String())
|
||||||
|
|
||||||
|
// Test a non-writer to trigger panic
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
utils.AssertEqual(t, "Invalid Writer. Please provide a valid Writer.", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
num := 10
|
||||||
|
ToWriter(num) // This should cause a panic
|
||||||
|
}
|
||||||
|
|
||||||
func Test_ToString(t *testing.T) {
|
func Test_ToString(t *testing.T) {
|
||||||
str := ToString(10)
|
str := ToString(10)
|
||||||
utils.AssertEqual(t, "10", str)
|
utils.AssertEqual(t, "10", str)
|
||||||
|
|||||||
+12
-11
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -42,32 +43,32 @@ type CastError struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TeaSDKError(err error) *tea.SDKError {
|
func TeaSDKError(err error) *tea.SDKError {
|
||||||
if(err == nil) {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if te, ok := err.(*SDKError); ok {
|
if te, ok := err.(*SDKError); ok {
|
||||||
return tea.NewSDKError(map[string]interface{}{
|
return tea.NewSDKError(map[string]interface{}{
|
||||||
"code": StringValue(te.Code),
|
"code": StringValue(te.Code),
|
||||||
"statusCode": IntValue(te.StatusCode),
|
"statusCode": IntValue(te.StatusCode),
|
||||||
"message": StringValue(te.Message),
|
"message": StringValue(te.Message),
|
||||||
"data": te.Data,
|
"data": te.Data,
|
||||||
"description": StringValue(te.Description),
|
"description": StringValue(te.Description),
|
||||||
"accessDeniedDetail": te.AccessDeniedDetail,
|
"accessDeniedDetail": te.AccessDeniedDetail,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if respErr, ok := err.(ResponseError); ok {
|
if respErr, ok := err.(ResponseError); ok {
|
||||||
return tea.NewSDKError(map[string]interface{}{
|
return tea.NewSDKError(map[string]interface{}{
|
||||||
"code": StringValue(respErr.GetCode()),
|
"code": StringValue(respErr.GetCode()),
|
||||||
"statusCode": IntValue(respErr.GetStatusCode()),
|
"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{}{
|
return tea.NewSDKError(map[string]interface{}{
|
||||||
"code": StringValue(baseErr.GetCode()),
|
"code": StringValue(baseErr.GetCode()),
|
||||||
"message": baseErr.Error(),
|
"message": baseErr.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|||||||
Reference in New Issue
Block a user