/
url.go
32 lines (27 loc) · 920 Bytes
/
url.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
package midware
import (
"net/http"
"reflect"
"github.com/adamcolton/luce/util/filter"
"github.com/adamcolton/luce/util/linject"
"github.com/gorilla/mux"
)
// URLFieldSetter references a Var in gorilla/mux.Vars. It fulfills
// FieldSetterInitilizer and FieldSetter. It is used to extract values from a
// URL and set them in the magic data field.
type URLFieldSetter struct {
Var string
}
// Vars references mux.Vars. It is left exposed for testing.
var (
Vars = mux.Vars
isString = filter.IsKind(reflect.String)
)
// URL creates a magic Initilizer to extract a segment from the URL by name and
// set it to a magic data field by fieldName.
func URL(segment, fieldName string) linject.Field {
fn := func(w http.ResponseWriter, r *http.Request) (string, func([]reflect.Value), error) {
return Vars(r)[segment], nil, nil
}
return linject.NewFieldSetter(fn, "TestField", filter.AnyType(), isString)
}