Skip to content

Commit c62feeb

Browse files
committed
Add map support
1 parent 2741efa commit c62feeb

File tree

2 files changed

+314
-167
lines changed

2 files changed

+314
-167
lines changed

scripts/apitypings/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
258258
tsType.ValueType = typescriptTag
259259
} else {
260260
var err error
261-
tsType, err = g.typescriptType(obj, field.Type())
261+
tsType, err = g.typescriptType(field.Type())
262262
if err != nil {
263263
return "", xerrors.Errorf("typescript type: %w", err)
264264
}
@@ -288,7 +288,7 @@ type TypescriptType struct {
288288
// golang type.
289289
// Eg:
290290
// []byte returns "string"
291-
func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptType, error) {
291+
func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
292292
switch ty.(type) {
293293
case *types.Basic:
294294
bs := ty.(*types.Basic)
@@ -312,7 +312,20 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
312312
return TypescriptType{ValueType: ty.String(), Comment: "Unknown struct, this might not work"}, nil
313313
case *types.Map:
314314
// TODO: Typescript dictionary??? Object?
315-
return TypescriptType{ValueType: "map_not_implemented"}, nil
315+
// map[string][string] -> Record<string, string>
316+
m := ty.(*types.Map)
317+
keyType, err := g.typescriptType(m.Key())
318+
if err != nil {
319+
return TypescriptType{}, xerrors.Errorf("map key: %w", err)
320+
}
321+
valueType, err := g.typescriptType(m.Elem())
322+
if err != nil {
323+
return TypescriptType{}, xerrors.Errorf("map key: %w", err)
324+
}
325+
326+
return TypescriptType{
327+
ValueType: fmt.Sprintf("Record<%s, %s>", keyType.ValueType, valueType.ValueType),
328+
}, nil
316329
case *types.Slice, *types.Array:
317330
// Slice/Arrays are pretty much the same.
318331
type hasElem interface {
@@ -329,7 +342,7 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
329342
return TypescriptType{ValueType: "string"}, nil
330343
default:
331344
// By default, just do an array of the underlying type.
332-
underlying, err := g.typescriptType(obj, arr.Elem())
345+
underlying, err := g.typescriptType(arr.Elem())
333346
if err != nil {
334347
return TypescriptType{}, xerrors.Errorf("array: %w", err)
335348
}
@@ -362,13 +375,13 @@ func (g *Generator) typescriptType(obj types.Object, ty types.Type) (TypescriptT
362375
}
363376

364377
// Defer to the underlying type.
365-
return g.typescriptType(obj, ty.Underlying())
378+
return g.typescriptType(ty.Underlying())
366379
case *types.Pointer:
367380
// Dereference pointers.
368381
// TODO: Nullable fields? We could say these fields can be null in the
369382
// typescript.
370383
pt := ty.(*types.Pointer)
371-
resp, err := g.typescriptType(obj, pt.Elem())
384+
resp, err := g.typescriptType(pt.Elem())
372385
if err != nil {
373386
return TypescriptType{}, xerrors.Errorf("pointer: %w", err)
374387
}

0 commit comments

Comments
 (0)