Structured Plan Renderer: Fix minor bugs causing diffs in the equivalence tests. (#32519)

* remove attributes that do not match the relevant attributes filter

* fix formatting

* fix renderer function, don't drop irrelevant attributes just mark them as no-ops

* fix imports

* fix bugs in the renderer exposed by the equivalence tests

* imports

* gofmt
This commit is contained in:
Liam Cervante
2023-01-17 09:31:29 +01:00
committed by GitHub
parent e015b15f12
commit 99823e4a15
10 changed files with 160 additions and 39 deletions

View File

@@ -57,7 +57,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
escapedAttributeKeys := make(map[string]string)
for key := range renderer.attributes {
attributeKeys = append(attributeKeys, key)
escapedKey := ensureValidAttributeName(key)
escapedKey := EnsureValidAttributeName(key)
escapedAttributeKeys[key] = escapedKey
if maximumAttributeKeyLen < len(escapedKey) {
maximumAttributeKeyLen = len(escapedKey)
@@ -141,7 +141,7 @@ func (renderer blockRenderer) RenderHuman(diff computed.Diff, indent int, opts c
for _, warning := range diff.WarningsHuman(indent+1, blockOpts) {
buf.WriteString(fmt.Sprintf("%s%s\n", formatIndent(indent+1), warning))
}
buf.WriteString(fmt.Sprintf("%s%s %s%s %s\n", formatIndent(indent+1), colorizeDiffAction(diff.Action, blockOpts), ensureValidAttributeName(key), mapKey, diff.RenderHuman(indent+1, blockOpts)))
buf.WriteString(fmt.Sprintf("%s%s %s%s %s\n", formatIndent(indent+1), colorizeDiffAction(diff.Action, blockOpts), EnsureValidAttributeName(key), mapKey, diff.RenderHuman(indent+1, blockOpts)))
}

View File

@@ -50,7 +50,7 @@ func (renderer objectRenderer) RenderHuman(diff computed.Diff, indent int, opts
escapedKeys := make(map[string]string)
for key := range renderer.attributes {
keys = append(keys, key)
escapedKey := ensureValidAttributeName(key)
escapedKey := EnsureValidAttributeName(key)
escapedKeys[key] = escapedKey
if maximumKeyLen < len(escapedKey) {
maximumKeyLen = len(escapedKey)

View File

@@ -177,12 +177,14 @@ func (renderer primitiveRenderer) renderStringDiff(diff computed.Diff, indent in
}
func (renderer primitiveRenderer) renderStringDiffAsJson(diff computed.Diff, indent int, opts computed.RenderHumanOpts, before evaluatedString, after evaluatedString) string {
jsonDiff := RendererJsonOpts().Transform(before.Json, after.Json, attribute_path.AlwaysMatcher())
jsonDiff := RendererJsonOpts().Transform(before.Json, after.Json, diff.Action != plans.Create, diff.Action != plans.Delete, attribute_path.AlwaysMatcher())
action := diff.Action
var whitespace, replace string
jsonOpts := opts.Clone()
jsonOpts.OverrideNullSuffix = true
var whitespace, replace string
if jsonDiff.Action == plans.NoOp && diff.Action == plans.Update {
// Then this means we are rendering a whitespace only change. The JSON
// differ will have ignored the whitespace changes so that makes the
@@ -214,7 +216,7 @@ func (renderer primitiveRenderer) renderStringDiffAsJson(diff computed.Diff, ind
}
if strings.Contains(renderedJsonDiff, "\n") {
return fmt.Sprintf("jsonencode(%s\n%s%s %s%s\n%s)", whitespace, formatIndent(indent+1), colorizeDiffAction(action, opts), renderedJsonDiff, replace, formatIndent(indent+1))
return fmt.Sprintf("jsonencode(%s\n%s%s %s%s\n%s)%s", whitespace, formatIndent(indent+1), colorizeDiffAction(action, opts), renderedJsonDiff, replace, formatIndent(indent+1), nullSuffix(diff.Action, opts))
}
return fmt.Sprintf("jsonencode(%s)%s%s", renderedJsonDiff, whitespace, replace)
}

View File

@@ -137,8 +137,8 @@ jsonencode(
{
- key_one = "value_one"
- key_two = "value_two"
} -> null
)
}
) -> null
`,
},
"primitive_json_string_update": {
@@ -153,6 +153,20 @@ jsonencode(
# (2 unchanged attributes hidden)
}
)
`,
},
"primitive_json_explicit_nulls": {
diff: computed.Diff{
Renderer: Primitive("{\"key_one\":\"value_one\",\"key_two\":\"value_two\"}", "{\"key_one\":null}", cty.String),
Action: plans.Update,
},
expected: `
jsonencode(
~ {
~ key_one = "value_one" -> null
- key_two = "value_two"
}
)
`,
},
"primitive_fake_json_string_update": {

View File

@@ -55,9 +55,9 @@ func unchanged(keyword string, count int, opts computed.RenderHumanOpts) string
return opts.Colorize.Color(fmt.Sprintf("[dark_gray]# (%d unchanged %ss hidden)[reset]", count, keyword))
}
// ensureValidAttributeName checks if `name` contains any HCL syntax and calls
// EnsureValidAttributeName checks if `name` contains any HCL syntax and calls
// and returns hclEscapeString.
func ensureValidAttributeName(name string) string {
func EnsureValidAttributeName(name string) string {
if !hclsyntax.ValidIdentifier(name) {
return hclEscapeString(name)
}