decode strings to ints
This commit is contained in:
parent
6606366746
commit
cd87a48c3c
@ -111,6 +111,13 @@ func (d *decoder) decodeInt(name string, o *hcl.Object, result reflect.Value) er
|
|||||||
switch o.Type {
|
switch o.Type {
|
||||||
case hcl.ValueTypeInt:
|
case hcl.ValueTypeInt:
|
||||||
result.Set(reflect.ValueOf(o.Value.(int)))
|
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:
|
default:
|
||||||
return fmt.Errorf("%s: unknown type %s", name, o.Type)
|
return fmt.Errorf("%s: unknown type %s", name, o.Type)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1
test-fixtures/basic_int_string.hcl
Normal file
1
test-fixtures/basic_int_string.hcl
Normal file
@ -0,0 +1 @@
|
|||||||
|
count = "3"
|
Loading…
Reference in New Issue
Block a user