Skip to content

Commit 8b01ab1

Browse files
committed
chore: define types explictly
1 parent f05920f commit 8b01ab1

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

preview/apitypes/apitypes.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ package apitypes
33
import (
44
"time"
55

6-
"github.com/coder/preview"
6+
"github.com/hashicorp/hcl/v2"
7+
78
"github.com/coder/preview/types"
89
)
910

1011
type PreviewOutput struct {
11-
Output *preview.Output `json:"output"`
12+
Output *Output `json:"output"`
1213
Diags types.Diagnostics `json:"diags"`
1314
// ParserLogs are trivy logs that occur during parsing the
1415
// Terraform files. This is useful for debugging issues with the
1516
// invalid terraform syntax.
1617
ParserLogs []ParserLog `json:"parser_logs,omitempty"`
1718
}
1819

20+
type Output struct {
21+
Parameters []ParameterWithSource `json:"parameters"`
22+
Files map[string]*hcl.File `json:"files"`
23+
}
24+
1925
type ParserLog struct {
2026
Time time.Time `json:"time"`
2127
Level string `json:"level"`
@@ -28,3 +34,24 @@ type ParserLog struct {
2834
type NullHCLString = types.NullHCLString
2935

3036
type FriendlyDiagnostic = types.FriendlyDiagnostic
37+
38+
type ParameterWithSource struct {
39+
types.Parameter
40+
TypeRange hcl.Range `json:"type_range"`
41+
}
42+
43+
func WithSource(p []types.Parameter) []ParameterWithSource {
44+
result := make([]ParameterWithSource, 0, len(p))
45+
for _, param := range p {
46+
src := ParameterWithSource{
47+
Parameter: param,
48+
}
49+
50+
if param.Source != nil {
51+
src.TypeRange = param.Source.HCLBlock().TypeRange
52+
}
53+
54+
result = append(result, src)
55+
}
56+
return result
57+
}

preview/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
7272
}, tf)
7373

7474
data, _ := json.Marshal(apitypes.PreviewOutput{
75-
Output: pOutput,
75+
Output: &apitypes.Output{
76+
Parameters: apitypes.WithSource(pOutput.Parameters),
77+
Files: pOutput.Files,
78+
},
7679
Diags: types.Diagnostics(diags),
7780
ParserLogs: l.entries,
7881
})

public/build/preview.wasm

24.9 KB
Binary file not shown.

src/gen/types.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
// Code generated by 'guts'. DO NOT EDIT.
22

3-
// From terraform/block.go
4-
export interface Block {
5-
}
6-
73
// From types/diagnostics.go
84
export interface DiagnosticExtra {
95
code: string;
@@ -39,12 +35,10 @@ export interface NullHCLString {
3935
valid: boolean;
4036
}
4137

42-
// From preview/preview.go
38+
// From apitypes/apitypes.go
4339
export interface Output {
44-
ModuleOutput: Value;
45-
Parameters: Parameter[];
46-
WorkspaceTags: TagBlocks;
47-
Files: Record<string, File | null> | null;
40+
parameters: ParameterWithSource[];
41+
files: Record<string, File | null> | null;
4842
}
4943

5044
// From types/parameter.go
@@ -101,6 +95,11 @@ export interface ParameterValidation {
10195
validation_monotonic: string | null;
10296
}
10397

98+
// From apitypes/apitypes.go
99+
export interface ParameterWithSource extends Parameter {
100+
type_range: Range;
101+
}
102+
104103
// From apitypes/apitypes.go
105104
export interface ParserLog {
106105
time: string;
@@ -111,33 +110,25 @@ export interface ParserLog {
111110
err: string;
112111
}
113112

113+
// From hcl/pos.go
114+
export interface Pos {
115+
Line: number;
116+
Column: number;
117+
Byte: number;
118+
}
119+
114120
// From apitypes/apitypes.go
115121
export interface PreviewOutput {
116122
output: Output | null;
117123
diags: Diagnostics;
118124
parser_logs?: ParserLog[];
119125
}
120126

121-
// From types/tags.go
122-
export interface Tag {
123-
Key: NullHCLString;
124-
Value: NullHCLString;
125-
}
126-
127-
// From types/tags.go
128-
export interface TagBlock {
129-
Tags: Tags;
130-
Block: Block | null;
131-
}
132-
133-
// From types/tags.go
134-
export type TagBlocks = TagBlock[];
135-
136-
// From types/tags.go
137-
export type Tags = Tag[];
138-
139-
// From cty/value.go
140-
export interface Value {
127+
// From hcl/pos.go
128+
export interface Range {
129+
Filename: string;
130+
Start: Pos;
131+
End: Pos;
141132
}
142133

143134

0 commit comments

Comments
 (0)