Skip to content

Commit e5c1de4

Browse files
committed
feat(0.8.x): 请求封装
1 parent 61f944d commit e5c1de4

File tree

5 files changed

+378
-5
lines changed

5 files changed

+378
-5
lines changed

common/cmap/cmap.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package cmap
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"github.com/pkg/errors"
8+
"net/url"
9+
"reflect"
10+
"strings"
11+
)
12+
13+
//type Map map[string]interface{}
14+
type CMap map[string][]string
15+
16+
// Get gets the first value associated with the given key.
17+
// If there are no values associated with the key, Get returns
18+
// the empty string. To access multiple values, use the map
19+
// directly.
20+
func (v CMap) Get(key string) string {
21+
if v == nil {
22+
return ""
23+
}
24+
vs := v[key]
25+
if len(vs) == 0 {
26+
return ""
27+
}
28+
return vs[0]
29+
}
30+
31+
// return Get value and Del key
32+
func (v CMap) Pop(key string) string {
33+
s := v.Get(key)
34+
v.Del(key)
35+
return s
36+
}
37+
38+
// Set sets the key to value. It replaces any existing
39+
// values.
40+
func (v CMap) Set(key, value string) CMap {
41+
v[key] = []string{value}
42+
return v
43+
}
44+
45+
// Add adds the value to key. It appends to any existing
46+
// values associated with key.
47+
func (v CMap) Add(key, value string) CMap {
48+
v[key] = append(v[key], value)
49+
return v
50+
}
51+
52+
// Del deletes the values associated with key.
53+
func (v CMap) Del(key string) CMap {
54+
delete(v, key)
55+
return v
56+
}
57+
58+
// Obtain get all values associated with the given key.
59+
func (v CMap) Obtain(key string) []string {
60+
if v == nil {
61+
return []string{}
62+
}
63+
return v[key]
64+
}
65+
66+
// Append set the key to value if it doesn't exists. append if it exists.
67+
func (v CMap) Append(key, value string) CMap {
68+
vs := v.Get(key)
69+
if vs == "" || len(strings.Trim(vs, " ")) == 0 {
70+
v.Set(key, value)
71+
return v
72+
}
73+
return v.Set(key, vs+value)
74+
}
75+
76+
// CMap to struct or map[string]string or map[string]interface{} data
77+
func (v CMap) ToStruct(value interface{}) error {
78+
var m = make(map[string]interface{})
79+
for k, v := range v {
80+
m[k] = v[0]
81+
}
82+
b, err := json.Marshal(m)
83+
if err != nil {
84+
return err
85+
}
86+
t := reflect.TypeOf(value)
87+
if t.Kind() == reflect.Ptr {
88+
t = t.Elem()
89+
if t.Kind() == reflect.Struct || t.Kind() == reflect.Map {
90+
err = json.Unmarshal(b, value)
91+
if err != nil {
92+
return err
93+
}
94+
}
95+
return nil
96+
}
97+
return errors.New("Type mismatch. Must be a pointer to Map or structure")
98+
}
99+
100+
func (v CMap) ToValues() url.Values {
101+
return url.Values(v)
102+
}
103+
104+
func (v CMap) ToMap() map[string]interface{} {
105+
param := make(map[string]interface{})
106+
_ = v.ToStruct(&param)
107+
return param
108+
}
109+
110+
// new a CMap
111+
func New() CMap {
112+
return CMap{}
113+
}
114+
115+
// new a CMap from url.Values
116+
func Values(values url.Values) CMap {
117+
return CMap(values)
118+
}
119+
120+
// new a CMap from struct
121+
func Struct(v interface{}) (values CMap) {
122+
values = New()
123+
iVal := reflect.ValueOf(v)
124+
if iVal.Kind() == reflect.Ptr {
125+
iVal = iVal.Elem()
126+
}
127+
typ := iVal.Type()
128+
for i := 0; i < iVal.NumField(); i++ {
129+
fi := typ.Field(i)
130+
name := fi.Tag.Get("json")
131+
if name == "" {
132+
name = fi.Name
133+
}
134+
// add support slice
135+
if iVal.Field(i).Kind() == reflect.Slice {
136+
var buf bytes.Buffer
137+
buf.WriteString("[")
138+
iValArr := iVal.Field(i)
139+
for j := 0; j < iValArr.Len(); j++ {
140+
buf.WriteString(fmt.Sprint(`"`, iValArr.Index(j), `",`))
141+
}
142+
val := string(buf.Bytes()[:buf.Len()-1])
143+
val += "]"
144+
values.Set(name, val)
145+
continue
146+
}
147+
values.Set(name, fmt.Sprint(iVal.Field(i)))
148+
}
149+
return
150+
}

common/cmap/cmap_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmap
2+
3+
import "testing"
4+
5+
func TestCMap_ToStruct(t *testing.T) {
6+
type Te struct {
7+
Name string `json:"name"`
8+
ID string `json:"id"`
9+
}
10+
var te Te
11+
var param = New()
12+
param.Add("name", "tea")
13+
param.Add("id", "1")
14+
err := param.ToStruct(&te)
15+
if err != nil {
16+
t.Log(err)
17+
return
18+
}
19+
t.Log(te)
20+
21+
t.Log(CMap{}.Add("id", "1").Set("test", "2"))
22+
}

jd.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type Service interface {
8383
// method 请求路由方法
8484
// 业务参数
8585
// 可通过 Request 方法执行其他未封装的京东联盟API
86-
Request(v interface{}, method Method, param map[string]interface{}) error
86+
Request(v interface{}, param IParam) error
8787

8888
// 参数校验
8989
CheckRequiredParameters(v interface{}) error
@@ -274,8 +274,16 @@ func (ser *ServiceImpl) Sign(method Method, param map[string]interface{}) (*Para
274274
return NewParam(parameter), err
275275
}
276276

277-
func (ser *ServiceImpl) Request(v interface{}, method Method, param map[string]interface{}) error {
278-
p, err := ser.Sign(method, param)
277+
func (ser *ServiceImpl) Request(v interface{}, param IParam) error {
278+
var (
279+
method Method
280+
params map[string]interface{}
281+
)
282+
283+
method = Method(param.Method())
284+
params = param.Params().ToMap()
285+
286+
p, err := ser.Sign(method, params)
279287
if err != nil {
280288
log.Error("Sign:", err)
281289
return err
@@ -321,8 +329,9 @@ func (ser *ServiceImpl) CheckRequiredParameters(v interface{}) error {
321329
}
322330

323331
// Deprecated: 使用新接口: Request
324-
func (ser *ServiceImpl) Do(v interface{}, method Method, param map[string]interface{}) error {
325-
return ser.Request(v, method, param)
332+
func (ser *ServiceImpl) Do(v interface{}, method Method, params map[string]interface{}) error {
333+
param := NewTParam(method, params)
334+
return ser.Request(v, param)
326335
}
327336

328337
func (ser *ServiceImpl) GetResult(res Result, err error) ([]byte, error) {

0 commit comments

Comments
 (0)