Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update urlparam.go & urlparam_test.go #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions urlparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"net/url"
"reflect"
"strconv"
"time"
"regexp"
"strings"
"code.byted.org/caijing_backend/alpha/service"
)

func Marshal(holder interface{}) url.Values {
Expand Down Expand Up @@ -48,23 +52,43 @@ 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
}
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 err
}
intVal, err := strconv.ParseInt(x, 10, 64)
fv.SetString(str)
continue
}
if fv.Kind() == reflect.Int || fv.Kind() == reflect.Int64 {
intVal, err := CheckInt(x, tag)
if err != nil {
return errors.New("param " + tag + " should be integer: " + err.Error())
return err
}
fv.SetInt(intVal)
continue
Expand All @@ -73,3 +97,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
}
28 changes: 19 additions & 9 deletions urlparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}