forked from gocraft/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
222 lines (198 loc) · 4.01 KB
/
source.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package meta
import (
"bytes"
"encoding/json"
"net/url"
"strconv"
"strings"
)
// source normalizes form value and json.
type source interface {
Get(key string) source
// A pointer must be passed.
// If the value is not present, the pointer will be nil.
Value(interface{}) Errorable
Empty() bool
ValueMap() map[string]interface{}
// Malformed source must return ErrMalformed when Value is called.
// It should be set by the parent source.
// If the parent is malformed, its children must be malformed.
Malformed() bool
}
type mergedSource []source
func newMergedSource(src ...source) source {
return mergedSource(src)
}
func (s mergedSource) Get(key string) source {
var src []source
for _, m := range s {
src = append(src, m.Get(key))
}
return newMergedSource(src...)
}
func (s mergedSource) Empty() bool {
// empty if all sources are empty
for _, m := range s {
if !m.Empty() {
return false
}
}
return true
}
func (s mergedSource) Malformed() bool {
// malformed if any source is malformed
for _, m := range s {
if m.Malformed() {
return true
}
}
return false
}
func (s mergedSource) Value(i interface{}) Errorable {
// use the first non-empty source
for _, m := range s {
if m.Malformed() {
return ErrMalformed
}
if m.Empty() {
continue
}
return m.Value(i)
}
return ErrBlank
}
func (s mergedSource) ValueMap() map[string]interface{} {
out := make(map[string]interface{})
for _, m := range s {
values := m.ValueMap()
for k, v := range values {
out[k] = v
}
}
return out
}
func newJSONSource(b []byte) source {
return &jsonSource{
RawMessage: b,
}
}
type jsonSource struct {
json.RawMessage
malformed bool
}
func (jv *jsonSource) Empty() bool {
return len(jv.RawMessage) == 0
}
func (jv *jsonSource) Malformed() bool {
return jv.malformed
}
func (jv *jsonSource) Get(key string) source {
s := &jsonSource{
malformed: jv.malformed,
}
if len(jv.RawMessage) == 0 {
return s
}
// numeric key implies array
i, err := strconv.Atoi(key)
if err == nil {
var slice []json.RawMessage
err = json.Unmarshal(jv.RawMessage, &slice)
if err != nil {
s.malformed = true
return s
}
if i >= len(slice) {
return s
}
s.RawMessage = slice[i]
return s
}
var m map[string]json.RawMessage
err = json.Unmarshal(jv.RawMessage, &m)
if err != nil {
s.malformed = true
return s
}
raw, ok := m[key]
if !ok {
return s
}
s.RawMessage = raw
return s
}
func (jv *jsonSource) Value(i interface{}) Errorable {
if len(jv.RawMessage) == 0 {
return ErrBlank
}
dec := json.NewDecoder(bytes.NewReader(jv.RawMessage))
dec.UseNumber()
err := dec.Decode(i)
if err != nil {
return ErrMalformed
}
return nil
}
func (jv *jsonSource) ValueMap() map[string]interface{} {
var out map[string]interface{}
dec := json.NewDecoder(bytes.NewReader(jv.RawMessage))
dec.UseNumber()
if err := dec.Decode(&out); err != nil {
return nil
}
return out
}
func newFormValueSource(values url.Values) source {
return &formValueSource{Values: values}
}
type formValueSource struct {
url.Values
prefix string
}
func (fv *formValueSource) Empty() bool {
for key := range fv.Values {
if key == fv.prefix || strings.HasPrefix(key, fv.prefix+".") {
return false
}
}
return true
}
func (fv *formValueSource) Get(key string) source {
if fv.prefix != "" {
key = fv.prefix + "." + key
}
return &formValueSource{
Values: fv.Values,
prefix: key,
}
}
func (fv *formValueSource) Malformed() bool {
return false
}
func (fv *formValueSource) Value(i interface{}) Errorable {
value := fv.Values.Get(fv.prefix)
switch v := i.(type) {
case *string:
*v = value
case *interface{}:
*v = value
default:
return ErrBlank
}
return nil
}
func (fv *formValueSource) ValueMap() map[string]interface{} {
out := make(map[string]interface{})
for k, v := range fv.Values {
if fv.prefix != "" && !strings.HasPrefix(k, fv.prefix+".") {
continue
}
key := strings.TrimPrefix(k, fv.prefix+".")
if len(v) == 1 {
out[key] = v[0]
} else {
out[key] = v
}
}
return out
}