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
Add 'DO NOT EDIT' to the top
  • Loading branch information
Emyrk committed Apr 28, 2022
commit 0d943f1b8cde184c21d081986b3121da1c64bad0
14 changes: 11 additions & 3 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"

"golang.org/x/tools/go/packages"
Expand All @@ -19,6 +20,7 @@ import (

const (
baseDir = "./codersdk"
indent = " "
)

// 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"

Expand All @@ -44,6 +46,8 @@ 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")

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

Expand All @@ -54,6 +58,9 @@ func (t TypescriptTypes) String() string {
sortedEnums = append(sortedEnums, k)
}

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

for _, k := range sortedTypes {
v := t.Types[k]
s.WriteString(v)
Expand All @@ -65,7 +72,8 @@ func (t TypescriptTypes) String() string {
s.WriteString(v)
s.WriteRune('\n')
}
return s.String()

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

// GenerateFromDirectory will return all the typescript code blocks for a directory
Expand Down Expand Up @@ -265,13 +273,13 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
}

if tsType.Comment != "" {
s.WriteString(fmt.Sprintf("\t// %s\n", tsType.Comment))
s.WriteString(fmt.Sprintf("%s// %s\n", indent, tsType.Comment))
}
optional := ""
if tsType.Optional {
optional = "?"
}
s.WriteString(fmt.Sprintf("\treadonly %s%s: %s\n", jsonName, optional, tsType.ValueType))
s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
}
s.WriteString("}\n")
return s.String(), nil
Expand Down
4 changes: 2 additions & 2 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Code generated by 'make coder/scripts/apitypings/main.go'. DO NOT EDIT.

// From codersdk/workspaceagents.go:35:6
export interface AWSInstanceIdentityToken {
readonly signature: string
Expand Down Expand Up @@ -420,5 +422,3 @@ export type UserStatus = "active" | "suspended"

// From codersdk/workspaceresources.go:15:6
export type WorkspaceAgentStatus = "connected" | "connecting" | "disconnected"