Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Feb 13, 2022
1 parent e815e29 commit 1cc052b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
26 changes: 21 additions & 5 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,24 @@ func (md *MetaData) unifyDatetime(data interface{}, rv reflect.Value) error {
}

func (md *MetaData) unifyString(data interface{}, rv reflect.Value) error {
if s, ok := data.(string); ok {
rv.SetString(s)
switch v := data.(type) {
case string:
rv.SetString(v)
return nil
case int:
rv.SetString(strconv.Itoa(v))
return nil
case int64:
rv.SetString(strconv.FormatInt(v, 10))
return nil
case float32:
rv.SetString(strconv.FormatFloat(float64(v), 'f', -1, 32))
return nil
case float64:
rv.SetString(strconv.FormatFloat(v, 'f', -1, 64))
return nil
case bool:
rv.SetString(strconv.FormatBool(v))
return nil
}
return badtype("string", data)
Expand Down Expand Up @@ -464,11 +480,11 @@ func (md *MetaData) unifyText(data interface{}, v TextUnmarshaler) error {
case string:
s = sdata
case bool:
s = fmt.Sprintf("%v", sdata)
s = strconv.FormatBool(sdata)
case int64:
s = fmt.Sprintf("%d", sdata)
s = strconv.FormatInt(sdata, 10)
case float64:
s = fmt.Sprintf("%f", sdata)
s = strconv.FormatFloat(sdata, 'f', -1, 64)
default:
return badtype("primitive (string-like)", data)
}
Expand Down
26 changes: 26 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,32 @@ func TestDecodeSimpleArray(t *testing.T) {
}
}

func TestDecodeSimpleString(t *testing.T) {
type file struct {
Size string
}
var s1 file
if _, err := Decode(`size = "2M"`, &s1); err != nil {
t.Fatal(err)
}
assert.Equal(t, `2M`, s1.Size)

if _, err := Decode(`size = 100.1`, &s1); err != nil {
t.Fatal(err)
}
assert.Equal(t, `100.1`, s1.Size)

if _, err := Decode(`size = 100`, &s1); err != nil {
t.Fatal(err)
}
assert.Equal(t, `100`, s1.Size)

if _, err := Decode(`size = true`, &s1); err != nil {
t.Fatal(err)
}
assert.Equal(t, `true`, s1.Size)
}

func TestDecodeArrayWrongSize(t *testing.T) {
var s1 sphere
if _, err := Decode(`center = [0.1, 2.3]`, &s1); err == nil {
Expand Down

0 comments on commit 1cc052b

Please sign in to comment.