From 783205948c56ef3dd2cb5f062df377077b05ec78 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 29 Aug 2018 19:29:54 -0700 Subject: [PATCH] command/format: JSON value formatting heuristic not for primitive values Since our own syntax for primitive values is similar to that of JSON, and since we permit automatic conversions from number and bool to string, we must do this special JSON value diff formatting only if the value is a JSON array or object to avoid confusing results. --- command/format/diff.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/command/format/diff.go b/command/format/diff.go index e642f4c95..bc9a4bdd4 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -424,10 +424,10 @@ func (p *blockBodyDiffPrinter) writeValue(val cty.Value, action plans.Action, in switch ty { case cty.String: { - // Special behavior for JSON strings + // Special behavior for JSON strings containing array or object src := []byte(val.AsString()) ty, err := ctyjson.ImpliedType(src) - if err == nil { + if err == nil && !ty.IsPrimitiveType() { jv, err := ctyjson.Unmarshal(src, ty) if err == nil { p.buf.WriteString("jsonencode(") @@ -548,12 +548,13 @@ func (p *blockBodyDiffPrinter) writeValueDiff(old, new cty.Value, indent int, pa newS := new.AsString() { - // Special behavior for JSON strings + // Special behavior for JSON strings containing object or + // list values. oldBytes := []byte(oldS) newBytes := []byte(newS) oldType, oldErr := ctyjson.ImpliedType(oldBytes) newType, newErr := ctyjson.ImpliedType(newBytes) - if oldErr == nil && newErr == nil { + if oldErr == nil && newErr == nil && !(oldType.IsPrimitiveType() && newType.IsPrimitiveType()) { oldJV, oldErr := ctyjson.Unmarshal(oldBytes, oldType) newJV, newErr := ctyjson.Unmarshal(newBytes, newType) if oldErr == nil && newErr == nil {