Skip to content

Commit 21d1687

Browse files
committed
Add comments, run gen
1 parent 462ebeb commit 21d1687

File tree

2 files changed

+231
-224
lines changed

2 files changed

+231
-224
lines changed

scripts/apitypings/main.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
124124
structs := make(map[string]string)
125125
enums := make(map[string]types.Object)
126126
enumConsts := make(map[string][]*types.Const)
127-
//constants := make(map[string]string)
128127

129128
// Look for comments that indicate to ignore a type for typescript generation.
130129
ignoredTypes := make(map[string]struct{})
@@ -167,14 +166,16 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
167166
}
168167
switch named.Underlying().(type) {
169168
case *types.Struct:
170-
// Structs are obvious
169+
// type <Name> struct
170+
// Structs are obvious.
171171
st := obj.Type().Underlying().(*types.Struct)
172172
codeBlock, err := g.buildStruct(obj, st)
173173
if err != nil {
174174
return nil, xerrors.Errorf("generate %q: %w", obj.Name())
175175
}
176176
structs[obj.Name()] = codeBlock
177177
case *types.Basic:
178+
// type <Name> string
178179
// These are enums. Store to expand later.
179180
enums[obj.Name()] = obj
180181
}
@@ -293,9 +294,6 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
293294
case *types.Basic:
294295
bs := ty.(*types.Basic)
295296
// All basic literals (string, bool, int, etc).
296-
// TODO: Actually ensure the golang names are ok, otherwise,
297-
// we want to put another switch to capture these types
298-
// and rename to typescript.
299297
switch {
300298
case bs.Info()&types.IsNumeric > 0:
301299
return TypescriptType{ValueType: "number"}, nil
@@ -308,10 +306,15 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
308306
return TypescriptType{ValueType: bs.Name()}, nil
309307
}
310308
case *types.Struct:
311-
// TODO: This kinda sucks right now. It just dumps the struct def
312-
return TypescriptType{ValueType: ty.String(), Comment: "Unknown struct, this might not work"}, nil
309+
// This handles anonymous structs. This should never happen really.
310+
// Such as:
311+
// type Name struct {
312+
// Embedded struct {
313+
// Field string `json:"field"`
314+
// }
315+
// }
316+
return TypescriptType{ValueType: "any", Comment: "Embedded struct, please fix by naming it"}, nil
313317
case *types.Map:
314-
// TODO: Typescript dictionary??? Object?
315318
// map[string][string] -> Record<string, string>
316319
m := ty.(*types.Map)
317320
keyType, err := g.typescriptType(m.Key())
@@ -360,7 +363,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
360363
return TypescriptType{ValueType: name}, nil
361364
}
362365

363-
// These are special types that we handle uniquely.
366+
// These are external named types that we handle uniquely.
364367
switch n.String() {
365368
case "net/url.URL":
366369
return TypescriptType{ValueType: "string"}, nil
@@ -379,11 +382,14 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
379382
}
380383

381384
// Defer to the underlying type.
382-
return g.typescriptType(ty.Underlying())
385+
ts, err := g.typescriptType(ty.Underlying())
386+
if err != nil {
387+
return TypescriptType{}, xerrors.Errorf("named underlying: %w", err)
388+
}
389+
ts.Comment = "This is likely an enum in an external package"
390+
return ts, nil
383391
case *types.Pointer:
384392
// Dereference pointers.
385-
// TODO: Nullable fields? We could say these fields can be null in the
386-
// typescript.
387393
pt := ty.(*types.Pointer)
388394
resp, err := g.typescriptType(pt.Elem())
389395
if err != nil {

0 commit comments

Comments
 (0)