This commit is contained in:
fancl 2023-08-16 09:57:42 +08:00
parent d24f7efb0b
commit 2c5c83578c
3 changed files with 21 additions and 4 deletions

View File

@ -60,7 +60,7 @@ __end:
break break
} }
} }
return ipaddr return strings.TrimSpace(ipaddr)
} }
func (ctx *Context) Request() *http.Request { 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) { func (ctx *Context) Redirect(url string, code int) {
if code != http.StatusFound && code != http.StatusMovedPermanently { if code != http.StatusFound && code != http.StatusMovedPermanently {
code = http.StatusMovedPermanently code = http.StatusFound
} }
http.Redirect(ctx.Response(), ctx.Request(), url, code) http.Redirect(ctx.Response(), ctx.Request(), url, code)
} }

View File

@ -2,7 +2,7 @@ package reflection
import "git.nspix.com/golang/kos/util/reflect" 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 { for k, v := range variables {
if err = Set(hacky, k, v); err != nil { if err = Set(hacky, k, v); err != nil {
return err return err
@ -11,6 +11,6 @@ func Setter(hacky any, variables map[string]any) (err error) {
return 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) return reflect.Set(hacky, field, value)
} }

View File

@ -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")
}
}