Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionMakefile
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,7 +83,7 @@ site/out/index.html: $(shell find ./site -not -path './site/node_modules/*' -typ
# Restores GITKEEP files!
git checkout HEAD site/out

site/src/api/typesGenerated.ts: $(shell find codersdk -type f -name '*.go')
site/src/api/typesGenerated.ts:scripts/apitypings/main.go$(shell find codersdk -type f -name '*.go')
go run scripts/apitypings/main.go > site/src/api/typesGenerated.ts
cd site && yarn run format:types

Expand Down
6 changes: 3 additions & 3 deletionscodersdk/pagination.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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
31 changes: 28 additions & 3 deletionsscripts/apitypings/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -237,10 +237,31 @@ func (g *Generator) posLine(obj types.Object) string {
func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, error) {
var s strings.Builder
_, _ = s.WriteString(g.posLine(obj))
_, _ = s.WriteString(fmt.Sprintf("export interface %s ", obj.Name()))

_, _ = s.WriteString(fmt.Sprintf("export interface %s {\n", obj.Name()))
// Handle named embedded structs in the codersdk package via extension.
var extends []string
extendedFields := make(map[int]bool)
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
tag := reflect.StructTag(st.Tag(i))
// Adding a json struct tag causes the json package to consider
// the field unembedded.
if field.Embedded() && tag.Get("json") == "" && field.Pkg().Name() == "codersdk" {
extendedFields[i] = true
extends = append(extends, field.Name())
}
}
if len(extends) > 0 {
_, _ = s.WriteString(fmt.Sprintf("extends %s ", strings.Join(extends, ", ")))
}

_, _ = s.WriteString("{\n")
// For each field in the struct, we print 1 line of the typescript interface
for i := 0; i < st.NumFields(); i++ {
if extendedFields[i] {
continue
}
field := st.Field(i)
tag := reflect.StructTag(st.Tag(i))

Expand All@@ -251,6 +272,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 +298,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
_, _ = s.WriteRune('\n')
}
optional := ""
if tsType.Optional {
ifjsonOptional ||tsType.Optional {
optional = "?"
}
_, _ = s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n", indent, jsonName, optional, tsType.ValueType))
Expand DownExpand Up@@ -322,7 +347,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
return TypescriptType{
ValueType: "any",
AboveTypeLine: fmt.Sprintf("%s\n%s",
indentedComment("Embedded struct, please fix by naming it"),
indentedComment("Embeddedanonymousstruct, please fix by naming it"),
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
),
}, nil
Expand Down
24 changes: 11 additions & 13 deletionssite/src/api/typesGenerated.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand Up@@ -264,9 +264,8 @@ export interface TemplateVersionParameterSchema {
}

// From codersdk/templates.go:74:6
export interface TemplateVersionsByTemplateRequest {
export interface TemplateVersionsByTemplateRequestextends Pagination{
readonly template_id: string
readonly Pagination: Pagination
}

// From codersdk/templates.go:28:6
Expand DownExpand Up@@ -323,10 +322,9 @@ export interface UserRoles {
}

// From codersdk/users.go:23:6
export interface UsersRequest {
export interface UsersRequestextends Pagination{
readonly search: string
readonly status: string
readonly Pagination: Pagination
}

// From codersdk/workspaces.go:18:6
Expand DownExpand Up@@ -355,12 +353,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 DownExpand Up@@ -415,7 +413,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

[8]ページ先頭

©2009-2025 Movatter.jp