mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
Improve developer experience (#681)
* feat: 添加query 以及query单元测试 feat: 添加模板字符串解析以及模板字符串单元测试 * improve: 序列化请求参数,使得参数更易读 * delete: delete useless module * format: format code * docs: add function comment * docs: comment method * fix: fixed type convert error * feat: support any type * feat: support other type * format: format code * test: check logger * format: format code * test: udpate testing case * del: remove useless code * del: remove useless module * test: update testing case * ✨ feat: support for unsigned integers * ✨ feat: template string support any type
This commit is contained in:
24
util/query.go
Normal file
24
util/query.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Query 将Map序列化为Query参数
|
||||
func Query(params map[string]interface{}) string {
|
||||
finalString := make([]string, 0)
|
||||
for key, value := range params {
|
||||
valueString := ""
|
||||
switch v := value.(type) {
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
valueString = fmt.Sprintf("%d", v)
|
||||
case bool:
|
||||
valueString = fmt.Sprintf("%v", v)
|
||||
default:
|
||||
valueString = fmt.Sprintf("%s", v)
|
||||
}
|
||||
finalString = append(finalString, strings.Join([]string{key, valueString}, "="))
|
||||
}
|
||||
return strings.Join(finalString, "&")
|
||||
}
|
||||
19
util/query_test.go
Normal file
19
util/query_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestQuery query method test case
|
||||
func TestQuery(t *testing.T) {
|
||||
result := Query(map[string]interface{}{
|
||||
"age": 12,
|
||||
"name": "Alan",
|
||||
"cat": "Peter",
|
||||
})
|
||||
if result == "" {
|
||||
// 由于hash是乱序 所以没法很好的预测输出的字符串
|
||||
// 将会输出符合Query规则的字符串 "age=12&name=Alan&cat=Peter"
|
||||
t.Error("NOT PASS")
|
||||
}
|
||||
}
|
||||
24
util/template.go
Normal file
24
util/template.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Template 对字符串中的和map的key相同的字符串进行模板替换 仅支持 形如: {name}
|
||||
func Template(source string, data map[string]interface{}) string {
|
||||
sourceCopy := &source
|
||||
for k, val := range data {
|
||||
valStr := ""
|
||||
switch v := val.(type) {
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
valStr = fmt.Sprintf("%d", v)
|
||||
case bool:
|
||||
valStr = fmt.Sprintf("%v", v)
|
||||
default:
|
||||
valStr = fmt.Sprintf("%s", v)
|
||||
}
|
||||
*sourceCopy = strings.Replace(*sourceCopy, strings.Join([]string{"{", k, "}"}, ""), valStr, 1)
|
||||
}
|
||||
return *sourceCopy
|
||||
}
|
||||
20
util/template_test.go
Normal file
20
util/template_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestTemplate testing case about Template method
|
||||
func TestTemplate(t *testing.T) {
|
||||
result := Template("{name}={age};{with}={another};any={any};boolean={boolean}", map[string]interface{}{
|
||||
"name": "Helan",
|
||||
"age": "33",
|
||||
"with": "Pep",
|
||||
"another": "C",
|
||||
"any": 33,
|
||||
"boolean": false,
|
||||
})
|
||||
if result != "Helan=33;Pep=C;any=33;boolean=false" {
|
||||
t.Error("NOT PSS testing")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user