From 39143f46f8064c03ccbfdb8588da88964dc64d5c Mon Sep 17 00:00:00 2001 From: James Nugent Date: Mon, 20 Jun 2016 19:53:10 +0300 Subject: [PATCH] Fix bug with unquoting null from JSON-encoded HCL Fixes hashicorp/terraform#6774. --- decoder_test.go | 13 +++++++++++++ hcl/token/token.go | 5 +++++ test-fixtures/null_strings.json | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 test-fixtures/null_strings.json diff --git a/decoder_test.go b/decoder_test.go index 79994cb..b441496 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -109,6 +109,19 @@ func TestDecode_interface(t *testing.T) { false, map[string]interface{}{"foo": "bar\nbaz"}, }, + { + "null_strings.json", + false, + map[string]interface{}{ + "module": []map[string]interface{}{ + map[string]interface{}{ + "app": []map[string]interface{}{ + map[string]interface{}{"foo": ""}, + }, + }, + }, + }, + }, { "scientific.json", false, diff --git a/hcl/token/token.go b/hcl/token/token.go index 6e99498..e37c066 100644 --- a/hcl/token/token.go +++ b/hcl/token/token.go @@ -152,6 +152,11 @@ func (t Token) Value() interface{} { f = strconv.Unquote } + // This case occurs if json null is used + if t.Text == "" { + return "" + } + v, err := f(t.Text) if err != nil { panic(fmt.Sprintf("unquote %s err: %s", t.Text, err)) diff --git a/test-fixtures/null_strings.json b/test-fixtures/null_strings.json new file mode 100644 index 0000000..a5b8a5a --- /dev/null +++ b/test-fixtures/null_strings.json @@ -0,0 +1,7 @@ +{ + "module": { + "app": { + "foo": null + } + } +}