Skip to content

Commit

Permalink
support pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao committed Apr 21, 2020
1 parent ea9b8a8 commit 214013b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 91 deletions.
3 changes: 3 additions & 0 deletions ChangLog.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
2020-04-21 Version: v1.0.0
- Use pointer params as input params.

2020-04-15 Version: v0.0.1
- Init project.
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module github.com/alibabacloud-go/tea-oss-utils

require (
github.com/alibabacloud-go/tea v0.0.12
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
github.com/alibabacloud-go/tea v1.0.0
github.com/yuin/goldmark v1.1.30 // indirect
golang.org/x/crypto v0.0.0-20200420201142-3c4aac89819a // indirect
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a // indirect
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
golang.org/x/tools v0.0.0-20200421042724-cfa8b22178d2 // indirect
)
10 changes: 5 additions & 5 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func getSignedStrV1(req *tea.Request, canonicalizedResource, accessKeySecret str
contentType := req.Headers["content-type"]
contentMd5 := req.Headers["content-md5"]

signStr := req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource
signStr := tea.StringValue(req.Method) + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + canonicalizedResource
h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(accessKeySecret))
io.WriteString(h, signStr)
signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
Expand Down Expand Up @@ -460,7 +460,7 @@ func getSignedStrV2(req *tea.Request, canonicalizedResource, accessKeySecret str
date := req.Headers["date"]
contentType := req.Headers["content-type"]
contentMd5 := req.Headers["content-md5"]
signStr := req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + strings.Join(additionalHeaders, ";") + "\n" + canonicalizedResource
signStr := tea.StringValue(req.Method) + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + strings.Join(additionalHeaders, ";") + "\n" + canonicalizedResource
h := hmac.New(func() hash.Hash { return sha256.New() }, []byte(accessKeySecret))
io.WriteString(h, signStr)
signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
Expand Down Expand Up @@ -514,7 +514,7 @@ func getSignatureV1(request *tea.Request, bucketName, accessKeySecret string) st
if bucketName != "" {
resource = "/" + bucketName
}
resource = resource + request.Pathname
resource = resource + tea.StringValue(request.Pathname)
if !strings.Contains(resource, "?") && len(request.Query) > 0 {
resource += "?"
}
Expand All @@ -534,9 +534,9 @@ func getSignatureV1(request *tea.Request, bucketName, accessKeySecret string) st

func getSignatureV2(request *tea.Request, bucketName, accessKeySecret string, additionalHeaders []string) string {
resource := ""
pathName := request.Pathname
pathName := tea.StringValue(request.Pathname)
if bucketName != "" {
pathName = "/" + bucketName + request.Pathname
pathName = "/" + bucketName + tea.StringValue(request.Pathname)
}

strs := strings.Split(pathName, "?")
Expand Down
98 changes: 49 additions & 49 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ type ServiceError struct {
HostId string `json:"HostId" xml:"HostId"`
}

func GetSignature(request *tea.Request, bucketName, accessKeyId, accessKeySecret, signatureVersion string, addtionalHeaders []string) string {
func GetSignature(request *tea.Request, bucketName, accessKeyId, accessKeySecret, signatureVersion *string, addtionalHeaders []*string) *string {
sign := ""
if strings.ToUpper(signatureVersion) == "V2" {
if strings.ToUpper(tea.StringValue(signatureVersion)) == "V2" {
if len(addtionalHeaders) == 0 {
sign = "OSS2 AccessKeyId:" + accessKeyId + ",Signature:" +
getSignatureV2(request, bucketName, accessKeySecret, addtionalHeaders)
return sign
sign = "OSS2 AccessKeyId:" + tea.StringValue(accessKeyId) + ",Signature:" +
getSignatureV2(request, tea.StringValue(bucketName), tea.StringValue(accessKeySecret), tea.StringSliceValue(addtionalHeaders))
return tea.String(sign)
} else {
sign = "OSS2 AccessKeyId:" + accessKeyId + ",AdditionalHeaders:" + listToString(addtionalHeaders, ";") +
",Signature:" + getSignatureV2(request, bucketName, accessKeySecret, addtionalHeaders)
return sign
sign = "OSS2 AccessKeyId:" + tea.StringValue(accessKeyId) + ",AdditionalHeaders:" + listToString(tea.StringSliceValue(addtionalHeaders), ";") +
",Signature:" + getSignatureV2(request, tea.StringValue(bucketName), tea.StringValue(accessKeySecret), tea.StringSliceValue(addtionalHeaders))
return tea.String(sign)
}

} else {
sign = "OSS " + accessKeyId + ":" + getSignatureV1(request, bucketName, accessKeySecret)
return sign
sign = "OSS " + tea.StringValue(accessKeyId) + ":" + getSignatureV1(request, tea.StringValue(bucketName), tea.StringValue(accessKeySecret))
return tea.String(sign)
}
}

Expand All @@ -162,33 +162,33 @@ func GetSignature(request *tea.Request, bucketName, accessKeyId, accessKeySecret
// }

// Add prefix to key of meta
func ToMeta(meta map[string]string, prefix string) map[string]string {
func ToMeta(meta map[string]string, prefix *string) map[string]string {
result := make(map[string]string)
for key, value := range meta {
if !strings.HasPrefix(strings.ToLower((key)), prefix) {
key = prefix + key
if !strings.HasPrefix(strings.ToLower((key)), tea.StringValue(prefix)) {
key = tea.StringValue(prefix) + key
}
result[key] = value
}
return result
}

// Remove prefix from key of meta
func ParseMeta(meta map[string]string, prefix string) map[string]string {
func ParseMeta(meta map[string]string, prefix *string) map[string]string {
userMeta := make(map[string]string)
for key, value := range meta {
if strings.HasPrefix(strings.ToLower(key), prefix) {
key = strings.Replace(key, prefix, "", 1)
if strings.HasPrefix(strings.ToLower(key), tea.StringValue(prefix)) {
key = strings.Replace(key, tea.StringValue(prefix), "", 1)
}
userMeta[key] = value
}
return userMeta
}

func GetErrMessage(bodyStr string) map[string]interface{} {
func GetErrMessage(bodyStr *string) map[string]interface{} {
resp := make(map[string]interface{})
errMsg := &ServiceError{}
err := xml.Unmarshal([]byte(bodyStr), errMsg)
err := xml.Unmarshal([]byte(tea.StringValue(bodyStr)), errMsg)
if err != nil {
return resp
}
Expand All @@ -200,42 +200,42 @@ func GetErrMessage(bodyStr string) map[string]interface{} {
}

// Return md5 according to body
func GetContentMD5(a string, isEnableMD5 bool) string {
if !isEnableMD5 {
return ""
func GetContentMD5(a *string, isEnableMD5 *bool) *string {
if !tea.BoolValue(isEnableMD5) {
return tea.String("")
}

sum := md5.Sum([]byte(a))
sum := md5.Sum([]byte(tea.StringValue(a)))
b64 := base64.StdEncoding.EncodeToString(sum[:])
return b64
return tea.String(b64)
}

// Return content-type according to object name
func GetContentType(name string) string {
return typeByExtension(name)
func GetContentType(name *string) *string {
return tea.String(typeByExtension(tea.StringValue(name)))
}

// Encryption
func Encode(val string, encodeType string) string {
strs := strings.Split(val, "/")
if encodeType == "Base64" {
func Encode(val *string, encodeType *string) *string {
strs := strings.Split(tea.StringValue(val), "/")
if tea.StringValue(encodeType) == "Base64" {
encodeStr := base64.StdEncoding.EncodeToString([]byte(strs[len(strs)-1]))
strs[len(strs)-1] = encodeStr
} else if encodeType == "UrlEncode" {
} else if tea.StringValue(encodeType) == "UrlEncode" {
encodeStr, err := url.QueryUnescape(strs[len(strs)-1])
if err != nil {
return ""
return tea.String("")
}
strs[len(strs)-1] = encodeStr
}
return strings.Join(strs, "/")
return tea.String(strings.Join(strs, "/"))
}

func Decode(value string, decodeType string) string {
if decodeType == "Base64Decode" {
return base64Decode(value)
} else if decodeType == "UrlDecode" {
return urlDecode(value)
func Decode(value *string, decodeType *string) *string {
if tea.StringValue(decodeType) == "Base64Decode" {
return tea.String(base64Decode(tea.StringValue(value)))
} else if tea.StringValue(decodeType) == "UrlDecode" {
return tea.String(urlDecode(tea.StringValue(value)))
}
return value
}
Expand All @@ -245,24 +245,24 @@ func Inject(body io.Reader, ref map[string]string) io.Reader {
return body
}

func GetHost(bucketName, regionId, endpoint, hostModel string) string {
func GetHost(bucketName, regionId, endpoint, hostModel *string) *string {
host := ""
if regionId == "" {
regionId = "cn-hangzhou"
if tea.StringValue(regionId) == "" {
regionId = tea.String("cn-hangzhou")
}
if endpoint == "" {
endpoint = "oss-" + regionId + ".aliyuncs.com"
if tea.StringValue(endpoint) == "" {
endpoint = tea.String("oss-" + tea.StringValue(regionId) + ".aliyuncs.com")
}
if bucketName != "" {
if strings.ToLower(hostModel) == "ip" {
host = endpoint + "/" + bucketName
} else if strings.ToLower(hostModel) == "cname" {
host = endpoint
if tea.StringValue(bucketName) != "" {
if strings.ToLower(tea.StringValue(hostModel)) == "ip" {
host = tea.StringValue(endpoint) + "/" + tea.StringValue(bucketName)
} else if strings.ToLower(tea.StringValue(hostModel)) == "cname" {
host = tea.StringValue(endpoint)
} else {
host = bucketName + "." + endpoint
host = tea.StringValue(bucketName) + "." + tea.StringValue(endpoint)
}
} else {
host = endpoint
host = tea.StringValue(endpoint)
}
return host
return tea.String(host)
}
70 changes: 35 additions & 35 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_ToMeta(t *testing.T) {
meta := map[string]string{
"a": "ok",
}
result := ToMeta(meta, "x-oss-")
result := ToMeta(meta, tea.String("x-oss-"))
utils.AssertEqual(t, "ok", result["x-oss-a"])
}

Expand All @@ -63,7 +63,7 @@ func Test_ParseMeta(t *testing.T) {
raw := map[string]string{
"x-oss-key": "oss",
}
res := ParseMeta(raw, "x-oss-")
res := ParseMeta(raw, tea.String("x-oss-"))
utils.AssertEqual(t, "oss", res["key"])
}

Expand Down Expand Up @@ -130,42 +130,42 @@ func Test_ParseMeta(t *testing.T) {
// }

func Test_GetErrmessage(t *testing.T) {
result := GetErrMessage("")
result := GetErrMessage(tea.String(""))
utils.AssertEqual(t, len(result), 0)

str := `<?xml version="1.0" encoding="utf-8" standalone="no"?>
<num>10</num>`
result = GetErrMessage(str)
result = GetErrMessage(tea.String(str))
utils.AssertEqual(t, result["Code"].(string), "")

}

func Test_GetContentType(t *testing.T) {
typ := GetContentType("a.txt")
utils.AssertEqual(t, "text/plain; charset=utf-8", typ)
typ := GetContentType(tea.String("a.txt"))
utils.AssertEqual(t, "text/plain; charset=utf-8", tea.StringValue(typ))
}

func Test_Encode(t *testing.T) {
res := Encode("/oss", "Base64")
utils.AssertEqual(t, "/b3Nz", res)
res := Encode(tea.String("/oss"), tea.String("Base64"))
utils.AssertEqual(t, "/b3Nz", tea.StringValue(res))

res = Encode("/oss", "UrlEncode")
utils.AssertEqual(t, "/oss", res)
res = Encode(tea.String("/oss"), tea.String("UrlEncode"))
utils.AssertEqual(t, "/oss", tea.StringValue(res))
}

func Test_Decode(t *testing.T) {
dec := Decode("a/afgg?';;", "Base64Decode")
utils.AssertEqual(t, "", dec)
dec = Decode("afff", "Base64Decode")
utils.AssertEqual(t, 3, len(dec))

dec = Decode("a/afg%", "UrlDecode")
utils.AssertEqual(t, "", dec)
dec = Decode("afff", "UrlDecode")
utils.AssertEqual(t, 4, len(dec))

dec = Decode("afff", "")
utils.AssertEqual(t, "afff", dec)
dec := Decode(tea.String("a/afgg?';;"), tea.String("Base64Decode"))
utils.AssertEqual(t, "", tea.StringValue(dec))
dec = Decode(tea.String("afff"), tea.String("Base64Decode"))
utils.AssertEqual(t, 3, len(tea.StringValue(dec)))

dec = Decode(tea.String("a/afg%"), tea.String("UrlDecode"))
utils.AssertEqual(t, "", tea.StringValue(dec))
dec = Decode(tea.String("afff"), tea.String("UrlDecode"))
utils.AssertEqual(t, 4, len(tea.StringValue(dec)))

dec = Decode(tea.String("afff"), tea.String(""))
utils.AssertEqual(t, "afff", tea.StringValue(dec))
}

func Test_GetRefer(t *testing.T) {
Expand All @@ -187,11 +187,11 @@ func Test_GetRefer(t *testing.T) {
}

func Test_GetContentMD5(t *testing.T) {
md5 := GetContentMD5("oss", true)
utils.AssertEqual(t, "NpZEESCkAveTpwR2ZUDmng==", md5)
md5 := GetContentMD5(tea.String("oss"), tea.Bool(true))
utils.AssertEqual(t, "NpZEESCkAveTpwR2ZUDmng==", tea.StringValue(md5))

md5 = GetContentMD5("oss", false)
utils.AssertEqual(t, "", md5)
md5 = GetContentMD5(tea.String("oss"), tea.Bool(false))
utils.AssertEqual(t, "", tea.StringValue(md5))
}

func Test_GetSignature(t *testing.T) {
Expand All @@ -204,17 +204,17 @@ func Test_GetSignature(t *testing.T) {
"x-oss-meta": "user",
}

sign := GetSignature(request, "script", "accessKeyId", "accessKeySecret", "V1", []string{"x-oss-meta"})
utils.AssertEqual(t, "OSS accessKeyId:S7JeWK7VQxpJ+LyWrfYz0o69Nm4=", sign)
sign := GetSignature(request, tea.String("script"), tea.String("accessKeyId"), tea.String("accessKeySecret"), tea.String("V1"), []*string{tea.String("x-oss-meta")})
utils.AssertEqual(t, "OSS accessKeyId:S7JeWK7VQxpJ+LyWrfYz0o69Nm4=", tea.StringValue(sign))

sign = GetSignature(request, "script", "accessKeyId", "accessKeySecret", "V2", nil)
utils.AssertEqual(t, "OSS2 AccessKeyId:accessKeyId,Signature:5fFDYYbdsAFGXOH0p53pT5dg1ox0Z/qwmb08Zr37cT0=", sign)
sign = GetSignature(request, tea.String("script"), tea.String("accessKeyId"), tea.String("accessKeySecret"), tea.String("V2"), nil)
utils.AssertEqual(t, "OSS2 AccessKeyId:accessKeyId,Signature:5fFDYYbdsAFGXOH0p53pT5dg1ox0Z/qwmb08Zr37cT0=", tea.StringValue(sign))

request.Pathname = "test?sdk"
request.Pathname = tea.String("test?sdk")

sign = GetSignature(request, "script", "accessKeyId", "accessKeySecret", "V1", []string{"x-oss-meta"})
utils.AssertEqual(t, "OSS accessKeyId:ZL1VdoByVOT5pajpF+bV29JEOGM=", sign)
sign = GetSignature(request, tea.String("script"), tea.String("accessKeyId"), tea.String("accessKeySecret"), tea.String("V1"), []*string{tea.String("x-oss-meta")})
utils.AssertEqual(t, "OSS accessKeyId:ZL1VdoByVOT5pajpF+bV29JEOGM=", tea.StringValue(sign))

sign = GetSignature(request, "script", "accessKeyId", "accessKeySecret", "V2", []string{"x-oss-meta"})
utils.AssertEqual(t, "OSS2 AccessKeyId:accessKeyId,AdditionalHeaders:x-oss-meta,Signature:3bTli+EG0i9H93UbtZRdvHd0TB4I2/T9u+JzCT/EOJ4=", sign)
sign = GetSignature(request, tea.String("script"), tea.String("accessKeyId"), tea.String("accessKeySecret"), tea.String("V2"), []*string{tea.String("x-oss-meta")})
utils.AssertEqual(t, "OSS2 AccessKeyId:accessKeyId,AdditionalHeaders:x-oss-meta,Signature:3bTli+EG0i9H93UbtZRdvHd0TB4I2/T9u+JzCT/EOJ4=", tea.StringValue(sign))
}

0 comments on commit 214013b

Please sign in to comment.