From 460dc378ea67a88ef1b559bbb21f12a7c389e276 Mon Sep 17 00:00:00 2001 From: hupeh Date: Mon, 2 Jan 2023 11:55:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=BB=91=E5=AE=9A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/net.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/net.go b/app/net.go index 32860c8..0a9674b 100644 --- a/app/net.go +++ b/app/net.go @@ -13,6 +13,11 @@ type Convertor[V any] func(s string) (V, error) type HandlerFunc func(w *ResponseWriter, r *Request) type ParamGetter func(key string) (string, bool) +// Binder 参数绑定接口 +type Binder interface { + Bind(r *Request) error +} + func GetParam[V any](r *Params, key string, def V, convertor Convertor[V], taps []TapFunc[V]) V { var v V if str, ok := r.Get(key); ok { @@ -35,7 +40,7 @@ type Params struct { } func NewParams(pg ParamGetter) *Params { - return &Params{pg } + return &Params{pg} } func NewPathParams(r *Request) *Params { @@ -101,7 +106,7 @@ func (u *Params) Uint(key string, def uint, taps ...TapFunc[uint]) uint { func (u *Params) Uint32(key string, def uint32, taps ...TapFunc[uint32]) uint32 { return GetParam[uint32](u, key, def, func(s string) (uint32, error) { - n,e := strconv.ParseUint(s, 10, 64) + n, e := strconv.ParseUint(s, 10, 64) return uint32(n), e }, taps) } @@ -125,11 +130,10 @@ func (u *Params) Float64(key string, def float64, taps ...TapFunc[float64]) floa }, taps) } - type Request struct { *http.Request *Params - pathParams *Params + pathParams *Params queryParams *Params } @@ -183,15 +187,15 @@ func (r *Request) QueryParams() *Params { type ResponseWriter struct { http.ResponseWriter - mutex sync.RWMutex - sent bool + mutex sync.RWMutex + sent bool } func NewResponseWriter(w http.ResponseWriter) *ResponseWriter { return &ResponseWriter{ ResponseWriter: w, - mutex: sync.RWMutex{}, - sent: false, + mutex: sync.RWMutex{}, + sent: false, } } @@ -265,4 +269,3 @@ func Handler(hf HandlerFunc) http.HandlerFunc { hf(NewResponseWriter(w), NewRequest(r)) } } -