Skip to content

chore: Add generics to typescript generator #4664

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 23 commits into from
Oct 20, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Support generics in apitypings
  • Loading branch information
Emyrk committed Oct 20, 2022
commit 6163ace2431ddb9ae0f9c468fed94b74a3d431f3
22 changes: 15 additions & 7 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,23 +326,26 @@ type structTemplateState struct {
PosLine string
Name string
Fields []string
Generics []string
Extends string
AboveLine string
}

const structTemplate = `{{ .PosLine -}}
{{ if .AboveLine }}{{ .AboveLine }}
{{ end }}export interface {{ .Name }}{{ if .Extends }} extends {{ .Extends }}{{ end }}{
{{- range .Fields }}
{{ . -}}
{{- end }}
{{ end }}export interface {{ .Name }}{{ if .Generics }}<{{ join .Generics ", " }}>{{ end }}{{ if .Extends }} extends {{ .Extends }}{{ end }} {
{{ join .Fields "\n"}}
}
`

// buildStruct just prints the typescript def for a type.
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, error) {
state := structTemplateState{}
tpl, err := template.New("struct").Parse(structTemplate)
tpl := template.New("struct")
tpl.Funcs(template.FuncMap{
"join": strings.Join,
})
tpl, err := tpl.Parse(structTemplate)
if err != nil {
return "", xerrors.Errorf("parse struct template: %w", err)
}
Expand Down Expand Up @@ -428,7 +431,12 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
if jsonOptional || tsType.Optional {
optional = "?"
}
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, tsType.ValueType))
valueType := tsType.ValueType
if tsType.GenericMapping != "" {
valueType = tsType.GenericMapping
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
}
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType))
}

data := bytes.NewBuffer(make([]byte, 0))
Expand Down Expand Up @@ -603,7 +611,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {

generic := ty.Constraint()
// This is kinda a hack, but we want just the end of the name.
name := strings.TrimSuffix("github.com/coder/coder/codersdk.", generic.String())
name := strings.TrimPrefix(generic.String(), "github.com/coder/coder/codersdk.")
return TypescriptType{
GenericMapping: ty.Obj().Name(),
ValueType: name,
Expand Down