From 48f1245e6b7b85d56c268080ae024e0dfb0aa94c Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 30 Nov 2018 18:29:56 -0800 Subject: [PATCH] configs/configupgrade: Replace lookup(...) with index syntax If lookup is being used with only two arguments then it is equivalent to index syntax and more readable that way, so we'll replace it. Ideally we'd do similarly for element(...) here but sadly we cannot because we can't prove in static analysis that the user is not relying on the modulo wraparound behavior of that function. --- .../valid/funcs-replaced/input/funcs-replaced.tf | 3 +++ .../valid/funcs-replaced/want/funcs-replaced.tf | 5 +++++ configs/configupgrade/upgrade_expr.go | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/configs/configupgrade/test-fixtures/valid/funcs-replaced/input/funcs-replaced.tf b/configs/configupgrade/test-fixtures/valid/funcs-replaced/input/funcs-replaced.tf index 1f631a34c..2cea2f8ba 100644 --- a/configs/configupgrade/test-fixtures/valid/funcs-replaced/input/funcs-replaced.tf +++ b/configs/configupgrade/test-fixtures/valid/funcs-replaced/input/funcs-replaced.tf @@ -10,4 +10,7 @@ locals { list_of_map = "${list(map("a", "b"))}" map_of_list = "${map("a", list("b"))}" + + lookup_literal = "${lookup(map("a", "b"), "a")}" + lookup_ref = "${lookup(local.map, "a")}" } diff --git a/configs/configupgrade/test-fixtures/valid/funcs-replaced/want/funcs-replaced.tf b/configs/configupgrade/test-fixtures/valid/funcs-replaced/want/funcs-replaced.tf index f6c3afaa5..927ed5e47 100644 --- a/configs/configupgrade/test-fixtures/valid/funcs-replaced/want/funcs-replaced.tf +++ b/configs/configupgrade/test-fixtures/valid/funcs-replaced/want/funcs-replaced.tf @@ -26,4 +26,9 @@ locals { map_of_list = { "a" = ["b"] } + + lookup_literal = { + "a" = "b" + }["a"] + lookup_ref = local.map["a"] } diff --git a/configs/configupgrade/upgrade_expr.go b/configs/configupgrade/upgrade_expr.go index 545267871..2a0ed37aa 100644 --- a/configs/configupgrade/upgrade_expr.go +++ b/configs/configupgrade/upgrade_expr.go @@ -236,6 +236,19 @@ func upgradeExpr(val interface{}, filename string, interp bool, an *analysis) ([ buf.WriteByte('}') return buf.Bytes(), diags } + case "lookup": + // A lookup call with only two arguments is equivalent to native + // index syntax. (A third argument would specify a default value, + // so calls like that must be left alone.) + // (Note that we can't safely do this for element(...) because + // the user may be relying on its wraparound behavior.) + if len(argExprs) == 2 { + buf.Write(argExprs[0]) + buf.WriteByte('[') + buf.Write(argExprs[1]) + buf.WriteByte(']') + return buf.Bytes(), diags + } } buf.WriteString(name)