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.
This commit is contained in:
Martin Atkins 2018-11-30 18:29:56 -08:00
parent 4927a4105b
commit 48f1245e6b
3 changed files with 21 additions and 0 deletions

View File

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

View File

@ -26,4 +26,9 @@ locals {
map_of_list = {
"a" = ["b"]
}
lookup_literal = {
"a" = "b"
}["a"]
lookup_ref = local.map["a"]
}

View File

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