From 12fdc04f1b2c9e9ea1c9c2a636ef71141f342818 Mon Sep 17 00:00:00 2001 From: Witaya Tospitakkul Date: Fri, 25 Jan 2019 00:15:40 +0700 Subject: [PATCH 1/4] fix: when parse post form it didnt parse fields which have same name but the first index is empty but another is not --- go.mod | 2 +- go.sum | 1 + templatefunc.go | 29 +++++++++++++++++++++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index a7e1194b..fbdec124 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,9 @@ module github.com/astaxie/beego require ( github.com/Knetic/govaluate v3.0.0+incompatible // indirect + github.com/OwnLocal/goes v1.0.0 github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 - github.com/OwnLocal/goes v1.0.0 github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737 github.com/casbin/casbin v1.7.0 github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 diff --git a/go.sum b/go.sum index fbe3a8c3..ab233162 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg= github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= +github.com/OwnLocal/goes v1.0.0/go.mod h1:8rIFjBGTue3lCU0wplczcUgt9Gxgrkkrw7etMIcn8TM= github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd h1:jZtX5jh5IOMu0fpOTC3ayh6QGSPJ/KWOv1lgPvbRw1M= github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ= github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 h1:nYXb+3jF6Oq/j8R/y90XrKpreCxIalBWfeyeKymgOPk= diff --git a/templatefunc.go b/templatefunc.go index 9ec2a9e8..eeb34cc6 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -85,24 +85,24 @@ func DateFormat(t time.Time, layout string) (datestring string) { var datePatterns = []string{ // year "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 - "y", "06", //A two digit representation of a year Examples: 99 or 03 + "y", "06", //A two digit representation of a year Examples: 99 or 03 // month - "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 - "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 - "M", "Jan", // A short textual representation of a month, three letters Jan through Dec + "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 + "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 + "M", "Jan", // A short textual representation of a month, three letters Jan through Dec "F", "January", // A full textual representation of a month, such as January or March January through December // day "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 - "j", "2", // Day of the month without leading zeros 1 to 31 + "j", "2", // Day of the month without leading zeros 1 to 31 // week - "D", "Mon", // A textual representation of a day, three letters Mon through Sun + "D", "Mon", // A textual representation of a day, three letters Mon through Sun "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday // time - "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 + "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 @@ -297,8 +297,21 @@ func parseFormToStruct(form url.Values, objT reflect.Type, objV reflect.Value) e tag = tags[0] } + if fieldT.Type.Kind() == reflect.Slice { + found := false + for _, v := range form[tag] { + if len(v) != 0 { + found = true + break + } + } + if !found { + continue + } + } + value := form.Get(tag) - if len(value) == 0 { + if (fieldT.Type.Kind() != reflect.Slice) && len(value) == 0 { continue } From 920207f72c20161bbb45e0fb8d509d2165601df7 Mon Sep 17 00:00:00 2001 From: Witaya Tospitakkul Date: Fri, 25 Jan 2019 00:38:14 +0700 Subject: [PATCH 2/4] add testing for ParseForm when form post has a slice in body --- templatefunc_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/templatefunc_test.go b/templatefunc_test.go index e99ff880..6c1d9d72 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -111,7 +111,7 @@ func TestHtmlunquote(t *testing.T) { func TestParseForm(t *testing.T) { type ExtendInfo struct { - Hobby string `form:"hobby"` + Hobby []string `form:"hobby"` Memo string } @@ -146,7 +146,7 @@ func TestParseForm(t *testing.T) { "date": []string{"2014-11-12"}, "organization": []string{"beego"}, "title": []string{"CXO"}, - "hobby": []string{"Basketball"}, + "hobby": []string{"Basketball", "Football"}, "memo": []string{"nothing"}, } if err := ParseForm(form, u); err == nil { @@ -186,8 +186,11 @@ func TestParseForm(t *testing.T) { if u.Title != "CXO" { t.Errorf("Title should equal `CXO`, but got `%v`", u.Title) } - if u.Hobby != "Basketball" { - t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby) + if u.Hobby[0] != "Basketball" { + t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby[0]) + } + if u.Hobby[1] != "Football" { + t.Errorf("Hobby should equal `Football`, but got `%v`", u.Hobby[1]) } if len(u.Memo) != 0 { t.Errorf("Memo's length should equal 0 but got %v", len(u.Memo)) From bd1b421491d1d79f92b55344fc45341a264be624 Mon Sep 17 00:00:00 2001 From: Witaya Tospitakkul Date: Fri, 25 Jan 2019 09:04:01 +0700 Subject: [PATCH 3/4] fix: adding test for issue due to testing is not reflect changed --- templatefunc_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/templatefunc_test.go b/templatefunc_test.go index 6c1d9d72..b4c19c2e 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -146,7 +146,7 @@ func TestParseForm(t *testing.T) { "date": []string{"2014-11-12"}, "organization": []string{"beego"}, "title": []string{"CXO"}, - "hobby": []string{"Basketball", "Football"}, + "hobby": []string{"", "Basketball", "Football"}, "memo": []string{"nothing"}, } if err := ParseForm(form, u); err == nil { @@ -186,11 +186,14 @@ func TestParseForm(t *testing.T) { if u.Title != "CXO" { t.Errorf("Title should equal `CXO`, but got `%v`", u.Title) } - if u.Hobby[0] != "Basketball" { - t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby[0]) + if u.Hobby[0] != "" { + t.Errorf("Hobby should equal ``, but got `%v`", u.Hobby[0]) } - if u.Hobby[1] != "Football" { - t.Errorf("Hobby should equal `Football`, but got `%v`", u.Hobby[1]) + if u.Hobby[1] != "Basketball" { + t.Errorf("Hobby should equal `Basketball`, but got `%v`", u.Hobby[1]) + } + if u.Hobby[2] != "Football" { + t.Errorf("Hobby should equal `Football`, but got `%v`", u.Hobby[2]) } if len(u.Memo) != 0 { t.Errorf("Memo's length should equal 0 but got %v", len(u.Memo)) From 3bd7614ade4f0b9d5cd9d7f2da972a4c370805b8 Mon Sep 17 00:00:00 2001 From: Witaya Tospitakkul Date: Fri, 25 Jan 2019 11:00:24 +0700 Subject: [PATCH 4/4] refactoring code after discussion --- templatefunc.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/templatefunc.go b/templatefunc.go index eeb34cc6..d302cb64 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -297,24 +297,18 @@ func parseFormToStruct(form url.Values, objT reflect.Type, objV reflect.Value) e tag = tags[0] } - if fieldT.Type.Kind() == reflect.Slice { - found := false - for _, v := range form[tag] { - if len(v) != 0 { - found = true - break - } - } - if !found { + formValues := form[tag] + var value string + if len(formValues) == 0 { + continue + } + if len(formValues) == 1 { + value = formValues[0] + if value == "" { continue } } - value := form.Get(tag) - if (fieldT.Type.Kind() != reflect.Slice) && len(value) == 0 { - continue - } - switch fieldT.Type.Kind() { case reflect.Bool: if strings.ToLower(value) == "on" || strings.ToLower(value) == "1" || strings.ToLower(value) == "yes" {