Skip to content

Commit 9f3e8e4

Browse files
committed
feat: Support map types for codersdk typescript gen
1 parent 899eccc commit 9f3e8e4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

scripts/apitypings/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ func (g *Generator) generateAll() (*TypescriptTypes, error) {
183183
// type <Name> string
184184
// These are enums. Store to expand later.
185185
enums[obj.Name()] = obj
186+
case *types.Map:
187+
// Declared maps that are not structs are still valid codersdk objects.
188+
// Handle them custom by calling 'typescriptType' directly instead of
189+
// iterating through each struct field.
190+
// These types support no json/typescript tags.
191+
// These are **NOT** enums, as a map in Go would never be used for an enum.
192+
ts, err := g.typescriptType(obj.Type().Underlying())
193+
if err != nil {
194+
return nil, xerrors.Errorf("(map) generate %q: %w", obj.Name(), err)
195+
}
196+
197+
var str strings.Builder
198+
_, _ = str.WriteString(g.posLine(obj))
199+
if ts.AboveTypeLine != "" {
200+
str.WriteString(ts.AboveTypeLine)
201+
str.WriteRune('\n')
202+
}
203+
// Use similar output syntax to enums.
204+
str.WriteString(fmt.Sprintf("export type %s = %s\n", obj.Name(), ts.ValueType))
205+
structs[obj.Name()] = str.String()
206+
case *types.Array, *types.Slice:
207+
// TODO: @emyrk if you need this, follow the same design as "*types.Map" case.
186208
}
187209
case *types.Var:
188210
// TODO: Are any enums var declarations? This is also codersdk.Me.

site/src/api/typesGenerated.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ export interface UserPermissionCheckRequest {
334334
readonly checks: Record<string, UserPermissionCheck>
335335
}
336336

337+
// From codersdk/users.go:79:6
338+
export type UserPermissionCheckResponse = Record<string, boolean>
339+
337340
// From codersdk/users.go:74:6
338341
export interface UserRoles {
339342
readonly roles: string[]

0 commit comments

Comments
 (0)