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.
This commit is contained in:
Martin Atkins 2019-04-04 17:47:58 -07:00
parent ac2052f0bb
commit 26c1e40ad7
4 changed files with 27 additions and 1 deletions

View File

@ -0,0 +1,7 @@
locals {
decimal_int = 1
decimal_float = 1.5
decimal_float_tricky = 0.1
hex_int = 0xff
octal_int = 0777
}

View File

@ -0,0 +1,7 @@
locals {
decimal_int = 1
decimal_float = 1.5
decimal_float_tricky = 0.1
hex_int = 255
octal_int = 511
}

View File

@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.12"
}

View File

@ -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)
}