Skip to content

Commit 7b349d7

Browse files
committed
chore: fixup json types for diagnostics
1 parent 7b90650 commit 7b349d7

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

extract/parameter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
7878
}
7979

8080
// Diagnostics are scoped to the parameter
81-
p.Diagnostics = diags
81+
p.Diagnostics = types.Diagnostics(diags)
8282

8383
return &p, nil
8484
}

types/diagnostics.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package types
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/hashicorp/hcl/v2"
7+
)
8+
9+
// Diagnostics is a JSON friendly form of hcl.Diagnostics.
10+
// Data is lost when doing a json marshal.
11+
type Diagnostics hcl.Diagnostics
12+
13+
func (d Diagnostics) MarshalJSON() ([]byte, error) {
14+
cpy := make([]FriendlyDiagnostic, 0, len(d))
15+
for _, diag := range d {
16+
severity := DiagnosticSeverityError
17+
if diag.Severity == hcl.DiagWarning {
18+
severity = DiagnosticSeverityWarning
19+
}
20+
21+
cpy = append(cpy, FriendlyDiagnostic{
22+
Severity: severity,
23+
Summary: diag.Summary,
24+
Detail: diag.Detail,
25+
})
26+
}
27+
return json.Marshal(cpy)
28+
}
29+
30+
type DiagnosticSeverityString string
31+
32+
const (
33+
DiagnosticSeverityError DiagnosticSeverityString = "error"
34+
DiagnosticSeverityWarning DiagnosticSeverityString = "warning"
35+
)
36+
37+
type FriendlyDiagnostic struct {
38+
Severity DiagnosticSeverityString `json:"severity"`
39+
Summary string `json:"summary"`
40+
Detail string `json:"detail"`
41+
}

types/parameter.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package types
22

33
import (
4-
"crypto/sha256"
5-
"encoding/json"
64
"fmt"
75
"slices"
86
"strings"
97

10-
"github.com/hashicorp/hcl/v2"
118
"github.com/zclconf/go-cty/cty"
129
)
1310

@@ -38,7 +35,7 @@ type Parameter struct {
3835

3936
// Diagnostics is used to store any errors that occur during parsing
4037
// of the parameter.
41-
Diagnostics hcl.Diagnostics `json:"-" hcl:"-"`
38+
Diagnostics Diagnostics `json:"diagnostics"`
4239
}
4340

4441
type RichParameter struct {
@@ -73,17 +70,6 @@ type ParameterOption struct {
7370
Icon string `json:"icon"`
7471
}
7572

76-
// Hash can be used to compare two RichParameter objects at a glance.
77-
func (r *RichParameter) Hash() ([32]byte, error) {
78-
// Option order matters, so just json marshal the whole thing.
79-
data, err := json.Marshal(r)
80-
if err != nil {
81-
return [32]byte{}, fmt.Errorf("marshal: %w", err)
82-
}
83-
84-
return sha256.Sum256(data), nil
85-
}
86-
8773
// CtyType returns the cty.Type for the RichParameter.
8874
// A fixed set of types are supported.
8975
func (r *RichParameter) CtyType() (cty.Type, error) {

web/diagnostic.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

web/genweb/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"golang.org/x/xerrors"
1010

1111
"github.com/coder/guts"
12+
"github.com/coder/guts/bindings"
1213
"github.com/coder/guts/config"
1314
)
1415

@@ -85,7 +86,15 @@ func TsMutations(ts *guts.Typescript) {
8586
func TypeMappings(gen *guts.GoParser) error {
8687
gen.IncludeCustomDeclaration(config.StandardMappings())
8788

88-
gen.IncludeCustomDeclaration(map[string]guts.TypeOverride{})
89+
gen.IncludeCustomDeclaration(map[string]guts.TypeOverride{
90+
"github.com/hashicorp/hcl/v2.Diagnostic": func() bindings.ExpressionType {
91+
return bindings.Reference(bindings.Identifier{
92+
Name: "FriendlyDiagnostic",
93+
Package: nil,
94+
Prefix: "",
95+
})
96+
},
97+
})
8998

9099
err := gen.IncludeCustom(map[string]string{
91100
"github.com/coder/preview/types.HCLString": "string",

web/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Request struct {
1818

1919
type Response struct {
2020
ID int `json:"id"`
21-
Diagnostics Diagnostics `json:"diagnostics"`
21+
Diagnostics types.Diagnostics `json:"diagnostics"`
2222
Parameters []types.Parameter `json:"parameters"`
2323
// TODO: Workspace tags
2424
}
@@ -73,7 +73,7 @@ func (s *Session) preview(ctx context.Context, req *Request) Response {
7373

7474
r := Response{
7575
ID: req.ID,
76-
Diagnostics: FromHCLDiagnostics(diags),
76+
Diagnostics: types.Diagnostics(diags),
7777
}
7878
if output == nil {
7979
return r

web/typescripttypes/preview.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
// Code generated by 'guts'. DO NOT EDIT.
22

3-
// From web/diagnostic.go
4-
export interface Diagnostic {
5-
readonly severity: DiagnosticSeverity;
6-
readonly summary: string;
7-
readonly detail: string;
8-
}
3+
// From types/diagnostics.go
4+
export type DiagnosticSeverityString = "error" | "warning";
95

10-
// From web/diagnostic.go
11-
export type DiagnosticSeverity = "error" | "warning";
6+
export const DiagnosticSeverityStrings: DiagnosticSeverityString[] = ["error", "warning"];
127

13-
export const DiagnosticSeveritys: DiagnosticSeverity[] = ["error", "warning"];
8+
// From types/diagnostics.go
9+
export type Diagnostics = readonly (FriendlyDiagnostic)[];
1410

15-
// From web/diagnostic.go
16-
export type Diagnostics = readonly Diagnostic[];
11+
// From types/diagnostics.go
12+
export interface FriendlyDiagnostic {
13+
readonly severity: DiagnosticSeverityString;
14+
readonly summary: string;
15+
readonly detail: string;
16+
}
1717

1818
// From types/parameter.go
1919
export interface Parameter extends RichParameter {
2020
readonly value: string;
21+
readonly diagnostics: Diagnostics;
2122
}
2223

2324
// From types/parameter.go
@@ -44,12 +45,13 @@ export interface ParameterValidation {
4445

4546
// From web/session.go
4647
export interface Request {
47-
readonly ID: number;
48-
readonly Inputs: Record<string, string>;
48+
readonly id: number;
49+
readonly inputs: Record<string, string>;
4950
}
5051

5152
// From web/session.go
5253
export interface Response {
54+
readonly id: number;
5355
readonly diagnostics: Diagnostics;
5456
readonly parameters: readonly Parameter[];
5557
}

0 commit comments

Comments
 (0)