From fc96bf8575e5066db65c56b92a6e2a2513e2728d Mon Sep 17 00:00:00 2001 From: Hchen <928572361@qq.com> Date: Sat, 27 May 2017 14:51:30 +0800 Subject: [PATCH 1/4] Update urlparam_test.go --- urlparam_test.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/urlparam_test.go b/urlparam_test.go index 3b36ff6..8a7c8de 100644 --- a/urlparam_test.go +++ b/urlparam_test.go @@ -3,19 +3,26 @@ package urlparam import ( "net/url" "testing" + "time" ) type TestObj struct { - Id int `url:"id"` - AppId int64 `url:"app_id"` - Name string `url:"name"` + Code string `url:"code" regex:"^[a-zA-Z0-9 .]+$" required:"true"` + Date time.Time `url:"date" layout:"2006-01-02" required:"true"` + Datetime time.Time `url:"datetime" layout:"200601021504" required:"true"` + Number int `url:"number" required:"true"` + Parameter string `url:"parameter" list:"param1,param2,param3" required:"true"` } func TestMarshal(t *testing.T) { + date, _ := time.Parse("2006-01-02", "2017-05-26") + datetime, _ := time.Parse("200601021504","201705261503") obj := &TestObj{ - Id: 1, - AppId: 1001, - Name: "test app", + Code: "123", + Date: date, + Datetime: datetime, + Number: 454, + Parameter: "param1", } params := Marshal(obj) t.Logf("params %v", params) @@ -24,13 +31,16 @@ func TestMarshal(t *testing.T) { func TestUnmarshal(t *testing.T) { params := make(url.Values) - params.Set("id", "1") - params.Set("app_id", "1001") - params.Set("name", "test app") + params.Set("code", "123") + params.Set("date", "2017-05-25") + params.Set("datetime", "201705261504") + params.Set("number", "16983") + params.Set("parameter", "param1") var obj TestObj err := Unmarshal(params, &obj) if err != nil { t.Fatal("unmarshal error: %v", err) } t.Logf("result %+v", obj) + t.Logf("params %+v", params) } From 49d0cf32ddf4faaa887085089fadca0ac4380614 Mon Sep 17 00:00:00 2001 From: Hchen <928572361@qq.com> Date: Sat, 27 May 2017 14:53:12 +0800 Subject: [PATCH 2/4] Update urlparam.go --- urlparam.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 9 deletions(-) diff --git a/urlparam.go b/urlparam.go index 68385e1..8b73c05 100644 --- a/urlparam.go +++ b/urlparam.go @@ -5,8 +5,13 @@ import ( "net/url" "reflect" "strconv" + "time" + "regexp" + "strings" + "code.byted.org/caijing_backend/alpha/service" ) + func Marshal(holder interface{}) url.Values { ret := make(url.Values) tp := reflect.TypeOf(holder) @@ -35,6 +40,7 @@ func Marshal(holder interface{}) url.Values { return ret } + func Unmarshal(params url.Values, holder interface{}) error { tp := reflect.TypeOf(holder) if tp.Kind() != reflect.Ptr { @@ -48,23 +54,44 @@ func Unmarshal(params url.Values, holder interface{}) error { if len(tag) == 0 { continue } - required := ft.Tag.Get("require") - fv := val.Field(i) + required := ft.Tag.Get("required") x := params.Get(tag) + + fv := val.Field(i) if len(x) == 0 && "true" == required { return errors.New("missing required param: " + tag) } - if fv.Kind() == reflect.String { - fv.SetString(x) + reg,ok :=ft.Tag.Lookup("regex") + if fv.Kind() == reflect.String && ok { + code, err := CheckRegex(reg, x, tag) + if err!=nil { + return err + } + fv.SetString(code) continue } - if fv.Kind() == reflect.Int || fv.Kind() == reflect.Int64 { - if len(x) == 0 { - continue + layout,ok :=ft.Tag.Lookup("layout") + if fv.Kind() == reflect.Struct && ok { + t, err := CheckLayout(layout, x, tag) + if err != nil { + return err } - intVal, err := strconv.ParseInt(x, 10, 64) + fv.Set(t) + continue + } + strList,ok :=ft.Tag.Lookup("list") + if fv.Kind() == reflect.String && ok{ + str, err := CheckList(strList, x, tag) if err != nil { - return errors.New("param " + tag + " should be integer: " + err.Error()) + return err + } + fv.SetString(str) + continue + } + if fv.Kind() == reflect.Int || fv.Kind() == reflect.Int64 { + intVal, err := CheckInt(x, tag) + if err != nil { + return err } fv.SetInt(intVal) continue @@ -73,3 +100,51 @@ func Unmarshal(params url.Values, holder interface{}) error { } return nil } + +func CheckRegex(reg, x, tag string) (val string, err error) { + regex := regexp.MustCompile(reg) + if !regex.MatchString(x) { + return "", service.NewServiceError(service.StatusInvalidParam, tag+": " + x + " is invalid parameter.") + } + return x, nil +} + +func CheckLayout(layout, x, tag string) (val reflect.Value, err error) { + t, err := time.Parse(layout, x) + if err != nil { + return reflect.ValueOf(nil), service.NewServiceError(service.StatusInvalidParam, tag+": " + x + " is invalid parameter.") + } + if t.Format(layout) != x { + return reflect.ValueOf(nil), service.NewServiceError(service.StatusInvalidParam, tag+": " + x + " is invalid parameter.") + } + return reflect.ValueOf(t), nil +} + +func CheckInt(x, tag string) (val int64, err error) { + if len(x) == 0 { + return 0, nil + } + intVal, err := strconv.ParseInt(x, 10, 64) + if err != nil { + return 0, service.NewServiceError(service.StatusInvalidParam, tag+": " + x + " is invalid parameter.") + } + return intVal, nil +} + +func CheckList(strList, x, tag string) (val string, err error) { + list := strings.Split(strList, ",") + if !CheckInList(x, list) { + return "", service.NewServiceError(service.StatusInvalidParam, tag+": " + x + " is invalid parameter.") + } + + return x, nil +} + +func CheckInList(target string, list []string) bool { + for _, item := range list { + if target == item { + return true + } + } + return false +} From 7d8cf29bc4a152e0c3b65bc083f16e85b5332546 Mon Sep 17 00:00:00 2001 From: Hchen <928572361@qq.com> Date: Sat, 27 May 2017 14:57:56 +0800 Subject: [PATCH 3/4] Update urlparam.go --- urlparam.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/urlparam.go b/urlparam.go index 8b73c05..0f7e54a 100644 --- a/urlparam.go +++ b/urlparam.go @@ -11,7 +11,6 @@ import ( "code.byted.org/caijing_backend/alpha/service" ) - func Marshal(holder interface{}) url.Values { ret := make(url.Values) tp := reflect.TypeOf(holder) @@ -56,7 +55,6 @@ func Unmarshal(params url.Values, holder interface{}) error { } required := ft.Tag.Get("required") x := params.Get(tag) - fv := val.Field(i) if len(x) == 0 && "true" == required { return errors.New("missing required param: " + tag) From 564ad7d1a45d7f141c38a541dfb017d0c0ee679f Mon Sep 17 00:00:00 2001 From: Hchen <928572361@qq.com> Date: Sat, 27 May 2017 14:59:05 +0800 Subject: [PATCH 4/4] Update urlparam.go --- urlparam.go | 1 - 1 file changed, 1 deletion(-) diff --git a/urlparam.go b/urlparam.go index 0f7e54a..2f9c5fd 100644 --- a/urlparam.go +++ b/urlparam.go @@ -39,7 +39,6 @@ func Marshal(holder interface{}) url.Values { return ret } - func Unmarshal(params url.Values, holder interface{}) error { tp := reflect.TypeOf(holder) if tp.Kind() != reflect.Ptr {