forked from Experticity/muxtagconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
muxtagconfig.go
73 lines (58 loc) · 1.51 KB
/
muxtagconfig.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
package muxtagconfig
import (
"net/http"
"net/url"
"reflect"
"github.com/gorilla/mux"
"github.com/Experticity/tagconfig"
)
// ParseMuxRequestToStruct is a convenience method so to wrap the operations of creating a MuxURLGetter and running
// tagconfig.Process
func ParseMuxRequestToStruct(req *http.Request, container interface{}) error {
return tagconfig.Process(&MuxURLGetter{req}, container)
}
// MuxURLGetter Implements the TagValueGetter to parse struct tagconfig into a struct instance
type MuxURLGetter struct {
*http.Request
}
func (mg *MuxURLGetter) TagName() string {
return "mux.url"
}
// Get will be called from tagconfig.Process for any fields with the tag mux.url and consult secondary fields
// mux.param, mux.form and mux.path to retrieve the appropriate value for the struct field
func (mg *MuxURLGetter) Get(key string, f reflect.StructField) (v string) {
if mg.Request == nil {
return ""
}
// To avoid shadowing, pre-declaring
var ok bool
if f.Tag.Get("mux.param") != "" {
// QueryParams
v, ok = tryURLValues(key, mg.Request.URL.Query())
if ok {
return
}
}
if f.Tag.Get("mux.form") != "" {
// PostForm Values
v, ok = tryURLValues(key, mg.Request.PostForm)
if ok {
return
}
// Form Values
v, ok = tryURLValues(key, mg.Request.Form)
if ok {
return
}
}
if f.Tag.Get("mux.path") != "" {
vs := mux.Vars(mg.Request)
return vs[key]
}
return ""
}
func tryURLValues(key string, vs url.Values) (v string, present bool) {
v = vs.Get(key)
present = v != ""
return
}