10000 feat: Add support for json omitempty and embedded structs in apitypings by mafredri · Pull Request #1318 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Add support for json omitempty and embedded structs in apitypings #1318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension All 2 file types selected

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Add support for json omitempty to apitypings
  • Loading branch information
mafredri committed May 10, 2022
commit f0be8853b21103718321bb45ec408a9c031aa8a3
6 changes: 3 additions & 3 deletions codersdk/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ type Pagination struct {
// Offset for better performance. To use it as an alternative,
// set AfterID to the last UUID returned by the previous
// request.
AfterID uuid.UUID `json:"after_id"`
AfterID uuid.UUID `json:"after_id,omitempty"`
// Limit sets the maximum number of users to be returned
// in a single page. If the limit is <= 0, there is no limit
// and all users are returned.
Limit int `json:"limit"`
Limit int `json:"limit,omitempty"`
// Offset is used to indicate which page to return. An offset of 0
// returns the first 'limit' number of users.
// To get the next page, use offset=<limit>*<page_number>.
// Offset is 0 indexed, so the first record sits at offset 0.
Offset int `json:"offset"`
Offset int `json:"offset,omitempty"`
}

// asRequestOption returns a function that can be used in (*Client).request.
Expand Down
6 changes: 5 additions & 1 deletion scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
if jsonName == "" {
jsonName = field.Name()
}
jsonOptional := false
if len(arr) > 1 && arr[1] == "omitempty" {
jsonOptional = true
}

var tsType TypescriptType
// If a `typescript:"string"` exists, we take this, and do not try to infer.
Expand All @@ -273,7 +277,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
_, _ = s.WriteRune('\n')
}
optional := ""
if tsType.Optional {
if jsonOptional || tsType.Optional {
optional = "?"
}
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
Expand Down
18 changes: 9 additions & 9 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface CreateWorkspaceBuildRequest {
// This is likely an enum in an external package ("github.com/coder/coder/coderd/database.WorkspaceTransition")
readonly transition: string
readonly dry_run: boolean
readonly state: string
readonly state?: string
}

// From codersdk/organizations.go:52:6
Expand Down Expand Up @@ -149,9 +149,9 @@ export interface OrganizationMember {

// From codersdk/pagination.go:11:6
export interface Pagination {
readonly after_id: string
readonly limit: number
readonly offset: number
readonly after_id?: string
readonly limit?: number
readonly offset?: number
}

// From codersdk/parameters.go:26:6
Expand Down Expand Up @@ -185,7 +185,7 @@ export interface ProvisionerJob {
readonly created_at: string
readonly started_at?: string
readonly completed_at?: string
readonly error: string
readonly error?: string
readonly status: ProvisionerJobStatus
readonly worker_id?: string
}
Expand Down Expand Up @@ -355,12 +355,12 @@ export interface WorkspaceAgent {
readonly status: WorkspaceAgentStatus
readonly name: string
readonly resource_id: string
readonly instance_id: string
readonly instance_id?: string
readonly architecture: string
readonly environment_variables: Record<string, string>
readonly operating_system: string
readonly startup_script: string
readonly directory: string
readonly startup_script?: string
readonly directory?: string
}

// From codersdk/workspaceagents.go:47:6
Expand Down Expand Up @@ -415,7 +415,7 @@ export interface WorkspaceResource {
readonly workspace_transition: string
readonly type: string
readonly name: string
readonly agents: WorkspaceAgent[]
readonly agents?: WorkspaceAgent[]
}

// From codersdk/parameters.go:16:6
Expand Down
0