refactor: add maps utility for getting keys

This commit is contained in:
Fu Diwei 2026-07-17 16:56:00 +08:00
parent 62109e5d91
commit afab301d8e
6 changed files with 30 additions and 20 deletions

View File

@ -12,6 +12,8 @@ import (
"sort"
"strings"
"time"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
type signer struct {
@ -47,10 +49,7 @@ func (s *signer) Sign(req *http.Request) error {
if len(req.URL.Query()) > 0 {
query := req.URL.Query()
keys := make([]string, 0, len(query))
for key := range query {
keys = append(keys, key)
}
keys := xmaps.Keys(query)
sort.Strings(keys)
for i, key := range keys {

View File

@ -19,6 +19,7 @@ import (
"github.com/go-resty/resty/v2"
"github.com/certimate-go/certimate/internal/app"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
type Client struct {
@ -354,10 +355,7 @@ func (c *Client) ensureApiUserToken(ctx context.Context) error {
}
func generateSignature(params map[string]any, secret string) string {
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
keys := xmaps.Keys(params)
sort.Strings(keys)
canonicalStr := ""

View File

@ -11,6 +11,8 @@ import (
"sort"
"strings"
"time"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
type signer struct {
@ -50,10 +52,7 @@ func (s *signer) Sign(req *http.Request) error {
if len(req.URL.Query()) > 0 {
query := req.URL.Query()
keys := make([]string, 0, len(query))
for key := range query {
keys = append(keys, key)
}
keys := xmaps.Keys(query)
sort.Strings(keys)
for i, key := range keys {

View File

@ -12,6 +12,8 @@ import (
"sort"
"strings"
"time"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
type signer struct {
@ -61,10 +63,7 @@ func (s *signer) Sign(req *http.Request) error {
params["Timestamp"] = timestamp
params["SignatureVersion"] = "1.0"
params["SignatureMethod"] = "HMAC-SHA256"
paramsKeys := make([]string, 0, len(params))
for k := range params {
paramsKeys = append(paramsKeys, k)
}
paramsKeys := xmaps.Keys(params)
sort.Strings(paramsKeys)
stringToSign := ""

View File

@ -12,6 +12,8 @@ import (
"sort"
"strings"
"time"
xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)
type signer struct {
@ -41,10 +43,7 @@ func (s *signer) Sign(req *http.Request) error {
if len(req.URL.Query()) > 0 {
query := req.URL.Query()
keys := make([]string, 0, len(query))
for key := range query {
keys = append(keys, key)
}
keys := xmaps.Keys(query)
sort.Strings(keys)
for i, key := range keys {

16
pkg/utils/maps/key.go Normal file
View File

@ -0,0 +1,16 @@
package maps
// 获取字典中的所有键组成的切片。
//
// 入参:
// - dict: 字典。
//
// 出参:
// - keys: 字典中的所有键组成的切片。
func Keys[Map ~map[K]V, K comparable, V any](dict Map) []K {
keys := make([]K, 0, len(dict))
for k := range dict {
keys = append(keys, k)
}
return keys
}