Skip to content

chore: Add generics to typescript generator #4658

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

Closed
wants to merge 8 commits into from
Closed
Changes from 5 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
168 changes: 150 additions & 18 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"fmt"
"go/types"
Expand All @@ -10,6 +11,7 @@ import (
"regexp"
"sort"
"strings"
"text/template"

"github.com/fatih/structtag"
"golang.org/x/tools/go/packages"
Expand Down Expand Up @@ -39,8 +41,9 @@ func main() {
// TypescriptTypes holds all the code blocks created.
type TypescriptTypes struct {
// Each entry is the type name, and it's typescript code block.
Types map[string]string
Enums map[string]string
Types map[string]string
Enums map[string]string
Generics map[string]string
}

// String just combines all the codeblocks.
Expand All @@ -50,16 +53,21 @@ func (t TypescriptTypes) String() string {

sortedTypes := make([]string, 0, len(t.Types))
sortedEnums := make([]string, 0, len(t.Enums))
sortedGenerics := make([]string, 0, len(t.Generics))

for k := range t.Types {
sortedTypes = append(sortedTypes, k)
}
for k := range t.Enums {
sortedEnums = append(sortedEnums, k)
}
for k := range t.Generics {
sortedGenerics = append(sortedGenerics, k)
}

sort.Strings(sortedTypes)
sort.Strings(sortedEnums)
sort.Strings(sortedGenerics)

for _, k := range sortedTypes {
v := t.Types[k]
Expand All @@ -73,6 +81,12 @@ func (t TypescriptTypes) String() string {
_, _ = s.WriteRune('\n')
}

for _, k := range sortedGenerics {
v := t.Generics[k]
_, _ = s.WriteString(v)
_, _ = s.WriteRune('\n')
}

return strings.TrimRight(s.String(), "\n")
}

Expand Down Expand Up @@ -129,6 +143,7 @@ func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error
// generateAll will generate for all types found in the pkg
func (g *Generator) generateAll() (*TypescriptTypes, error) {
structs := make(map[string]string)
generics := make(map[string]string)
enums := make(map[string]types.Object)
enumConsts := make(map[string][]*types.Const)

Expand Down Expand Up @@ -170,12 +185,11 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
if !ok {
panic("all typename should be named types")
}
switch named.Underlying().(type) {
switch underNamed := named.Underlying().(type) {
case *types.Struct:
// type <Name> struct
// Structs are obvious.
st, _ := obj.Type().Underlying().(*types.Struct)
codeBlock, err := g.buildStruct(obj, st)
codeBlock, err := g.buildStruct(obj, underNamed)
if err != nil {
return nil, xerrors.Errorf("generate %q: %w", obj.Name(), err)
}
Expand Down Expand Up @@ -205,7 +219,35 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
str.WriteString(fmt.Sprintf("export type %s = %s\n", obj.Name(), ts.ValueType))
structs[obj.Name()] = str.String()
case *types.Array, *types.Slice:
// TODO: @emyrk if you need this, follow the same design as "*types.Map" case.
// TODO: @emyrk if you need this, follow the same design as "*types.Map" case.
case *types.Interface:
// Interfaces are used as generics. Non-generic interfaces are
// not supported.
if underNamed.NumEmbeddeds() == 1 {
union, ok := underNamed.EmbeddedType(0).(*types.Union)
if !ok {
// If the underlying is not a union, but has 1 type. It's
// just that one type.
union = types.NewUnion([]*types.Term{
// Set the tilde to true to support underlying.
// Doesn't actually affect our generation.
types.NewTerm(true, underNamed.EmbeddedType(0)),
})
}

block, err := g.buildUnion(obj, union)
if err != nil {
return nil, xerrors.Errorf("generate union %q: %w", obj.Name(), err)
}
generics[obj.Name()] = block
}
case *types.Signature:
// Ignore named functions.
default:
// If you hit this error, you added a new unsupported named type.
// The easiest way to solve this is add a new case above with
// your type and a TODO to implement it.
return nil, xerrors.Errorf("unsupported named type %q", underNamed.String())
}
case *types.Var:
// TODO: Are any enums var declarations? This is also codersdk.Me.
Expand Down Expand Up @@ -242,8 +284,9 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
}

return &TypescriptTypes{
Types: structs,
Enums: enumCodeBlocks,
Types: structs,
Enums: enumCodeBlocks,
Generics: generics,
}, nil
}

Expand All @@ -253,10 +296,61 @@ func (g *Generator) posLine(obj types.Object) string {
}

// buildStruct just prints the typescript def for a type.
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, error) {
func (g *Generator) buildUnion(obj types.Object, st *types.Union) (string, error) {
var s strings.Builder
_, _ = s.WriteString(g.posLine(obj))
_, _ = s.WriteString(fmt.Sprintf("export interface %s ", obj.Name()))

allTypes := make([]string, 0, st.Len())
var optional bool
for i := 0; i < st.Len(); i++ {
term := st.Term(i)
scriptType, err := g.typescriptType(term.Type())
if err != nil {
return "", xerrors.Errorf("union %q for %q failed to get type: %w", st.String(), obj.Name(), err)
}
allTypes = append(allTypes, scriptType.ValueType)
optional = optional || scriptType.Optional
}

if optional {
allTypes = append(allTypes, "null")
}

s.WriteString(fmt.Sprintf("export type %s = %s\n", obj.Name(), strings.Join(allTypes, " | ")))

return s.String(), nil
}

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 .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 := 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)
}

state.PosLine = g.posLine(obj)
state.Name = obj.Name()

// Handle named embedded structs in the codersdk package via extension.
var extends []string
Expand All @@ -272,10 +366,10 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
}
}
if len(extends) > 0 {
_, _ = s.WriteString(fmt.Sprintf("extends %s ", strings.Join(extends, ", ")))
state.Extends = strings.Join(extends, ", ")
}

