Skip to content

Commit ac39143

Browse files
authored
Merge pull request #130 from coder/stevenmasley/converters
chore: add converter methods to diagnostic and hclstring
2 parents 9606efe + 3ab2022 commit ac39143

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

types/diagnostics.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ func SetDiagnosticExtra(diag *hcl.Diagnostic, extra DiagnosticExtra) {
4848
// Data is lost when doing a json marshal.
4949
type Diagnostics hcl.Diagnostics
5050

51+
func (d Diagnostics) FriendlyDiagnostics() []FriendlyDiagnostic {
52+
cpy := make([]FriendlyDiagnostic, 0, len(d))
53+
for _, diag := range d {
54+
severity := DiagnosticSeverityError
55+
if diag.Severity == hcl.DiagWarning {
56+
severity = DiagnosticSeverityWarning
57+
}
58+
59+
extra := ExtractDiagnosticExtra(diag)
60+
61+
cpy = append(cpy, FriendlyDiagnostic{
62+
Severity: severity,
63+
Summary: diag.Summary,
64+
Detail: diag.Detail,
65+
Extra: extra,
66+
})
67+
}
68+
return cpy
69+
}
70+
5171
func (d *Diagnostics) UnmarshalJSON(data []byte) error {
5272
cpy := make([]FriendlyDiagnostic, 0)
5373
if err := json.Unmarshal(data, &cpy); err != nil {
@@ -75,23 +95,7 @@ func (d *Diagnostics) UnmarshalJSON(data []byte) error {
7595
}
7696

7797
func (d Diagnostics) MarshalJSON() ([]byte, error) {
78-
cpy := make([]FriendlyDiagnostic, 0, len(d))
79-
for _, diag := range d {
80-
severity := DiagnosticSeverityError
81-
if diag.Severity == hcl.DiagWarning {
82-
severity = DiagnosticSeverityWarning
83-
}
84-
85-
extra := ExtractDiagnosticExtra(diag)
86-
87-
cpy = append(cpy, FriendlyDiagnostic{
88-
Severity: severity,
89-
Summary: diag.Summary,
90-
Detail: diag.Detail,
91-
Extra: extra,
92-
})
93-
}
94-
return json.Marshal(cpy)
98+
return json.Marshal(d.FriendlyDiagnostics())
9599
}
96100

97101
type DiagnosticSeverityString string

types/value.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ func ToHCLString(block *terraform.Block, attr *terraform.Attribute) HCLString {
4646
}
4747
}
4848

49-
func (s HCLString) MarshalJSON() ([]byte, error) {
50-
return json.Marshal(NullHCLString{
49+
func (s HCLString) NullHCLString() NullHCLString {
50+
return NullHCLString{
5151
Value: s.AsString(),
5252
Valid: s.Valid() && s.Value.IsKnown(),
53-
})
53+
}
54+
}
55+
56+
func (s HCLString) MarshalJSON() ([]byte, error) {
57+
return json.Marshal(s.NullHCLString())
5458
}
5559

5660
func (s *HCLString) UnmarshalJSON(data []byte) error {

0 commit comments

Comments
 (0)