88 "sort"
99 "strings"
1010
11+ "github.com/hashicorp/hcl/v2/hclsyntax"
1112 "github.com/mitchellh/colorstring"
1213 "github.com/zclconf/go-cty/cty"
1314 ctyjson "github.com/zclconf/go-cty/cty/json"
@@ -320,6 +321,7 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
320321 blankBeforeBlocks := false
321322
322323 attrNames := make ([]string , 0 , len (attrsS ))
324+ displayAttrNames := make (map [string ]string , len (attrsS ))
323325 attrNameLen := 0
324326 for name := range attrsS {
325327 oldVal := ctyGetAttrMaybeNull (old , name )
@@ -333,8 +335,9 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
333335 }
334336
335337 attrNames = append (attrNames , name )
336- if len (name ) > attrNameLen {
337- attrNameLen = len (name )
338+ displayAttrNames [name ] = displayAttributeName (name )
339+ if len (displayAttrNames [name ]) > attrNameLen {
340+ attrNameLen = len (displayAttrNames [name ])
338341 }
339342 }
340343 sort .Strings (attrNames )
@@ -348,7 +351,7 @@ func (p *blockBodyDiffPrinter) writeAttrsDiff(
348351 newVal := ctyGetAttrMaybeNull (new , name )
349352
350353 result .bodyWritten = true
351- skipped := p .writeAttrDiff (name , attrS , oldVal , newVal , attrNameLen , indent , path )
354+ skipped := p .writeAttrDiff (displayAttrNames [ name ] , attrS , oldVal , newVal , attrNameLen , indent , path )
352355 if skipped {
353356 result .skippedAttributes ++
354357 }
@@ -1110,23 +1113,26 @@ func (p *blockBodyDiffPrinter) writeValue(val cty.Value, action plans.Action, in
11101113
11111114 atys := ty .AttributeTypes ()
11121115 attrNames := make ([]string , 0 , len (atys ))
1116+ displayAttrNames := make (map [string ]string , len (atys ))
11131117 nameLen := 0
11141118 for attrName := range atys {
11151119 attrNames = append (attrNames , attrName )
1116- if len (attrName ) > nameLen {
1117- nameLen = len (attrName )
1120+ displayAttrNames [attrName ] = displayAttributeName (attrName )
1121+ if len (displayAttrNames [attrName ]) > nameLen {
1122+ nameLen = len (displayAttrNames [attrName ])
11181123 }
11191124 }
11201125 sort .Strings (attrNames )
11211126
11221127 for _ , attrName := range attrNames {
11231128 val := val .GetAttr (attrName )
1129+ displayAttrName := displayAttrNames [attrName ]
11241130
11251131 p .buf .WriteString ("\n " )
11261132 p .buf .WriteString (strings .Repeat (" " , indent + 2 ))
11271133 p .writeActionSymbol (action )
1128- p .buf .WriteString (attrName )
1129- p .buf .WriteString (strings .Repeat (" " , nameLen - len (attrName )))
1134+ p .buf .WriteString (displayAttrName )
1135+ p .buf .WriteString (strings .Repeat (" " , nameLen - len (displayAttrName )))
11301136 p .buf .WriteString (" = " )
11311137 p .writeValue (val , action , indent + 4 )
11321138 }
@@ -1458,21 +1464,24 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
14581464 p .buf .WriteString ("\n " )
14591465
14601466 var allKeys []string
1467+ displayKeys := make (map [string ]string )
14611468 keyLen := 0
14621469 for it := old .ElementIterator (); it .Next (); {
14631470 k , _ := it .Element ()
14641471 keyStr := k .AsString ()
14651472 allKeys = append (allKeys , keyStr )
1466- if len (keyStr ) > keyLen {
1467- keyLen = len (keyStr )
1473+ displayKeys [keyStr ] = displayAttributeName (keyStr )
1474+ if len (displayKeys [keyStr ]) > keyLen {
1475+ keyLen = len (displayKeys [keyStr ])
14681476 }
14691477 }
14701478 for it := new .ElementIterator (); it .Next (); {
14711479 k , _ := it .Element ()
14721480 keyStr := k .AsString ()
14731481 allKeys = append (allKeys , keyStr )
1474- if len (keyStr ) > keyLen {
1475- keyLen = len (keyStr )
1482+ displayKeys [keyStr ] = displayAttributeName (keyStr )
1483+ if len (displayKeys [keyStr ]) > keyLen {
1484+ keyLen = len (displayKeys [keyStr ])
14761485 }
14771486 }
14781487
@@ -1515,8 +1524,8 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
15151524
15161525 p .buf .WriteString (strings .Repeat (" " , indent + 2 ))
15171526 p .writeActionSymbol (action )
1518- p .writeValue (kV , action , indent + 4 )
1519- p .buf .WriteString (strings .Repeat (" " , keyLen - len (k )))
1527+ p .writeValue (cty . StringVal ( displayKeys [ k ]) , action , indent + 4 )
1528+ p .buf .WriteString (strings .Repeat (" " , keyLen - len (displayKeys [ k ] )))
15201529 p .buf .WriteString (" = " )
15211530 switch action {
15221531 case plans .Create , plans .NoOp :
@@ -1563,21 +1572,24 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
15631572 forcesNewResource := p .pathForcesNewResource (path )
15641573
15651574 var allKeys []string
1575+ displayKeys := make (map [string ]string )
15661576 keyLen := 0
15671577 for it := old .ElementIterator (); it .Next (); {
15681578 k , _ := it .Element ()
15691579 keyStr := k .AsString ()
15701580 allKeys = append (allKeys , keyStr )
1571- if len (keyStr ) > keyLen {
1572- keyLen = len (keyStr )
1581+ displayKeys [keyStr ] = displayAttributeName (keyStr )
1582+ if len (displayKeys [keyStr ]) > keyLen {
1583+ keyLen = len (displayKeys [keyStr ])
15731584 }
15741585 }
15751586 for it := new .ElementIterator (); it .Next (); {
15761587 k , _ := it .Element ()
15771588 keyStr := k .AsString ()
15781589 allKeys = append (allKeys , keyStr )
1579- if len (keyStr ) > keyLen {
1580- keyLen = len (keyStr )
1590+ displayKeys [keyStr ] = displayAttributeName (keyStr )
1591+ if len (displayKeys [keyStr ]) > keyLen {
1592+ keyLen = len (displayKeys [keyStr ])
15811593 }
15821594 }
15831595
@@ -1615,8 +1627,8 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa
16151627
16161628 p .buf .WriteString (strings .Repeat (" " , indent + 2 ))
16171629 p .writeActionSymbol (action )
1618- p .buf .WriteString (k )
1619- p .buf .WriteString (strings .Repeat (" " , keyLen - len (k )))
1630+ p .buf .WriteString (displayKeys [ k ] )
1631+ p .buf .WriteString (strings .Repeat (" " , keyLen - len (displayKeys [ k ] )))
16201632 p .buf .WriteString (" = " )
16211633
16221634 switch action {
@@ -2001,3 +2013,10 @@ func (p *blockBodyDiffPrinter) writeSkippedElems(skipped, indent int) {
20012013 p .buf .WriteString ("\n " )
20022014 }
20032015}
2016+
2017+ func displayAttributeName (name string ) string {
2018+ if ! hclsyntax .ValidIdentifier (name ) {
2019+ return fmt .Sprintf ("%q" , name )
2020+ }
2021+ return name
2022+ }
0 commit comments