Merge pull request #174 from kron4eg/decode-int
Try to use SetInt in case if possible
This commit is contained in:
commit
37ab263305
14
decoder.go
14
decoder.go
@ -91,7 +91,7 @@ func (d *decoder) decode(name string, node ast.Node, result reflect.Value) error
|
|||||||
return d.decodeBool(name, node, result)
|
return d.decodeBool(name, node, result)
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
return d.decodeFloat(name, node, result)
|
return d.decodeFloat(name, node, result)
|
||||||
case reflect.Int:
|
case reflect.Int, reflect.Int32, reflect.Int64:
|
||||||
return d.decodeInt(name, node, result)
|
return d.decodeInt(name, node, result)
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
// When we see an interface, we make our own thing
|
// When we see an interface, we make our own thing
|
||||||
@ -164,7 +164,11 @@ func (d *decoder) decodeInt(name string, node ast.Node, result reflect.Value) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Set(reflect.ValueOf(int(v)))
|
if result.Kind() == reflect.Interface {
|
||||||
|
result.Set(reflect.ValueOf(int(v)))
|
||||||
|
} else {
|
||||||
|
result.SetInt(v)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
case token.STRING:
|
case token.STRING:
|
||||||
v, err := strconv.ParseInt(n.Token.Value().(string), 0, 0)
|
v, err := strconv.ParseInt(n.Token.Value().(string), 0, 0)
|
||||||
@ -172,7 +176,11 @@ func (d *decoder) decodeInt(name string, node ast.Node, result reflect.Value) er
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Set(reflect.ValueOf(int(v)))
|
if result.Kind() == reflect.Interface {
|
||||||
|
result.Set(reflect.ValueOf(int(v)))
|
||||||
|
} else {
|
||||||
|
result.SetInt(v)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/hashicorp/hcl/hcl/ast"
|
"github.com/hashicorp/hcl/hcl/ast"
|
||||||
@ -781,6 +782,21 @@ func TestDecode_intString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecode_intStringAliased(t *testing.T) {
|
||||||
|
var value struct {
|
||||||
|
Count time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Decode(&value, testReadFile(t, "basic_int_string.hcl"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if value.Count != time.Duration(3) {
|
||||||
|
t.Fatalf("bad: %#v", value.Count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecode_Node(t *testing.T) {
|
func TestDecode_Node(t *testing.T) {
|
||||||
// given
|
// given
|
||||||
var value struct {
|
var value struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user