retry to Unmarshal form value with quoted

and add test
This commit is contained in:
LaysDragon 2020-09-23 15:40:46 +08:00
parent 3100b7cb05
commit 24191b20c4
2 changed files with 48 additions and 1 deletions

View File

@ -209,7 +209,13 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
case time.Time: case time.Time:
return setTimeField(val, field, value) return setTimeField(val, field, value)
} }
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) err1 := json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
if err1 != nil {
if err2 := json.Unmarshal(bytesconv.StringToBytes(strconv.Quote(val)), value.Addr().Interface()); err2 != nil {
return fmt.Errorf("%v;tryping to parse with quoted json string but still failed: %v", err1, err2)
}
}
return nil
case reflect.Map: case reflect.Map:
return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())
default: default:

View File

@ -5,6 +5,7 @@
package binding package binding
import ( import (
"encoding/json"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@ -190,6 +191,46 @@ func TestMappingTime(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
} }
type CustomDateTime time.Time
const CustomDateTimeFormat = "2006/01/02 15:04:05"
func (t CustomDateTime) String() string {
return time.Time(t).Format(CustomDateTimeFormat)
}
func (t *CustomDateTime) UnmarshalJSON(data []byte) error {
var value string
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
parseTime, err := time.Parse(CustomDateTimeFormat, value)
if err != nil {
return err
}
*t = CustomDateTime(parseTime)
return nil
}
func TestMappingCustomStructFromJsonString(t *testing.T) {
var s struct {
Time CustomDateTime
}
value := "2020/09/23 13:20:49"
// ok
err := mappingByPtr(&s, formSource{"Time": {value}}, "form")
assert.NoError(t, err)
expectedTime, err := time.Parse(CustomDateTimeFormat, value)
assert.NoError(t, err)
assert.Equal(t, CustomDateTime(expectedTime), s.Time)
// parse failed
err = mappingByPtr(&s, formSource{"Time": {"20200/09/23 13:20:49"}}, "form")
assert.Error(t, err)
}
func TestMappingTimeDuration(t *testing.T) { func TestMappingTimeDuration(t *testing.T) {
var s struct { var s struct {
D time.Duration D time.Duration