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

Decode: convert ints if target type is compatible #594

Merged
merged 2 commits into from
Sep 10, 2021
Merged
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
23 changes: 16 additions & 7 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,22 @@ func TestIssue424(t *testing.T) {
require.Equal(t, msg2, msg2parsed)
}

func TestIssue567(t *testing.T) {
var m map[string]interface{}
err := toml.Unmarshal([]byte("A = 12:08:05"), &m)
require.NoError(t, err)
require.IsType(t, m["A"], toml.LocalTime{})
}

func TestIssue590(t *testing.T) {
type CustomType int
var cfg struct {
Option CustomType `toml:"option"`
}
err := toml.Unmarshal([]byte("option = 42"), &cfg)
require.NoError(t, err)
}

func ExampleMarshal() {
type MyConfig struct {
Version int
Expand All @@ -806,10 +822,3 @@ func ExampleMarshal() {
// Name = 'go-toml'
// Tags = ['go', 'toml']
}

func TestIssue567(t *testing.T) {
var m map[string]interface{}
err := toml.Unmarshal([]byte("A = 12:08:05"), &m)
require.NoError(t, err)
require.IsType(t, m["A"], toml.LocalTime{})
}
34 changes: 21 additions & 13 deletions unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,71 +823,79 @@ func (d *decoder) unmarshalInteger(value *ast.Node, v reflect.Value) error {
return err
}

var r reflect.Value

switch v.Kind() {
case reflect.Int64:
v.SetInt(i)
return nil
case reflect.Int32:
if i < math.MinInt32 || i > math.MaxInt32 {
return fmt.Errorf("toml: number %d does not fit in an int32", i)
}

v.Set(reflect.ValueOf(int32(i)))
return nil
r = reflect.ValueOf(int32(i))
case reflect.Int16:
if i < math.MinInt16 || i > math.MaxInt16 {
return fmt.Errorf("toml: number %d does not fit in an int16", i)
}

v.Set(reflect.ValueOf(int16(i)))
r = reflect.ValueOf(int16(i))
case reflect.Int8:
if i < math.MinInt8 || i > math.MaxInt8 {
return fmt.Errorf("toml: number %d does not fit in an int8", i)
}

v.Set(reflect.ValueOf(int8(i)))
r = reflect.ValueOf(int8(i))
case reflect.Int:
if i < minInt || i > maxInt {
return fmt.Errorf("toml: number %d does not fit in an int", i)
}

v.Set(reflect.ValueOf(int(i)))
r = reflect.ValueOf(int(i))
case reflect.Uint64:
if i < 0 {
return fmt.Errorf("toml: negative number %d does not fit in an uint64", i)
}

v.Set(reflect.ValueOf(uint64(i)))
r = reflect.ValueOf(uint64(i))
case reflect.Uint32:
if i < 0 || i > math.MaxUint32 {
return fmt.Errorf("toml: negative number %d does not fit in an uint32", i)
}

v.Set(reflect.ValueOf(uint32(i)))
r = reflect.ValueOf(uint32(i))
case reflect.Uint16:
if i < 0 || i > math.MaxUint16 {
return fmt.Errorf("toml: negative number %d does not fit in an uint16", i)
}

v.Set(reflect.ValueOf(uint16(i)))
r = reflect.ValueOf(uint16(i))
case reflect.Uint8:
if i < 0 || i > math.MaxUint8 {
return fmt.Errorf("toml: negative number %d does not fit in an uint8", i)
}

v.Set(reflect.ValueOf(uint8(i)))
r = reflect.ValueOf(uint8(i))
case reflect.Uint:
if i < 0 {
return fmt.Errorf("toml: negative number %d does not fit in an uint", i)
}

v.Set(reflect.ValueOf(uint(i)))
r = reflect.ValueOf(uint(i))
case reflect.Interface:
v.Set(reflect.ValueOf(i))
r = reflect.ValueOf(i)
default:
err = fmt.Errorf("toml: cannot store TOML integer into a Go %s", v.Kind())
return fmt.Errorf("toml: cannot store TOML integer into a Go %s", v.Kind())
}

return err
if !r.Type().AssignableTo(v.Type()) {
r = r.Convert(v.Type())
}

v.Set(r)

return nil
}

func (d *decoder) unmarshalString(value *ast.Node, v reflect.Value) error {
Expand Down