Skip to content
Prev Previous commit
Next Next commit
chore: clean up type gen script
  • Loading branch information
brettkolodny committed May 31, 2025
commit 2059bcf77e5e5924f19ac336396336f209500d03
5 changes: 1 addition & 4 deletions preview/apitypes/apitypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ type PreviewOutput struct {
ParserLogs string `json:"parser_logs,omitempty"`
}

type NullHCLString struct {
Value string `json:"value"`
Valid bool `json:"valid"`
}
type NullHCLString = types.NullHCLString
17 changes: 8 additions & 9 deletions preview/scripts/types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func main() {
}

referencePackages := map[string]string{
"github.com/coder/preview": "Output",
"github.com/coder/preview/types": "Paramater",
"github.com/hashicorp/hcl/v2": "File",
"github.com/hashicorp/hcl/v2/hclwrite": "Expression",
"github.com/zclconf/go-cty/cty": "Value",
"github.com/aquasecurity/trivy/pkg/iac/terraform": "Block",
"github.com/coder/terraform-provider-coder/v2/provider": "ParameterFormType",
"github.com/zclconf/go-cty/cty/function": "Function"}
"github.com/coder/preview": "",
"github.com/coder/preview/types": "",
"github.com/hashicorp/hcl/v2": "",
"github.com/hashicorp/hcl/v2/hclwrite": "",
"github.com/zclconf/go-cty/cty": "",
"github.com/aquasecurity/trivy/pkg/iac/terraform": "",
"github.com/coder/terraform-provider-coder/v2/provider": "",
"github.com/zclconf/go-cty/cty/function": ""}

