Skip to content

Commit f0be885

Browse files
committed
feat: Add support for json omitempty to apitypings
1 parent 13fc406 commit f0be885

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

codersdk/pagination.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ type Pagination struct {
1414
// Offset for better performance. To use it as an alternative,
1515
// set AfterID to the last UUID returned by the previous
1616
// request.
17-
AfterID uuid.UUID `json:"after_id"`
17+
AfterID uuid.UUID `json:"after_id,omitempty"`
1818
// Limit sets the maximum number of users to be returned
1919
// in a single page. If the limit is <= 0, there is no limit
2020
// and all users are returned.
21-
Limit int `json:"limit"`
21+
Limit int `json:"limit,omitempty"`
2222
// Offset is used to indicate which page to return. An offset of 0
2323
// returns the first 'limit' number of users.
2424
// To get the next page, use offset=<limit>*<page_number>.
2525
// Offset is 0 indexed, so the first record sits at offset 0.
26-
Offset int `json:"offset"`
26+
Offset int `json:"offset,omitempty"`
2727
}
2828

2929
// asRequestOption returns a function that can be used in (*Client).request.

scripts/apitypings/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
251251
if jsonName == "" {
252252
jsonName = field.Name()
253253
}
254+
jsonOptional := false
255+
if len(arr) > 1 && arr[1] == "omitempty" {
256+
jsonOptional = true
257+
}
254258

255259
var tsType TypescriptType
256260
// If a `typescript:"string"` exists, we take this, and do not try to infer.
@@ -273,7 +277,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
273277
_, _ = s.WriteRune('\n')
274278
}
275279
optional := ""
276-
if tsType.Optional {
280+
if jsonOptional || tsType.Optional {
277281
optional = "?"
278282
}
279283
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))

site/src/api/typesGenerated.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export interface CreateWorkspaceBuildRequest {
9191
// This is likely an enum in an external package ("github.com/coder/coder/coderd/database.WorkspaceTransition")
9292
readonly transition: string
9393
readonly dry_run: boolean
94-
readonly state: string
94+
readonly state?: string
9595
}
9696

9797
// From codersdk/organizations.go:52:6
@@ -149,9 +149,9 @@ export interface OrganizationMember {
149149

150150
// From codersdk/pagination.go:11:6
151151
export interface Pagination {
152-
readonly after_id: string
153-
readonly limit: number
154-
readonly offset: number
152+
readonly after_id?: string
153+
readonly limit?: number
154+
readonly offset?: number
155155
}
156156

157157
// From codersdk/parameters.go:26:6
@@ -185,7 +185,7 @@ export interface ProvisionerJob {
185185
readonly created_at: string
186186
readonly started_at?: string
187187
readonly completed_at?: string
188-
readonly error: string
188+
readonly error?: string
189189
readonly status: ProvisionerJobStatus
190190
readonly worker_id?: string
191191
}
@@ -355,12 +355,12 @@ export interface WorkspaceAgent {
355355
readonly status: WorkspaceAgentStatus
356356
readonly name: string
357357
readonly resource_id: string
358-
readonly instance_id: string
358+
readonly instance_id?: string
359359
readonly architecture: string
360360
readonly environment_variables: Record<string, string>
361361
readonly operating_system: string
362-
readonly startup_script: string
363-
readonly directory: string
362+
readonly startup_script?: string
363+
readonly directory?: string
364364
}
365365

366366
// From codersdk/workspaceagents.go:47:6
@@ -415,7 +415,7 @@ export interface WorkspaceResource {
415415
readonly workspace_transition: string
416416
readonly type: string
417417
readonly name: string
418-
readonly agents: WorkspaceAgent[]
418+
readonly agents?: WorkspaceAgent[]
419419
}
420420

421421
// From codersdk/parameters.go:16:6

0 commit comments

Comments
 (0)