Skip to content

Commit 28c2811

Browse files
committed
Handle reused types
1 parent b0b17d0 commit 28c2811

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

scripts/apitypings/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
baseDir = "./codersdk"
2222
)
2323

24+
// TODO: Handle httpapi.Response and other types
2425
func main() {
2526
ctx := context.Background()
2627
log := slog.Make(sloghuman.Sink(os.Stderr))
@@ -49,7 +50,6 @@ func (t TypescriptTypes) String() string {
4950

5051
for _, v := range t.Enums {
5152
s.WriteString(v)
52-
s.WriteRune('\n')
5353
}
5454
return s.String()
5555
}
@@ -168,7 +168,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
168168
s.WriteString(fmt.Sprintf("export type %s = %s\n",
169169
name, strings.Join(values, " | "),
170170
))
171-
s.WriteRune('\n')
172171

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

298+
switch n.String() {
299+
case "net/url.URL":
300+
return "string", "", nil
301+
case "time.Time":
302+
return "string", "is this ok for time?", nil
303+
case "github.com/coder/coder/coderd/httpapi.Response":
304+
305+
}
306+
299307
// If it's a struct, just use the name of the struct type
300308
if _, ok := n.Underlying().(*types.Struct); ok {
301309
return name, "Unknown named type, this might not work", nil

0 commit comments

Comments
 (0)