Skip to content

Commit 179a219

Browse files
committed
chore: define types explictly
1 parent 1b28509 commit 179a219

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

preview/apitypes/apitypes.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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

@@ -33,14 +34,19 @@ const (
3334
)
3435

3536
type PreviewOutput struct {
36-
Output *preview.Output `json:"output"`
37+
Output *Output `json:"output"`
3738
Diags types.Diagnostics `json:"diags"`
3839
// ParserLogs are trivy logs that occur during parsing the
3940
// Terraform files. This is useful for debugging issues with the
4041
// invalid terraform syntax.
4142
ParserLogs []ParserLog `json:"parser_logs,omitempty"`
4243
}
4344

45+
type Output struct {
46+
Parameters []ParameterWithSource `json:"parameters"`
47+
Files map[string]*hcl.File `json:"files"`
48+
}
49+
4450
type ParserLog struct {
4551
Time time.Time `json:"time"`
4652
Level string `json:"level"`
@@ -53,3 +59,24 @@ type ParserLog struct {
5359
type NullHCLString = types.NullHCLString
5460

5561
type FriendlyDiagnostic = types.FriendlyDiagnostic
62+
63+
type ParameterWithSource struct {
64+
types.Parameter
65+
TypeRange hcl.Range `json:"type_range"`
66+
}
67+
68+
func WithSource(p []types.Parameter) []ParameterWithSource {
69+
result := make([]ParameterWithSource, 0, len(p))
70+
for _, param := range p {
71+
src := ParameterWithSource{
72+
Parameter: param,
73+
}
74+
75+
if param.Source != nil {
76+
src.TypeRange = param.Source.HCLBlock().TypeRange
77+
}
78+
79+
result = append(result, src)
80+
}
81+
return result
82+
}

preview/main.go

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

8888
data, _ := json.Marshal(apitypes.PreviewOutput{
89-
Output: pOutput,
89+
Output: &apitypes.Output{
90+
Parameters: apitypes.WithSource(pOutput.Parameters),
91+
Files: pOutput.Files,
92+
},
9093
Diags: types.Diagnostics(diags),
9194
ParserLogs: l.entries,
9295
})

0 commit comments

Comments
 (0)