From cd87a48c3c4bca64b2217b73d3ebeef0b955d1fb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 30 Sep 2014 22:29:21 -0700 Subject: [PATCH] decode strings to ints --- decoder.go | 7 +++++++ decoder_test.go | 15 +++++++++++++++ test-fixtures/basic_int_string.hcl | 1 + 3 files changed, 23 insertions(+) create mode 100644 test-fixtures/basic_int_string.hcl diff --git a/decoder.go b/decoder.go index 7fb14ab..2fcac72 100644 --- a/decoder.go +++ b/decoder.go @@ -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) } diff --git a/decoder_test.go b/decoder_test.go index 36e85f6..3a488c0 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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) + } +} diff --git a/test-fixtures/basic_int_string.hcl b/test-fixtures/basic_int_string.hcl new file mode 100644 index 0000000..4e415da --- /dev/null +++ b/test-fixtures/basic_int_string.hcl @@ -0,0 +1 @@ +count = "3"