File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,28 @@ import (
1010// Data is lost when doing a json marshal.
1111type Diagnostics hcl.Diagnostics
1212
13+ func (d * Diagnostics ) UnmarshalJSON (data []byte ) error {
14+ cpy := make ([]FriendlyDiagnostic , 0 )
15+ if err := json .Unmarshal (data , & cpy ); err != nil {
16+ return err
17+ }
18+
19+ * d = make (Diagnostics , 0 , len (cpy ))
20+ for _ , diag := range cpy {
21+ severity := hcl .DiagError
22+ if diag .Severity == DiagnosticSeverityWarning {
23+ severity = hcl .DiagWarning
24+ }
25+
26+ * d = append (* d , & hcl.Diagnostic {
27+ Severity : severity ,
28+ Summary : diag .Summary ,
29+ Detail : diag .Detail ,
30+ })
31+ }
32+ return nil
33+ }
34+
1335func (d Diagnostics ) MarshalJSON () ([]byte , error ) {
1436 cpy := make ([]FriendlyDiagnostic , 0 , len (d ))
1537 for _ , diag := range d {
Original file line number Diff line number Diff line change 1+ package types_test
2+
3+ import (
4+ "encoding/json"
5+ "testing"
6+
7+ "github.com/hashicorp/hcl/v2"
8+ "github.com/stretchr/testify/require"
9+
10+ "github.com/coder/preview/types"
11+ )
12+
13+ func TestDiagnosticsJSON (t * testing.T ) {
14+
15+ diags := types.Diagnostics {
16+ {
17+ Severity : hcl .DiagWarning ,
18+ Summary : "Some summary" ,
19+ Detail : "Some detail" ,
20+ },
21+ {
22+ Severity : hcl .DiagError ,
23+ Summary : "Some summary" ,
24+ Detail : "Some detail" ,
25+ },
26+ }
27+
28+ data , err := json .Marshal (diags )
29+ require .NoError (t , err , "marshal" )
30+
31+ var newDiags types.Diagnostics
32+ err = json .Unmarshal (data , & newDiags )
33+ require .NoError (t , err , "unmarshal" )
34+
35+ require .Equal (t , diags , newDiags )
36+ }
You can’t perform that action at this time.
0 commit comments