_, _ = s.WriteString("{\n")
genericsUsed := make(map[string]string)
// For each field in the struct, we print 1 line of the typescript interface
for i := 0; i < st.NumFields(); i++ {
if extendedFields[i] {
Expand Down Expand Up @@ -335,21 +429,42 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
}

if tsType.AboveTypeLine != "" {
_, _ = s.WriteString(tsType.AboveTypeLine)
_, _ = s.WriteRune('\n')
state.AboveLine = tsType.AboveTypeLine
}
optional := ""
if jsonOptional || tsType.Optional {
optional = "?"
}
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
valueType := tsType.ValueType
if tsType.GenericMapping != "" {
valueType = tsType.GenericMapping
// Don't add a generic twice
if _, ok := genericsUsed[tsType.GenericMapping]; !ok {
// TODO: We should probably check that the generic mapping is
// not a different type. Like 'T' being referenced to 2 different
// constraints. I don't think this is possible though in valid
// go, so I'm going to ignore this for now.
state.Generics = append(state.Generics, fmt.Sprintf("%s extends %s", tsType.GenericMapping, tsType.ValueType))
}
genericsUsed[tsType.GenericMapping] = tsType.ValueType
}
state.Fields = append(state.Fields, fmt.Sprintf("%sreadonly %s%s: %s", indent, jsonName, optional, valueType))
}
_, _ = s.WriteString("}\n")
return s.String(), nil

data := bytes.NewBuffer(make([]byte, 0))
err = tpl.Execute(data, state)
if err != nil {
return "", xerrors.Errorf("execute struct template: %w", err)
}
return data.String(), nil
}

type TypescriptType struct {
ValueType string
// GenericMapping gives a unique character for mapping the value type
// to a generic. This is only useful if you can use generic syntax.
// This is optional, as the ValueType will have the correct constraints.
GenericMapping string
ValueType string
// AboveTypeLine lets you put whatever text you want above the typescript
// type line.
AboveTypeLine string
Expand Down Expand Up @@ -498,6 +613,23 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
AboveTypeLine: indentedComment("eslint-disable-next-line")}, nil
}
return TypescriptType{}, xerrors.New("only empty interface types are supported")
case *types.TypeParam:
_, ok := ty.Underlying().(*types.Interface)
if !ok {
// If it's not an interface, it is likely a usage of generics that
// we have not hit yet. Feel free to add support for it.
return TypescriptType{}, xerrors.New("type param must be an interface")
}

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

// These are all the other types we need to support.
Expand Down