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
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
Handle reused types
  • Loading branch information
Emyrk committed Apr 28, 2022
commit 28c2811acaa86d47e239ac79ee147a0d5ebde83b
14 changes: 11 additions & 3 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
baseDir = "./codersdk"
)

// TODO: Handle httpapi.Response and other types
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should focus on making this script work perfectly for what we already plan to do in codersdk. If this is relevant with that in mind, you should probably make this more specific to the code that handles this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we need it. This is really to match Go types, but the general error/response type is not bad to implement once in TS for all endpoints that use it.

I don't want to run this code on another package, and type inferencing across packages is only possible to an extent.


I removed the TODO, but the basic thing is that external types are not currently handled. The only way to handle them better, is to run this code on those packages as well. Obviously then we want to allowlist types, vs the current deny list approach.

Here is the example of external types:
http://github.com/coder/coder/blob/3865f36c4bf8c2ddfc1c9081cf7bbf1a881675f6/codersdk/templateversions.go#L27-L30

They use database types that include enums. I would like to have a conversation about how to handle these, as we can handle them. Just a matter of "how"

func main() {
ctx := context.Background()
log := slog.Make(sloghuman.Sink(os.Stderr))
Expand Down Expand Up @@ -49,7 +50,6 @@ func (t TypescriptTypes) String() string {

for _, v := range t.Enums {
s.WriteString(v)
s.WriteRune('\n')
}
return s.String()
}
Expand Down Expand Up @@ -168,7 +168,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
s.WriteString(fmt.Sprintf("export type %s = %s\n",
name, strings.Join(values, " | "),
))
s.WriteRune('\n')

enumCodeBlocks[name] = s.String()
}
Expand Down Expand Up @@ -290,12 +289,21 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (string, str
// put the name as it will be defined in the typescript codeblock
// we generate.
name := n.Obj().Name()
if obj := g.pkg.Types.Scope().Lookup(n.String()); obj != nil && obj.Name() != name {
if obj := g.pkg.Types.Scope().Lookup(name); obj != nil {
// Sweet! Using other typescript types as fields. This could be an
// enum or another struct
return name, "", nil
}

switch n.String() {
case "net/url.URL":
return "string", "", nil
case "time.Time":
return "string", "is this ok for time?", nil
case "github.com/coder/coder/coderd/httpapi.Response":

}

// If it's a struct, just use the name of the struct type
if _, ok := n.Underlying().(*types.Struct); ok {
return name, "Unknown named type, this might not work", nil
Expand Down