Skip to content

feat: add support to json marshal the module output #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"

"github.com/coder/preview/hclext"
"github.com/coder/preview/types"
Expand All @@ -31,14 +32,35 @@ type Output struct {
// ModuleOutput is any 'output' values from the terraform files. This has 0
// effect on the parameters, tags, etc. It can be helpful for debugging, as it
// allows exporting some terraform values to the caller to review.
ModuleOutput cty.Value
//
// JSON marshalling is handled in the custom methods.
ModuleOutput cty.Value `json:"-"`

Parameters []types.Parameter
WorkspaceTags types.TagBlocks
Parameters []types.Parameter `json:"parameters"`
WorkspaceTags types.TagBlocks `json:"workspace_tags"`
// Files is included for printing diagnostics.
// TODO: Is the memory impact of this too much? Should we render diagnostic source code
// into the diagnostics up front? and remove this?
Files map[string]*hcl.File
// They can be marshalled, but not unmarshalled. This is a limitation
// of the HCL library.
Files map[string]*hcl.File `json:"-"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is setting the key to "-" prevent it from being unmarshalled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly 👍

}

// MarshalJSON includes the ModuleOutput and files in the JSON output. Output
// should never be unmarshalled. Marshalling to JSON is strictly useful for
// debugging information.
func (o Output) MarshalJSON() ([]byte, error) {
// Do not make this a fatal error, as it is supplementary information.
modOutput, _ := ctyjson.Marshal(o.ModuleOutput, o.ModuleOutput.Type())

type Alias Output
return json.Marshal(&struct {
ModuleOutput json.RawMessage `json:"module_output"`
Files map[string]*hcl.File `json:"files"`
Alias
}{
ModuleOutput: modOutput,
Files: o.Files,
Alias: (Alias)(o),
})
}

func Preview(ctx context.Context, input Input, dir fs.FS) (output *Output, diagnostics hcl.Diagnostics) {
Expand Down
Loading