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
Prev Previous commit
Next Next commit
Allow ignoring types for autogen
  • Loading branch information
Emyrk committed Apr 28, 2022
commit 071026deca83f558f9c5be3c70f90cda0105aa82
1 change: 1 addition & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func New(serverURL *url.URL) *Client {
}

// Client is an HTTP caller for methods to the Coder API.
// @typescript-ignore Client
type Client struct {
HTTPClient *http.Client
SessionToken string
Expand Down
40 changes: 35 additions & 5 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"strings"

"cdr.dev/slog/sloggers/sloghuman"
Expand Down Expand Up @@ -43,13 +44,26 @@ type TypescriptTypes struct {
// String just combines all the codeblocks. I store them in a map for unit testing purposes
func (t TypescriptTypes) String() string {
var s strings.Builder
for _, v := range t.Types {
sortedTypes := make([]string, 0, len(t.Types))
sortedEnums := make([]string, 0, len(t.Types))

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

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

for _, v := range t.Enums {
for _, k := range sortedEnums {
v := t.Enums[k]
s.WriteString(v)
s.WriteRune('\n')
}
return s.String()
}
Expand Down Expand Up @@ -104,22 +118,38 @@ func (g *Generator) parsePackage(ctx context.Context, patterns ...string) error
return nil
}

type Generated struct {
}

// generateAll will generate for all types found in the pkg
func (g *Generator) generateAll() (*TypescriptTypes, error) {
structs := make(map[string]string)
enums := make(map[string]types.Object)
constants := make(map[string][]*types.Const)

ignoredTypes := make(map[string]struct{})
ignoreRegex := regexp.MustCompile("@typescript-ignore:(?P<ignored_types>)")
for _, file := range g.pkg.Syntax {
for _, comment := range file.Comments {
matches := ignoreRegex.FindStringSubmatch(comment.Text())
ignored := ignoreRegex.SubexpIndex("ignored_types")
if len(matches) >= ignored && matches[ignored] != "" {
arr := strings.Split(matches[ignored], ",")
for _, s := range arr {
ignoredTypes[strings.TrimSpace(s)] = struct{}{}
}
}
}
}

for _, n := range g.pkg.Types.Scope().Names() {
obj := g.pkg.Types.Scope().Lookup(n)
if obj == nil || obj.Type() == nil {
// This would be weird, but it is if the package does not have the type def.
continue
}

if _, ok := ignoredTypes[obj.Name()]; ok {
continue
}

switch obj.(type) {
// All named types are type declarations
case *types.TypeName:
Expand Down