@@ -124,7 +124,6 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
124
124
structs := make (map [string ]string )
125
125
enums := make (map [string ]types.Object )
126
126
enumConsts := make (map [string ][]* types.Const )
127
- //constants := make(map[string]string)
128
127
129
128
// Look for comments that indicate to ignore a type for typescript generation.
130
129
ignoredTypes := make (map [string ]struct {})
@@ -167,14 +166,16 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
167
166
}
168
167
switch named .Underlying ().(type ) {
169
168
case * types.Struct :
170
- // Structs are obvious
169
+ // type <Name> struct
170
+ // Structs are obvious.
171
171
st := obj .Type ().Underlying ().(* types.Struct )
172
172
codeBlock , err := g .buildStruct (obj , st )
173
173
if err != nil {
174
174
return nil , xerrors .Errorf ("generate %q: %w" , obj .Name ())
175
175
}
176
176
structs [obj .Name ()] = codeBlock
177
177
case * types.Basic :
178
+ // type <Name> string
178
179
// These are enums. Store to expand later.
179
180
enums [obj .Name ()] = obj
180
181
}
@@ -293,9 +294,6 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
293
294
case * types.Basic :
294
295
bs := ty .(* types.Basic )
295
296
// 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.
299
297
switch {
300
298
case bs .Info ()& types .IsNumeric > 0 :
301
299
return TypescriptType {ValueType : "number" }, nil
@@ -308,10 +306,15 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
308
306
return TypescriptType {ValueType : bs .Name ()}, nil
309
307
}
310
308
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
313
317
case * types.Map :
314
- // TODO: Typescript dictionary??? Object?
315
318
// map[string][string] -> Record<string, string>
316
319
m := ty .(* types.Map )
317
320
keyType , err := g .typescriptType (m .Key ())
@@ -360,7 +363,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
360
363
return TypescriptType {ValueType : name }, nil
361
364
}
362
365
363
- // These are special types that we handle uniquely.
366
+ // These are external named types that we handle uniquely.
364
367
switch n .String () {
365
368
case "net/url.URL" :
366
369
return TypescriptType {ValueType : "string" }, nil
@@ -379,11 +382,14 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
379
382
}
380
383
381
384
// 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
383
391
case * types.Pointer :
384
392
// Dereference pointers.
385
- // TODO: Nullable fields? We could say these fields can be null in the
386
- // typescript.
387
393
pt := ty .(* types.Pointer )
388
394
resp , err := g .typescriptType (pt .Elem ())
389
395
if err != nil {
0 commit comments