From 26c1e40ad775297cc89b32794508f4d596615d5a Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 4 Apr 2019 17:47:58 -0700 Subject: [PATCH] configs/configupgrade: Normalize number literals to decimal The v0.12 language supports numeric constants only in decimal notation, as a simplification. For rare situations where a different base is more appropriate, such as unix-style file modes, we've found it better for providers to accept a string containing a representation in the appropriate base, since that way the interpretation can be validated and it will be displayed in the same way in the rendered plan diff, in outputs, etc. We use tv.Value() here to mimick how HCL 1 itself would have interpreted these, and then format them back out in the canonical form, which implicitly converts any non-decimal constants to decimal on the way through. --- .../valid/number-literals/input/number-literals.tf | 7 +++++++ .../valid/number-literals/want/number-literals.tf | 7 +++++++ .../valid/number-literals/want/versions.tf | 3 +++ configs/configupgrade/upgrade_expr.go | 11 ++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 configs/configupgrade/test-fixtures/valid/number-literals/input/number-literals.tf create mode 100644 configs/configupgrade/test-fixtures/valid/number-literals/want/number-literals.tf create mode 100644 configs/configupgrade/test-fixtures/valid/number-literals/want/versions.tf diff --git a/configs/configupgrade/test-fixtures/valid/number-literals/input/number-literals.tf b/configs/configupgrade/test-fixtures/valid/number-literals/input/number-literals.tf new file mode 100644 index 000000000..efae79c65 --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/number-literals/input/number-literals.tf @@ -0,0 +1,7 @@ +locals { + decimal_int = 1 + decimal_float = 1.5 + decimal_float_tricky = 0.1 + hex_int = 0xff + octal_int = 0777 +} diff --git a/configs/configupgrade/test-fixtures/valid/number-literals/want/number-literals.tf b/configs/configupgrade/test-fixtures/valid/number-literals/want/number-literals.tf new file mode 100644 index 000000000..19e7376ab --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/number-literals/want/number-literals.tf @@ -0,0 +1,7 @@ +locals { + decimal_int = 1 + decimal_float = 1.5 + decimal_float_tricky = 0.1 + hex_int = 255 + octal_int = 511 +} diff --git a/configs/configupgrade/test-fixtures/valid/number-literals/want/versions.tf b/configs/configupgrade/test-fixtures/valid/number-literals/want/versions.tf new file mode 100644 index 000000000..d9b6f790b --- /dev/null +++ b/configs/configupgrade/test-fixtures/valid/number-literals/want/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} diff --git a/configs/configupgrade/upgrade_expr.go b/configs/configupgrade/upgrade_expr.go index d568e8db6..2f525a3b1 100644 --- a/configs/configupgrade/upgrade_expr.go +++ b/configs/configupgrade/upgrade_expr.go @@ -143,8 +143,17 @@ Value: buf.WriteString("false") } + case hcl1token.NUMBER: + num := tv.Value() + buf.WriteString(strconv.FormatInt(num.(int64), 10)) + + case hcl1token.FLOAT: + num := tv.Value() + buf.WriteString(strconv.FormatFloat(num.(float64), 'f', -1, 64)) + default: - // For everything else (NUMBER, FLOAT) we'll just pass through the given bytes verbatim. + // For everything else we'll just pass through the given bytes verbatim, + // but we should't get here because the above is intended to be exhaustive. buf.WriteString(tv.Text) }