fancl 1 year ago
parent
commit
2c5c83578c
3 changed files with 21 additions and 4 deletions
  1. 2 2
      entry/http/context.go
  2. 2 2
      util/reflection/reflection.go
  3. 17 0
      util/reflection/reflection_test.go

+ 2 - 2
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)
 }

+ 2 - 2
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)
 }

+ 17 - 0
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")
+	}
+}