-
Notifications
You must be signed in to change notification settings - Fork 9
/
query.go
49 lines (41 loc) 路 859 Bytes
/
query.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
package wakatime
import (
"fmt"
"net/url"
"reflect"
"strings"
)
func isNil(i interface{}) bool {
vi := reflect.ValueOf(i)
if vi.Kind() == reflect.Ptr {
return vi.IsNil()
}
return false
}
// Query defines from the url.Values.
type Query url.Values
// Parse parses the given data to a Query.
// Only parses the elements with query tag.
func (q *Query) Parse(data interface{}) error {
typ := reflect.TypeOf(data)
value := reflect.ValueOf(data)
if value.Kind() == reflect.Ptr {
if value.IsNil() {
return nil
}
value = value.Elem()
typ = typ.Elem()
}
for i := 0; i < value.NumField(); i++ {
tag := strings.Split(typ.Field(i).Tag.Get("query"), ",")[0]
v := value.Field(i)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
continue
}
v = v.Elem()
}
(*url.Values)(q).Set(tag, fmt.Sprintf("%v", v))
}
return nil
}