Skip to content

feat: Switch packages for typescript generation code #1196

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 18 commits into from
Apr 28, 2022
Prev Previous commit
Next Next commit
Go linting
  • Loading branch information
Emyrk committed Apr 28, 2022
commit 2379c21c7afe8f06045c2a199ab957f745f45f90
50 changes: 23 additions & 27 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
}

// Just cat the output to a file to capture it
fmt.Println(codeBlocks.String())
_, _ = fmt.Println(codeBlocks.String())
}

// TypescriptTypes holds all the code blocks created.
Expand All @@ -46,7 +46,7 @@ type TypescriptTypes struct {
// String just combines all the codeblocks.
func (t TypescriptTypes) String() string {
var s strings.Builder
s.WriteString("// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.\n\n")
_, _ = s.WriteString("// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.\n\n")

sortedTypes := make([]string, 0, len(t.Types))
sortedEnums := make([]string, 0, len(t.Types))
Expand All @@ -63,14 +63,14 @@ func (t TypescriptTypes) String() string {

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

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

return strings.TrimRight(s.String(), "\n")
Expand Down Expand Up @@ -147,7 +147,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
ignoredTypes[strings.TrimSpace(s)] = struct{}{}
}
}

}
}
}
Expand All @@ -164,7 +163,7 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
continue
}

switch obj.(type) {
switch obj := obj.(type) {
// All named types are type declarations
case *types.TypeName:
named, ok := obj.Type().(*types.Named)
Expand All @@ -175,7 +174,7 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
case *types.Struct:
// type <Name> struct
// Structs are obvious.
st := obj.Type().Underlying().(*types.Struct)
st, _ := obj.Type().Underlying().(*types.Struct)
codeBlock, err := g.buildStruct(obj, st)
if err != nil {
return nil, xerrors.Errorf("generate %q: %w", obj.Name())
Expand All @@ -188,14 +187,11 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
}
case *types.Var:
// TODO: Are any enums var declarations? This is also codersdk.Me.
v := obj.(*types.Var)
var _ = v
case *types.Const:
c := obj.(*types.Const)
// We only care about named constant types, since they are enums
if named, ok := c.Type().(*types.Named); ok {
if named, ok := obj.Type().(*types.Named); ok {
name := named.Obj().Name()
enumConsts[name] = append(enumConsts[name], c)
enumConsts[name] = append(enumConsts[name], obj)
}
case *types.Func:
// Noop
Expand All @@ -214,8 +210,8 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
values = append(values, elem.Val().String())
}
var s strings.Builder
s.WriteString(g.posLine(v))
s.WriteString(fmt.Sprintf("export type %s = %s\n",
_, _ = s.WriteString(g.posLine(v))
_, _ = s.WriteString(fmt.Sprintf("export type %s = %s\n",
name, strings.Join(values, " | "),
))

Expand All @@ -240,9 +236,9 @@ 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) {
var s strings.Builder
s.WriteString(g.posLine(obj))
_, _ = s.WriteString(g.posLine(obj))

s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
_, _ = s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
// For each field in the struct, we print 1 line of the typescript interface
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
Expand Down Expand Up @@ -273,15 +269,15 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
}

if tsType.Comment != "" {
s.WriteString(fmt.Sprintf("%s// %s\n", indent, tsType.Comment))
_, _ = s.WriteString(fmt.Sprintf("%s// %s\n", indent, tsType.Comment))
}
optional := ""
if tsType.Optional {
optional = "?"
}
s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
}
s.WriteString("}\n")
_, _ = s.WriteString("}\n")
return s.String(), nil
}

Expand All @@ -297,9 +293,9 @@ type TypescriptType struct {
// Eg:
// []byte returns "string"
func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
switch ty.(type) {
switch ty := ty.(type) {
case *types.Basic:
bs := ty.(*types.Basic)
bs := ty
// All basic literals (string, bool, int, etc).
switch {
case bs.Info()&types.IsNumeric > 0:
Expand All @@ -323,7 +319,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{ValueType: "any", Comment: "Embedded struct, please fix by naming it"}, nil
case *types.Map:
// map[string][string] -> Record<string, string>
m := ty.(*types.Map)
m := ty
keyType, err := g.typescriptType(m.Key())
if err != nil {
return TypescriptType{}, xerrors.Errorf("map key: %w", err)
Expand All @@ -342,7 +338,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
Elem() types.Type
}

arr := ty.(hasElem)
arr, _ := ty.(hasElem)
switch {
// When type checking here, just use the string. You can cast it
// to a types.Basic and get the kind if you want too :shrug:
Expand All @@ -359,7 +355,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{ValueType: underlying.ValueType + "[]", Comment: underlying.Comment}, nil
}
case *types.Named:
n := ty.(*types.Named)
n := ty
// First see if the type is defined elsewhere. If it is, we can just
// put the name as it will be defined in the typescript codeblock
// we generate.
Expand Down Expand Up @@ -397,7 +393,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return ts, nil
case *types.Pointer:
// Dereference pointers.
pt := ty.(*types.Pointer)
pt := ty
resp, err := g.typescriptType(pt.Elem())
if err != nil {
return TypescriptType{}, xerrors.Errorf("pointer: %w", err)
Expand Down