@@ -48,6 +48,26 @@ func SetDiagnosticExtra(diag *hcl.Diagnostic, extra DiagnosticExtra) {
48
48
// Data is lost when doing a json marshal.
49
49
type Diagnostics hcl.Diagnostics
50
50
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
+
51
71
func (d * Diagnostics ) UnmarshalJSON (data []byte ) error {
52
72
cpy := make ([]FriendlyDiagnostic , 0 )
53
73
if err := json .Unmarshal (data , & cpy ); err != nil {
@@ -75,23 +95,7 @@ func (d *Diagnostics) UnmarshalJSON(data []byte) error {
75
95
}
76
96
77
97
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 ())
95
99
}
96
100
97
101
type DiagnosticSeverityString string
0 commit comments