-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecap.go
163 lines (136 loc) · 3.17 KB
/
recap.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package recap
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
func Parse(item interface{}, rx *regexp.Regexp, value string) (bool, error) {
match := rx.FindStringSubmatch(value)
m := map[string]string{}
if len(match) == 0 {
if rx.MatchString(value) {
return true, nil
}
return false, nil
}
for i, name := range rx.SubexpNames() {
if i > 0 && i < len(match) {
m[name] = match[i]
}
}
err := mapRegexGroupsToStruct(item, m, "")
if err != nil {
return false, err
} else {
return true, nil
}
}
func mapRegexGroupsToStruct(item interface{}, values map[string]string, prefix string) error {
v := reflect.ValueOf(item).Elem()
var err error
if !v.CanAddr() {
return fmt.Errorf("cannot assign to the item passed, item must be a pointer in order to assign")
}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := v.Type().Field(i)
if field.Kind() == reflect.Struct {
v := field.Addr()
tag := fieldType.Tag
subPrefix := prefix
if tagValue, ok := tag.Lookup("recap"); ok {
subPrefix = tagValue
}
err = mapRegexGroupsToStruct(v.Interface(), values, subPrefix)
if err != nil {
return err
}
continue
}
tag := fieldType.Tag
if tagVal, ok := tag.Lookup("recap"); ok {
parts := strings.Split(tagVal, ";")
valName := prefix + parts[0]
if rxVal, ok := values[valName]; ok {
if len(parts) > 1 {
rxVal = parseConditions(parts[1], rxVal)
}
typeVal, err := convertToType(rxVal, fieldType.Type.Name())
if err != nil {
return err
} else {
field.Set(reflect.ValueOf(typeVal))
}
}
}
}
return err
}
func parseConditions(conds string, value string) string {
parts := strings.Split(conds, ",")
for _, v := range parts {
method := strings.Split(v, "=")
if len(method) < 2 {
continue
}
if method[0] == "contains" {
return strconv.FormatBool(strings.Contains(value, method[1]))
}
if method[0] == "default" {
if len(value) == 0 {
return method[1]
}
}
}
return value
}
func convertToType(value string, valueType string) (interface{}, error) {
var intVal int64
var uintVal uint64
var floatVal float64
var boolVal bool
var retVal interface{}
var err error
if strings.HasPrefix(valueType, "int") {
intVal, err = strconv.ParseInt(value, 10, 64)
} else if strings.HasPrefix(valueType, "uint") {
uintVal, err = strconv.ParseUint(value, 10, 64)
} else if strings.HasPrefix(valueType, "float") {
floatVal, err = strconv.ParseFloat(value, 64)
} else if valueType == "string" {
return value, nil
} else if valueType == "bool" {
boolVal, err = strconv.ParseBool(value)
}
switch valueType {
case "bool":
retVal = boolVal
case "int":
retVal = int(intVal)
case "int8":
retVal = int8(intVal)
case "int16":
retVal = int16(intVal)
case "int32":
retVal = int32(intVal)
case "int64":
retVal = int64(intVal)
case "uint":
retVal = uint(uintVal)
case "uint8":
retVal = uint8(uintVal)
case "uint16":
retVal = uint16(uintVal)
case "uint32":
retVal = uint32(uintVal)
case "uint64":
retVal = uint64(uintVal)
case "float32":
retVal = float32(floatVal)
case "float64":
retVal = float64(floatVal)
}
return retVal, err
}