Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions types/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func SetDiagnosticExtra(diag *hcl.Diagnostic, extra DiagnosticExtra) {
// Data is lost when doing a json marshal.
type Diagnostics hcl.Diagnostics

func (d Diagnostics) FriendlyDiagnostics() []FriendlyDiagnostic {
cpy := make([]FriendlyDiagnostic, 0, len(d))
for _, diag := range d {
severity := DiagnosticSeverityError
if diag.Severity == hcl.DiagWarning {
severity = DiagnosticSeverityWarning
}

extra := ExtractDiagnosticExtra(diag)

cpy = append(cpy, FriendlyDiagnostic{
Severity: severity,
Summary: diag.Summary,
Detail: diag.Detail,
Extra: extra,
})
}
return cpy
}

func (d *Diagnostics) UnmarshalJSON(data []byte) error {
cpy := make([]FriendlyDiagnostic, 0)
if err := json.Unmarshal(data, &cpy); err != nil {
Expand Down Expand Up @@ -75,23 +95,7 @@ func (d *Diagnostics) UnmarshalJSON(data []byte) error {
}

func (d Diagnostics) MarshalJSON() ([]byte, error) {
cpy := make([]FriendlyDiagnostic, 0, len(d))
for _, diag := range d {
severity := DiagnosticSeverityError
if diag.Severity == hcl.DiagWarning {
severity = DiagnosticSeverityWarning
}

extra := ExtractDiagnosticExtra(diag)

cpy = append(cpy, FriendlyDiagnostic{
Severity: severity,
Summary: diag.Summary,
Detail: diag.Detail,
Extra: extra,
})
}
return json.Marshal(cpy)
return json.Marshal(d.FriendlyDiagnostics())
}

type DiagnosticSeverityString string
Expand Down
10 changes: 7 additions & 3 deletions types/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ func ToHCLString(block *terraform.Block, attr *terraform.Attribute) HCLString {
}
}

func (s HCLString) MarshalJSON() ([]byte, error) {
return json.Marshal(NullHCLString{
func (s HCLString) NullHCLString() NullHCLString {
return NullHCLString{
Value: s.AsString(),
Valid: s.Valid() && s.Value.IsKnown(),
})
}
}

func (s HCLString) MarshalJSON() ([]byte, error) {
return json.Marshal(s.NullHCLString())
}

func (s *HCLString) UnmarshalJSON(data []byte) error {
Expand Down
Loading