for pkg, prefix := range referencePackages {
err = gen.IncludeReference(pkg, prefix)
Expand Down Expand Up @@ -60,7 +60,6 @@ func typeMappings(gen *guts.GoParser) error {
gen.IncludeCustomDeclaration(config.StandardMappings())

gen.IncludeCustomDeclaration(map[string]guts.TypeOverride{
"github.com/coder/coder/v2/codersdk.NullTime": config.OverrideNullable(config.OverrideLiteral(bindings.KeywordString)),
// opt.Bool can return 'null' if unset
"tailscale.com/types/opt.Bool": config.OverrideNullable(config.OverrideLiteral(bindings.KeywordBoolean)),
"github.com/hashicorp/hcl/v2.Diagnostic": func() bindings.ExpressionType {
Expand Down
34 changes: 26 additions & 8 deletions src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { useStore } from "@/store";
import { cn } from "@/utils/cn";
import { ActivityIcon, ExternalLinkIcon, LoaderIcon } from "lucide-react";
import { type FC, useEffect, useState } from "react";
import * as v from "valibot";

// const OutputSchema =
import type { PreviewOutput } from "@/gen/types";

export const Preview: FC = () => {
const $wasmState = useStore((state) => state.wasmState);
Expand All @@ -16,7 +14,7 @@ export const Preview: FC = () => {

const [debouncedCode, isDebouncing] = useDebouncedValue($code, 1000);

const [output, setOutput] = useState<string | null>(() => null);
const [output, setOutput] = useState<PreviewOutput | null>(() => null);

useEffect(() => {
if (!window.go_preview) {
Expand All @@ -25,13 +23,14 @@ export const Preview: FC = () => {

const getOutput = async () => {
try {
const output = await window.go_preview?.({
const rawOutput = await window.go_preview?.({
"main.tf": debouncedCode,
});

if (output === undefined) {
if (rawOutput === undefined) {
console.error("Something went wrong");
} else {
const output = JSON.parse(rawOutput) as PreviewOutput;
setOutput(() => output);
}
} catch (e) {
Expand Down Expand Up @@ -67,12 +66,31 @@ export const Preview: FC = () => {
</div>

<div
className="flex h-full w-full items-center justify-center overflow-x-clip rounded-xl border p-4"
className={cn(
"flex h-full w-full items-center justify-center overflow-x-clip rounded-xl border p-4",
output && "block overflow-y-scroll",
)}
style={{
opacity: isDebouncing && $wasmState === "loaded" ? 0.5 : 1,
}}
>
{output ? output : <PreviewEmptyState />}
{output ? (
<div className="flex flex-col gap-4">
<p className=" w-fit break-all text-content-primary">
{JSON.stringify(output.output?.Parameters, null, 2)}
</p>

<p className=" w-fit break-all text-content-primary">
{JSON.stringify(output.diags, null, 2)}
</p>

<p className=" w-fit break-all text-content-primary">
{output.parser_logs}
</p>
</div>
) : (
<PreviewEmptyState />
)}
</div>
</div>

Expand Down
78 changes: 39 additions & 39 deletions src/gen/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Code generated by 'guts'. DO NOT EDIT.

// From terraform/block.go
export interface BlockBlock {
export interface Block {
}

// From types/diagnostics.go
export type Diagnostics = (unknown | null)[];

// From hcl/structure.go
export interface FileFile {
export interface File {
Body: unknown;
Bytes: string;
// empty interface{} type, falling back to unknown
Expand All @@ -19,97 +22,94 @@ export interface NullHCLString {
}

// From preview/preview.go
export interface OutputOutput {
ModuleOutput: ValueValue;
Parameters: ParamaterParameter[];
WorkspaceTags: ParamaterTagBlocks;
Files: Record<string, FileFile | null> | null;
export interface Output {
ModuleOutput: Value;
Parameters: Parameter[];
WorkspaceTags: TagBlocks;
Files: Record<string, File | null> | null;
}

// From types/diagnostics.go
export type ParamaterDiagnostics = (unknown | null)[];

// From types/parameter.go
export interface ParamaterParameter extends ParamaterParameterData {
export interface Parameter extends ParameterData {
value: NullHCLString;
diagnostics: ParamaterDiagnostics;
diagnostics: Diagnostics;
}

// From types/parameter.go
export interface ParamaterParameterData {
export interface ParameterData {
name: string;
display_name: string;
description: string;
type: ParamaterParameterType;
form_type: ParameterFormTypeParameterFormType;
styling: ParamaterParameterStyling;
type: ParameterType;
form_type: ParameterFormType;
styling: ParameterStyling;
mutable: boolean;
default_value: NullHCLString;
icon: string;
options: (ParamaterParameterOption | null)[];
validations: (ParamaterParameterValidation | null)[];
options: (ParameterOption | null)[];
validations: (ParameterValidation | null)[];
required: boolean;
order: number;
ephemeral: boolean;
}

// From provider/formtype.go
export type ParameterFormType = string;

// From types/parameter.go
export interface ParamaterParameterOption {
export interface ParameterOption {
name: string;
description: string;
value: NullHCLString;
icon: string;
}

// From types/parameter.go
export interface ParamaterParameterStyling {
export interface ParameterStyling {
placeholder?: string | null;
disabled?: boolean | null;
label?: string | null;
}

// From types/enum.go
export type ParamaterParameterType = string;
export type ParameterType = string;

// From types/parameter.go
export interface ParamaterParameterValidation {
export interface ParameterValidation {
validation_error: string;
validation_regex: string | null;
validation_min: number | null;
validation_max: number | null;
validation_monotonic: string | null;
}

// From apitypes/apitypes.go
export interface PreviewOutput {
output: Output | null;
diags: Diagnostics;
parser_logs?: string;
}

// From types/tags.go
export interface ParamaterTag {
export interface Tag {
Key: NullHCLString;
Value: NullHCLString;
}

// From types/tags.go
export interface ParamaterTagBlock {
Tags: ParamaterTags;
Block: BlockBlock | null;
export interface TagBlock {
Tags: Tags;
Block: Block | null;
}

// From types/tags.go
export type ParamaterTagBlocks = ParamaterTagBlock[];
export type TagBlocks = TagBlock[];

// From types/tags.go
export type ParamaterTags = ParamaterTag[];

// From provider/formtype.go
export type ParameterFormTypeParameterFormType = string;

// From apitypes/apitypes.go
export interface PreviewOutput {
output: OutputOutput | null;
diags: ParamaterDiagnostics;
parser_logs?: string;
}
export type Tags = Tag[];

// From cty/value.go
export interface ValueValue {
export interface Value {
}