forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgochi.go
More file actions
47 lines (39 loc) · 1.06 KB
/
gochi.go
File metadata and controls
47 lines (39 loc) · 1.06 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
// integration: "gochi"
// https://ggicci.github.io/httpin/integrations/gochi
package httpin
import (
"mime/multipart"
"net/http"
)
// GochiURLParamFunc is chi.URLParam
type GochiURLParamFunc func(r *http.Request, key string) string
// UseGochiURLParam registers a directive executor which can extract values
// from `chi.URLParam`, i.e. path variables.
// https://ggicci.github.io/httpin/integrations/gochi
//
// Usage:
//
// func init() {
// httpin.UseGochiURLParam("path", chi.URLParam)
// }
func UseGochiURLParam(executor string, fn GochiURLParamFunc) {
RegisterDirectiveExecutor(executor, &gochiURLParamExtractor{URLParam: fn}, nil)
}
type gochiURLParamExtractor struct {
URLParam GochiURLParamFunc
}
func (chi *gochiURLParamExtractor) Execute(ctx *DirectiveContext) error {
var kvs = make(map[string][]string)
for _, key := range ctx.Argv {
value := chi.URLParam(ctx.Request, key)
if value != "" {
kvs[key] = []string{value}
}
}
extractor := &extractor{
Form: multipart.Form{
Value: kvs,
},
}
return extractor.Execute(ctx)
}