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.
This commit is contained in:
Martin Atkins 2018-05-01 13:18:10 -07:00
parent 70f1635416
commit 9ca13d5c1d
1 changed files with 5 additions and 0 deletions

View File

@ -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 {