Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Fix double-quoted map keys in diff UI #30855

Merged
merged 1 commit into from Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 6 additions & 9 deletions internal/command/format/diff.go
Expand Up @@ -1473,24 +1473,21 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
p.buf.WriteString("\n")

var allKeys []string
displayKeys := make(map[string]string)
keyLen := 0
for it := old.ElementIterator(); it.Next(); {
k, _ := it.Element()
keyStr := k.AsString()
allKeys = append(allKeys, keyStr)
displayKeys[keyStr] = displayAttributeName(keyStr)
if len(displayKeys[keyStr]) > keyLen {
keyLen = len(displayKeys[keyStr])
if len(keyStr) > keyLen {
keyLen = len(keyStr)
}
}
for it := new.ElementIterator(); it.Next(); {
k, _ := it.Element()
keyStr := k.AsString()
allKeys = append(allKeys, keyStr)
displayKeys[keyStr] = displayAttributeName(keyStr)
if len(displayKeys[keyStr]) > keyLen {
keyLen = len(displayKeys[keyStr])
if len(keyStr) > keyLen {
keyLen = len(keyStr)
}
}

Expand Down Expand Up @@ -1533,8 +1530,8 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa

p.buf.WriteString(strings.Repeat(" ", indent+2))
p.writeActionSymbol(action)
p.writeValue(cty.StringVal(displayKeys[k]), action, indent+4)
p.buf.WriteString(strings.Repeat(" ", keyLen-len(displayKeys[k])))
p.writeValue(cty.StringVal(k), action, indent+4)
p.buf.WriteString(strings.Repeat(" ", keyLen-len(k)))
p.buf.WriteString(" = ")
switch action {
case plans.Create, plans.NoOp:
Expand Down
14 changes: 10 additions & 4 deletions internal/command/format/diff_test.go
Expand Up @@ -2115,6 +2115,7 @@ func TestResourceChange_map(t *testing.T) {
"ami": cty.StringVal("ami-STATIC"),
"map_field": cty.MapVal(map[string]cty.Value{
"new-key": cty.StringVal("new-element"),
"be:ep": cty.StringVal("boop"),
}),
}),
Schema: &configschema.Block{
Expand All @@ -2129,6 +2130,7 @@ func TestResourceChange_map(t *testing.T) {
~ resource "test_instance" "example" {
~ id = "i-02ae66f368e8518a9" -> (known after apply)
+ map_field = {
+ "be:ep" = "boop"
+ "new-key" = "new-element"
}
# (1 unchanged attribute hidden)
Expand All @@ -2148,6 +2150,7 @@ func TestResourceChange_map(t *testing.T) {
"ami": cty.StringVal("ami-STATIC"),
"map_field": cty.MapVal(map[string]cty.Value{
"new-key": cty.StringVal("new-element"),
"be:ep": cty.StringVal("boop"),
}),
}),
Schema: &configschema.Block{
Expand All @@ -2162,6 +2165,7 @@ func TestResourceChange_map(t *testing.T) {
~ resource "test_instance" "example" {
~ id = "i-02ae66f368e8518a9" -> (known after apply)
~ map_field = {
+ "be:ep" = "boop"
+ "new-key" = "new-element"
}
# (1 unchanged attribute hidden)
Expand All @@ -2183,9 +2187,10 @@ func TestResourceChange_map(t *testing.T) {
"id": cty.UnknownVal(cty.String),
"ami": cty.StringVal("ami-STATIC"),
"map_field": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("aaaa"),
"b": cty.StringVal("bbbb"),
"c": cty.StringVal("cccc"),
"a": cty.StringVal("aaaa"),
"b": cty.StringVal("bbbb"),
"b:b": cty.StringVal("bbbb"),
"c": cty.StringVal("cccc"),
}),
}),
Schema: &configschema.Block{
Expand All @@ -2200,7 +2205,8 @@ func TestResourceChange_map(t *testing.T) {
~ resource "test_instance" "example" {
~ id = "i-02ae66f368e8518a9" -> (known after apply)
~ map_field = {
+ "b" = "bbbb"
+ "b" = "bbbb"
+ "b:b" = "bbbb"
# (2 unchanged elements hidden)
}
# (1 unchanged attribute hidden)
Expand Down