-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv_mapping.go
44 lines (34 loc) · 1.59 KB
/
kv_mapping.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
package binding
import (
"github.com/hopeio/cherry/utils/net/http/binding"
stringsi "github.com/hopeio/cherry/utils/strings"
"github.com/valyala/fasthttp"
"reflect"
)
type ArgsSource fasthttp.Args
// TrySet tries to set a value by request's form source (like map[string][]string)
func (form *ArgsSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt binding.SetOptions) (isSet bool, err error) {
return binding.SetByKV(value, field, form, tagValue, opt)
}
func (form *ArgsSource) Peek(key string) ([]string, bool) {
v := stringsi.BytesToString((*fasthttp.Args)(form).Peek(key))
return []string{v}, v != ""
}
type CtxSource fasthttp.RequestCtx
// TrySet tries to set a value by request's form source (like map[string][]string)
func (form *CtxSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt binding.SetOptions) (isSet bool, err error) {
return binding.SetByKV(value, field, form, tagValue, opt)
}
func (form *CtxSource) Peek(key string) ([]string, bool) {
v := (*fasthttp.RequestCtx)(form).UserValue(key).(string)
return []string{v}, v != ""
}
type HeaderSource fasthttp.RequestHeader
// TrySet tries to set a value by request's form source (like map[string][]string)
func (form *HeaderSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt binding.SetOptions) (isSet bool, err error) {
return binding.SetByKV(value, field, form, tagValue, opt)
}
func (form *HeaderSource) Peek(key string) ([]string, bool) {
v := stringsi.BytesToString((*fasthttp.RequestHeader)(form).Peek(key))
return []string{v}, v != ""
}