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
Merged
Show file tree
Hide file tree
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
Next Next commit
feat: Switch packages for typescript generation code
  • Loading branch information
Emyrk committed Apr 27, 2022
commit ee71acdefb508c81efb8ca007279581d3c35619f
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ require (
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/tools v0.1.10
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f
google.golang.org/api v0.75.0
google.golang.org/protobuf v1.28.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,7 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
24 changes: 21 additions & 3 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package main

import (
"context"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"sort"
"strings"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"

"golang.org/x/xerrors"
)

Expand All @@ -19,9 +22,23 @@ const (
)

func main() {
err := run()
ctx := context.Background()
log := slog.Make(sloghuman.Sink(os.Stderr))
g := Generator{log: log}
err := g.parsePackage(ctx, "./codersdk")
if err != nil {
panic(err)
}
//err = g.generate("ProvisionerJob")
err = g.generateAll()
if err != nil {
panic(err)
}
return

err = run()
if err != nil {
log.Fatal(err)
log.Fatal(ctx, err.Error())
}
}

Expand Down Expand Up @@ -180,6 +197,7 @@ func getIdent(e ast.Expr) (*ast.Ident, string, error) {
}

func toTsType(fieldType string) string {
fmt.Println(fieldType)
switch fieldType {
case "bool":
return "boolean"
Expand Down
162 changes: 162 additions & 0 deletions scripts/apitypings/main2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package main

import (
"context"
"fmt"
"go/types"
"reflect"
"strings"

"golang.org/x/tools/go/packages"
"golang.org/x/xerrors"

"cdr.dev/slog"
)

type Generator struct {
pkg *packages.Package // Package we are scanning.
log slog.Logger
}

// parsePackage takes a list of patterns such as a directory, and parses them.
// All parsed packages will accumulate "foundTypes".
func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedName | packages.NeedTypesInfo |
packages.NeedTypesSizes | packages.NeedSyntax,
Tests: false,
Context: ctx,
}

pkgs, err := packages.Load(cfg, patterns...)
if err != nil {
return xerrors.Errorf("load package: %w", err)
}

if len(pkgs) != 1 {
return xerrors.Errorf("expected 1 package, found %d", len(pkgs))
}

g.pkg = pkgs[0]
return nil
}

// generateAll will generate for all types found in the pkg
func (g *Generator) generateAll() error {
for _, n := range g.pkg.Types.Scope().Names() {
err := g.generate(n)
if err != nil {
return xerrors.Errorf("generate %q: %w", n, err)
}
}
return nil
}

// generate generates the typescript for a singular Go type.
func (g *Generator) generate(typeName string) error {
obj := g.pkg.Types.Scope().Lookup(typeName)
if obj == nil || obj.Type() == nil {
return xerrors.Errorf("pkg is missing type %q", typeName)
}

st, ok := obj.Type().Underlying().(*types.Struct)
if !ok {
return nil
//return xerrors.Errorf("only generate for structs, found %q", obj.Type().String())
}

return g.buildStruct(obj, st)
}

// buildStruct just prints the typescript def for a type.
// TODO: Write to a buffer instead
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) error {
var s strings.Builder
s.WriteString("export interface " + obj.Name() + "{\n")
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
tag := reflect.StructTag(st.Tag(i))
jsonName := tag.Get("json")
arr := strings.Split(jsonName, ",")
jsonName = arr[0]
if jsonName == "" {
jsonName = field.Name()
}

ts, err := g.typescriptType(field.Type())
if err != nil {
return xerrors.Errorf("typescript type: %w", err)
}
s.WriteString(fmt.Sprintf("\treadonly %s: %s\n", jsonName, ts))
}
s.WriteString("}")
fmt.Println(s.String())
return nil
}

// typescriptType this function returns a typescript type for a given
// golang type.
// Eg:
// []byte returns "string"
func (g *Generator) typescriptType(ty types.Type) (string, error) {
switch ty.(type) {
case *types.Basic:
bs := ty.(*types.Basic)
// All basic literals (string, bool, int, etc).
// TODO: Actually ensure the golang names are ok, otherwise,
// we want to put another switch to capture these types
// and rename to typescript.
return bs.Name(), nil
case *types.Struct:
// TODO: This kinda sucks right now. It just dumps the struct def
return ty.String(), nil
case *types.Map:
// TODO: Typescript dictionary??? Object?
return "map", nil
case *types.Slice, *types.Array:
type hasElem interface {
Elem() types.Type
}

arr := ty.(hasElem)
// All byte arrays should be strings in typescript?
if arr.Elem().String() == "byte" {
return "string", nil
}

// Array of underlying type.
underlying, err := g.typescriptType(arr.Elem())
if err != nil {
return "", xerrors.Errorf("array: %w", err)
}
return underlying + "[]", nil
case *types.Named:
// Named is a named type like
// type EnumExample string
// Use the underlying type
n := ty.(*types.Named)
name := n.Obj().Name()
// If we have the type, just put the name because it will be defined
// elsewhere in the typescript gen.
if obj := g.pkg.Types.Scope().Lookup(n.String()); obj != nil {
return name, nil
}

// If it's a struct, just use the name for now.
if _, ok := ty.Underlying().(*types.Struct); ok {
return name, nil
}

// Defer to the underlying type.
return g.typescriptType(ty.Underlying())
case *types.Pointer:
// Dereference pointers.
// TODO: Nullable fields?
pt := ty.(*types.Pointer)
return g.typescriptType(pt.Elem())
}

// These are all the other types we need to support.
// time.Time, uuid, etc.
return "", xerrors.Errorf("unknown type: %s", ty.String())
}