Skip to content

Commit 2c46b37

Browse files
committed
feat: Add support for json omitempty to apitypings
1 parent 04b3b1f commit 2c46b37

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
}
@@ -343,12 +343,12 @@ export interface WorkspaceAgent {
343343
readonly status: WorkspaceAgentStatus
344344
readonly name: string
345345
readonly resource_id: string
346-
readonly instance_id: string
346+
readonly instance_id?: string
347347
readonly architecture: string
348348
readonly environment_variables: Record<string, string>
349349
readonly operating_system: string
350-
readonly startup_script: string
351-
readonly directory: string
350+
readonly startup_script?: string
351+
readonly directory?: string
352352
}
353353

354354
// From codersdk/workspaceagents.go:47:6
@@ -403,7 +403,7 @@ export interface WorkspaceResource {
403403
readonly workspace_transition: string
404404
readonly type: string
405405
readonly name: string
406-
readonly agents: WorkspaceAgent[]
406+
readonly agents?: WorkspaceAgent[]
407407
}
408408

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

0 commit comments

Comments
 (0)