From 2c5c83578ce5a465515d087775a115f6a8ee4a0f Mon Sep 17 00:00:00 2001 From: fancl Date: Wed, 16 Aug 2023 09:57:42 +0800 Subject: [PATCH] fix bug --- entry/http/context.go | 4 ++-- util/reflection/reflection.go | 4 ++-- util/reflection/reflection_test.go | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 util/reflection/reflection_test.go diff --git a/entry/http/context.go b/entry/http/context.go index 448c62e..f6b3177 100644 --- a/entry/http/context.go +++ b/entry/http/context.go @@ -60,7 +60,7 @@ __end: break } } - return ipaddr + return strings.TrimSpace(ipaddr) } func (ctx *Context) Request() *http.Request { @@ -120,7 +120,7 @@ func (ctx *Context) Error(code int, reason string) (err error) { func (ctx *Context) Redirect(url string, code int) { if code != http.StatusFound && code != http.StatusMovedPermanently { - code = http.StatusMovedPermanently + code = http.StatusFound } http.Redirect(ctx.Response(), ctx.Request(), url, code) } diff --git a/util/reflection/reflection.go b/util/reflection/reflection.go index a289f42..a40763a 100644 --- a/util/reflection/reflection.go +++ b/util/reflection/reflection.go @@ -2,7 +2,7 @@ package reflection import "git.nspix.com/golang/kos/util/reflect" -func Setter(hacky any, variables map[string]any) (err error) { +func Setter[T string | int | int64 | float64 | any](hacky any, variables map[string]T) (err error) { for k, v := range variables { if err = Set(hacky, k, v); err != nil { return err @@ -11,6 +11,6 @@ func Setter(hacky any, variables map[string]any) (err error) { return } -func Set(hacky any, field string, value interface{}) (err error) { +func Set(hacky any, field string, value any) (err error) { return reflect.Set(hacky, field, value) } diff --git a/util/reflection/reflection_test.go b/util/reflection/reflection_test.go new file mode 100644 index 0000000..b1768e8 --- /dev/null +++ b/util/reflection/reflection_test.go @@ -0,0 +1,17 @@ +package reflection + +import "testing" + +type fake struct { + Name string `json:"name"` + Age int `json:"age"` +} + +func TestSetter(t *testing.T) { + dst := &fake{} + ms := map[string]string{"name": "aa", "age": "5"} + Setter(dst, ms) + if dst.Age != 5 { + t.Errorf("setter failed") + } +}