This repository was archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
179 lines (163 loc) · 4.26 KB
/
parser.go
File metadata and controls
179 lines (163 loc) · 4.26 KB
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
package httputil
import (
"context"
"encoding"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/deixis/errors"
"github.com/deixis/spine/log"
"github.com/deixis/spine/net/http"
)
const queryStringTag = "qs"
// ParseReq parses the request and returns a standard error in case of failure
func ParseReq(ctx context.Context, r *http.Request, params interface{}) error {
err := r.Parse(ctx, params)
if err != nil {
log.Warn(ctx, "http.parse.err", "Cannot parse request",
log.Error(err),
)
return errors.WithBad(err)
}
return nil
}
// ParseQuery parses the values of v from the HTTP query
func ParseQuery(q url.Values, v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return errors.New("httputil: ParseQuery(non-pointer " + reflect.TypeOf(v).String() + ")")
}
rv = reflect.Indirect(rv)
tv := rv.Type()
switch rv.Kind() {
case reflect.Struct:
for i := 0; i < rv.NumField(); i++ {
field := tv.Field(i)
val := rv.Field(i)
// If the current field has a query string tags
tag, ok := field.Tag.Lookup(queryStringTag)
if !ok {
continue
}
tag, opts := parseTag(tag)
// If the query string has the given tag name
qVal := q.Get(tag)
if qVal == "" {
if opts.Contains("required") {
return errors.Bad(&errors.FieldViolation{
Field: tag,
Description: "Missing query string",
})
}
continue
}
// walk down to get the first non-pointer
ut, nptr := indirect(val)
if ut != nil {
if err := ut.UnmarshalText([]byte(qVal)); err != nil {
return errors.WithBad(err)
}
continue
}
val = nptr
// Parse primitives
switch reflect.Indirect(val).Kind() {
case reflect.Int, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(qVal, 10, 64)
if err != nil {
return errors.WithBad(err)
}
val.SetInt(i)
case reflect.Uint, reflect.Uint32, reflect.Uint64:
i, err := strconv.ParseUint(qVal, 10, 64)
if err != nil {
return errors.WithBad(err)
}
val.SetUint(i)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(qVal, 64)
if err != nil {
return errors.WithBad(err)
}
val.SetFloat(f)
case reflect.Bool:
b, err := strconv.ParseBool(qVal)
if err != nil {
return errors.WithBad(err)
}
val.SetBool(b)
case reflect.String:
val.SetString(qVal)
}
}
default:
return errors.New("httputil: ParseQuery(unsupported type " + reflect.TypeOf(v).String() + ")")
}
return nil
}
// indirect walks down v allocating pointers as needed, until it gets to a non-pointer.
func indirect(v reflect.Value) (encoding.TextUnmarshaler, reflect.Value) {
// If v is a named type and is addressable,
// start with its address, so that if the type has pointer methods,
// we find them.
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
v = v.Addr()
}
for {
// Load value from interface, but only if the result will be
// usefully addressable.
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && (e.Elem().Kind() == reflect.Ptr) {
v = e
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
if v.Type().NumMethod() > 0 {
if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
return u, reflect.Value{}
}
}
v = v.Elem()
}
return nil, v
}
// tagOptions is the string following a comma in a struct field's "qs"
// tag, or the empty string. It does not include the leading comma.
type tagOptions string
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
// Contains reports whether a comma-separated list of options
// contains a particular substr flag. substr must be surrounded by a
// string boundary or commas.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}