From 9ca13d5c1d7c8fa2eea9a113687ab44679cceb63 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 1 May 2018 13:18:10 -0700 Subject: [PATCH] command/validate: make sure diagnostics is always present and an array Previously an empty diagnostics would appear as "null" in the JSON output, since that is how encoding/json serializes a nil slice. It's more convenient for users of dynamic languages to keep the type consistent in all cases, since they can then just iterate the list without needing a special case for when it is null. --- command/validate.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/command/validate.go b/command/validate.go index 9250f6463..cc7b4703c 100644 --- a/command/validate.go +++ b/command/validate.go @@ -203,6 +203,11 @@ func (c *ValidateCommand) showResults(diags tfdiags.Diagnostics, jsonOutput bool output.Diagnostics = append(output.Diagnostics, jsonDiag) } + if output.Diagnostics == nil { + // Make sure this always appears as an array in our output, since + // this is easier to consume for dynamically-typed languages. + output.Diagnostics = []Diagnostic{} + } j, err := json.MarshalIndent(&output, "", " ") if err != nil {