-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal.go
164 lines (121 loc) · 2.71 KB
/
marshal.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
164
package querystring
import (
"fmt"
"net/url"
"reflect"
"strings"
"time"
)
//Marshal nested struct or map to url query strings
func Marshal(obj interface{}) (string, error) {
values := make(url.Values)
err := encode(obj, "", values)
return values.Encode(), err
}
//Stringify
func Stringify(obj interface{}) (string, error) {
return Marshal(obj)
}
func encode(val interface{}, prefix string, values url.Values) error {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return nil
}
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
err := encodeStruct(v, prefix, values)
if err != nil {
return err
}
case reflect.Map:
keys := v.MapKeys()
for _, k := range keys {
itemValue := v.MapIndex(k)
key := k.Interface().(string)
if len(prefix) > 0 {
key = fmt.Sprintf("[%s]", k.Interface().(string))
}
err := encode(itemValue.Interface(), prefix+key, values)
if err != nil {
return err
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
sliceValue := v.Index(i)
if sliceValue.Kind() == reflect.Ptr {
sliceValue = sliceValue.Elem()
}
key := fmt.Sprintf("[%d]", i)
err := encode(sliceValue.Interface(), prefix+key, values)
if err != nil {
return err
}
}
default:
if len(prefix) > 0 {
values.Add(prefix, valueToString(v))
}
}
return nil
}
func parseFieldTag(tag string) (string, []string) {
s := strings.Split(tag, ",")
return s[0], s[1:]
}
func encodeStruct(v reflect.Value, prefix string, values url.Values) error {
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
tf := typ.Field(i)
sv := v.Field(i)
tag, _ := tf.Tag.Lookup("url")
if tag == "-" {
continue
}
name := strings.ToLower(tf.Name)
if len(tag) >= 0 {
tname, _ := parseFieldTag(tag)
if len(tname) > 0 {
name = tname
}
}
key := name
if len(prefix) > 0 {
key = fmt.Sprintf("[%s]", name)
}
switch sv.Interface().(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
bool, string, time.Time:
values.Add(prefix+key, valueToString(sv))
case *int, *int8, *int16, *int32, *int64,
*uint, *uint8, *uint16, *uint32, *uint64,
*float32, *float64,
*bool, *string, *time.Time:
values.Add(prefix+key, valueToString(sv))
default:
err := encode(sv.Interface(), prefix+key, values)
if err != nil {
return err
}
}
}
return nil
}
func valueToString(v reflect.Value) string {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return ""
}
v = v.Elem()
}
if v.Type() == reflect.TypeOf(time.Time{}) {
t := v.Interface().(time.Time)
return t.Format(time.RFC3339)
}
return fmt.Sprint(v.Interface())
}