-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopt.go
More file actions
50 lines (45 loc) · 999 Bytes
/
Copy pathopt.go
File metadata and controls
50 lines (45 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package xmux
import (
"fmt"
"reflect"
"strings"
)
// 自动生成接口文档
type option struct {
Name string
Typ string
Need string
Default string
Information string
}
func postOpt(s interface{}) []option {
opts := make([]option, 0)
tpy := reflect.TypeOf(s)
if tpy.Kind() == reflect.Ptr {
tpy = tpy.Elem()
}
if tpy.Kind() != reflect.Struct {
return nil
}
for i := 0; i < tpy.NumField(); i++ {
opt := option{}
opt.Default = tpy.Field(i).Tag.Get("default")
opt.Name = strings.Split(tpy.Field(i).Tag.Get("json"), ",")[0]
opt.Need = tpy.Field(i).Tag.Get("need")
opt.Typ = tpy.Field(i).Tag.Get("type")
opt.Information = tpy.Field(i).Tag.Get("information")
opts = append(opts, opt)
}
return opts
}
func getOpt(s map[string]string) string {
if len(s) == 0 {
return ""
}
pms := make([]string, 0)
for k, v := range s {
pm := fmt.Sprintf("%s=%s", k, v)
pms = append(pms, pm)
}
return fmt.Sprintf("?%s", strings.Join(pms, "&"))
}