Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: define types explictly
  • Loading branch information
Emyrk committed Jun 10, 2025
commit 179a219cf3ab91cda2707a5a05b530b9e09c2507
31 changes: 29 additions & 2 deletions preview/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package apitypes
import (
"time"

"github.com/coder/preview"
"github.com/hashicorp/hcl/v2"

"github.com/coder/preview/types"
)

Expand Down Expand Up @@ -33,14 +34,19 @@ const (
)

type PreviewOutput struct {
Output *preview.Output `json:"output"`
Output *Output `json:"output"`
Diags types.Diagnostics `json:"diags"`
// ParserLogs are trivy logs that occur during parsing the
// Terraform files. This is useful for debugging issues with the
// invalid terraform syntax.
ParserLogs []ParserLog `json:"parser_logs,omitempty"`
}

type Output struct {
Parameters []ParameterWithSource `json:"parameters"`
Files map[string]*hcl.File `json:"files"`
}

type ParserLog struct {
Time time.Time `json:"time"`
Level string `json:"level"`
Expand All @@ -53,3 +59,24 @@ type ParserLog struct {
type NullHCLString = types.NullHCLString

type FriendlyDiagnostic = types.FriendlyDiagnostic

type ParameterWithSource struct {
types.Parameter
TypeRange hcl.Range `json:"type_range"`
}

func WithSource(p []types.Parameter) []ParameterWithSource {
result := make([]ParameterWithSource, 0, len(p))
for _, param := range p {
src := ParameterWithSource{
Parameter: param,
}

if param.Source != nil {
src.TypeRange = param.Source.HCLBlock().TypeRange
}

result = append(result, src)
}
return result
}
5 changes: 4 additions & 1 deletion preview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func tfpreview(this js.Value, p []js.Value) (output any) {
}, tf)

data, _ := json.Marshal(apitypes.PreviewOutput{
Output: pOutput,
Output: &apitypes.Output{
Parameters: apitypes.WithSource(pOutput.Parameters),
Files: pOutput.Files,
},
Diags: types.Diagnostics(diags),
ParserLogs: l.entries,
})
Expand Down