decode strings to ints

This commit is contained in:
Mitchell Hashimoto 2014-09-30 22:29:21 -07:00
parent 6606366746
commit cd87a48c3c
3 changed files with 23 additions and 0 deletions

View File

@ -111,6 +111,13 @@ func (d *decoder) decodeInt(name string, o *hcl.Object, result reflect.Value) er
switch o.Type {
case hcl.ValueTypeInt:
result.Set(reflect.ValueOf(o.Value.(int)))
case hcl.ValueTypeString:
v, err := strconv.ParseInt(o.Value.(string), 0, 0)
if err != nil {
return err
}
result.SetInt(int64(v))
default:
return fmt.Errorf("%s: unknown type %s", name, o.Type)
}

View File

@ -424,3 +424,18 @@ func TestDecode_structureMap(t *testing.T) {
}
}
}
func TestDecode_intString(t *testing.T) {
var value struct {
Count int
}
err := Decode(&value, testReadFile(t, "basic_int_string.hcl"))
if err != nil {
t.Fatalf("err: %s", err)
}
if value.Count != 3 {
t.Fatalf("bad: %#v", value.Count)
}
}

View File

@ -0,0 +1 @@
count = "3"