-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunmarshal.go
144 lines (108 loc) · 2.71 KB
/
unmarshal.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
package qs
import (
"fmt"
"net/url"
"regexp"
"strings"
)
var nameRegex = regexp.MustCompile(`\A[\[\]]*([^\[\]]+)\]*`)
var objectRegex1 = regexp.MustCompile(`^\[\]\[([^\[\]]+)\]$`)
var objectRegex2 = regexp.MustCompile(`^\[\](.+)$`)
func Unmarshal(qs string) (map[string]interface{}, error) {
components := strings.Split(qs, "&")
params := map[string]interface{}{}
for _, c := range components {
tuple := strings.Split(c, "=")
for i, item := range tuple {
if unesc, err := url.QueryUnescape(item); err == nil {
tuple[i] = unesc
}
}
key := ""
if len(tuple) > 0 {
key = tuple[0]
}
value := interface{}(nil)
if len(tuple) > 1 {
value = tuple[1]
}
if err := normalizeParams(params, key, value); err != nil {
return nil, err
}
}
return params, nil
}
func normalizeParams(params map[string]interface{}, key string, value interface{}) error {
after := ""
if pos := nameRegex.FindIndex([]byte(key)); len(pos) == 2 {
after = key[pos[1]:]
}
matches := nameRegex.FindStringSubmatch(key)
if len(matches) < 2 {
return nil
}
k := matches[1]
if after == "" {
params[k] = value
return nil
}
if after == "[]" {
ival, ok := params[k]
if !ok {
params[k] = []interface{}{value}
return nil
}
array, ok := ival.([]interface{})
if !ok {
return fmt.Errorf("Expected type '[]interface{}' for key '%s', but got '%T'", k, ival)
}
params[k] = append(array, value)
return nil
}
object1Matches := objectRegex1.FindStringSubmatch(after)
object2Matches := objectRegex2.FindStringSubmatch(after)
if len(object1Matches) > 1 || len(object2Matches) > 1 {
childKey := ""
if len(object1Matches) > 1 {
childKey = object1Matches[1]
} else if len(object2Matches) > 1 {
childKey = object2Matches[1]
}
if childKey != "" {
ival, ok := params[k]
if !ok {
params[k] = []interface{}{}
ival = params[k]
}
array, ok := ival.([]interface{})
if !ok {
return fmt.Errorf("Expected type '[]interface{}' for key '%s', but got '%T'", k, ival)
}
if length := len(array); length > 0 {
if hash, ok := array[length-1].(map[string]interface{}); ok {
if _, ok := hash[childKey]; !ok {
normalizeParams(hash, childKey, value)
return nil
}
}
}
newHash := map[string]interface{}{}
normalizeParams(newHash, childKey, value)
params[k] = append(array, newHash)
return nil
}
}
ival, ok := params[k]
if !ok {
params[k] = map[string]interface{}{}
ival = params[k]
}
hash, ok := ival.(map[string]interface{})
if !ok {
return fmt.Errorf("Expected type 'map[string]interface{}' for key '%s', but got '%T'", k, ival)
}
if err := normalizeParams(hash, after, value); err != nil {
return err
}
return nil
}