-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
65 lines (52 loc) · 1.16 KB
/
util.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tagrunner
import (
"reflect"
"strings"
"github.com/pkg/errors"
)
func JsonTagNameFromField(field reflect.StructField) string {
j := strings.Split(field.Tag.Get("json"), ",")
if len(j) == 0 || j[0] == "-" || j[0] == "" {
return field.Name
}
return j[0]
}
type valAndArgs struct {
val string
args []string
}
// deconstructTag compile the tag in pattern `{key}:"valA=argA1 argA2,valB=argB1 argB2"`, with returning
// []valAndArgs{
// {val: "valA", args: []string{"argA1", "argA2"}}
// {val: "valB", args: []string{"argB1", "argB2"}}
// }
func deconstructTag(tag string) ([]valAndArgs, error) {
result := make([]valAndArgs, 0)
for _, sec := range strings.Split(tag, ",") {
if sec == "" {
continue
}
vas := strings.Split(sec, "=")
if len(vas) > 2 {
return nil, errors.Wrap(ErrInvalidTagPattern, "sections more than 2")
}
val := vas[0]
if val == "" {
continue
}
args := make([]string, 0)
if len(vas) == 2 {
for _, a := range strings.Split(vas[1], " ") {
if a == "" {
continue
}
args = append(args, a)
}
}
result = append(result, valAndArgs{
val: val,
args: args,
})
}
return result, nil
}