Skip to content
Prev Previous commit
Next Next commit
feat: generate types for dynamic paramaters
  • Loading branch information
brettkolodny committed May 31, 2025
commit 8a41403cd0ca175cc6aa7692bb527c3fdd0ac775
42 changes: 14 additions & 28 deletions preview/scripts/types/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/coder/guts"
"github.com/coder/guts/bindings"
"github.com/coder/guts/config"
"golang.org/x/xerrors"
)

func main() {
Expand All @@ -23,7 +22,7 @@ func main() {

referencePackages := map[string]string{
"github.com/coder/preview": "Output",
"github.com/coder/preview/types": "FriendlyDiagnostic",
"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",
Expand All @@ -38,11 +37,6 @@ func main() {
}
}

// err = gen.IncludeReference("github.com/coder/preview/types", "FriendlyDiagnostic")
// if err != nil {
// log.Fatalf("include reference package %q: %v", "github.com/coder/preview/types", err)
// }

err = typeMappings(gen)
if err != nil {
log.Fatalf("type mappings: %v", err)
Expand All @@ -55,7 +49,11 @@ func main() {
log.Fatalf("to typescript: %v", err)
}

fmt.Println(ts.Serialize())
output, err := ts.Serialize()
if err != nil {
log.Fatalf("serialize: %v", err)
}
_, _ = fmt.Println(output)
}

func typeMappings(gen *guts.GoParser) error {
Expand All @@ -65,10 +63,16 @@ func typeMappings(gen *guts.GoParser) error {
"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)),
// hcl diagnostics should be cast to `preview.FriendlyDiagnostic`
"github.com/hashicorp/hcl/v2.Diagnostic": func() bindings.ExpressionType {
return bindings.Reference(bindings.Identifier{
Name: "FriendlyDiagnostic",
Name: "unknown",
Package: nil,
Prefix: "",
})
},
"github.com/hashicorp/hcl/v2.Body": func() bindings.ExpressionType {
return bindings.Reference(bindings.Identifier{
Name: "unknown",
Package: nil,
Prefix: "",
})
Expand All @@ -82,23 +86,5 @@ func typeMappings(gen *guts.GoParser) error {
},
})

err := gen.IncludeCustom(map[string]string{
// Serpent fields should be converted to their primitive types
"github.com/coder/serpent.Regexp": "string",
"github.com/coder/serpent.StringArray": "string",
"github.com/coder/serpent.String": "string",
"github.com/coder/serpent.YAMLConfigPath": "string",
"github.com/coder/serpent.Strings": "[]string",
"github.com/coder/serpent.Int64": "int64",
"github.com/coder/serpent.Bool": "bool",
"github.com/coder/serpent.Duration": "int64",
"github.com/coder/serpent.URL": "string",
"github.com/coder/serpent.HostPort": "string",
"encoding/json.RawMessage": "map[string]string",
})
if err != nil {
return xerrors.Errorf("include custom: %w", err)
}

return nil
}
68 changes: 34 additions & 34 deletions src/gen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,73 @@ export interface BlockBlock {

// From hcl/structure.go
export interface FileFile {
Body: FileBody;
Body: unknown;
Bytes: string;
// empty interface{} type, falling back to unknown
Nav: unknown;
}

// From apitypes/apitypes.go
export interface NullHCLString {
value: string;
valid: boolean;
}

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

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

// From types/parameter.go
export interface FriendlyDiagnosticParameter extends FriendlyDiagnosticParameterData {
export interface ParamaterParameter extends ParamaterParameterData {
value: NullHCLString;
diagnostics: FriendlyDiagnosticDiagnostics;
diagnostics: ParamaterDiagnostics;
}

// From types/parameter.go
export interface FriendlyDiagnosticParameterData {
export interface ParamaterParameterData {
name: string;
display_name: string;
description: string;
type: FriendlyDiagnosticParameterType;
type: ParamaterParameterType;
form_type: ParameterFormTypeParameterFormType;
styling: FriendlyDiagnosticParameterStyling;
styling: ParamaterParameterStyling;
mutable: boolean;
default_value: NullHCLString;
icon: string;
options: (FriendlyDiagnosticParameterOption | null)[];
validations: (FriendlyDiagnosticParameterValidation | null)[];
options: (ParamaterParameterOption | null)[];
validations: (ParamaterParameterValidation | null)[];
required: boolean;
order: number;
ephemeral: boolean;
}

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

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

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

// From types/parameter.go
export interface FriendlyDiagnosticParameterValidation {
export interface ParamaterParameterValidation {
validation_error: string;
validation_regex: string | null;
validation_min: number | null;
Expand All @@ -67,49 +81,35 @@ export interface FriendlyDiagnosticParameterValidation {
}

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

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

// From types/tags.go
export type FriendlyDiagnosticTagBlocks = FriendlyDiagnosticTagBlock[];
export type ParamaterTagBlocks = ParamaterTagBlock[];

// From types/tags.go
export type FriendlyDiagnosticTags = FriendlyDiagnosticTag[];

// From apitypes/apitypes.go
export interface NullHCLString {
value: string;
valid: boolean;
}

// From preview/preview.go
export interface OutputOutput {
ModuleOutput: ValueValue;
Parameters: FriendlyDiagnosticParameter[];
WorkspaceTags: FriendlyDiagnosticTagBlocks;
Files: Record<string, FileFile | null> | null;
}
export type ParamaterTags = ParamaterTag[];

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

// From apitypes/apitypes.go
export interface PreviewOutput {
output: OutputOutput | null;
diags: FriendlyDiagnosticDiagnostics;
diags: ParamaterDiagnostics;
parser_logs?: string;
}

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

<nil>