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

Commitaab400c

Browse files
authored
feat: Add support for json omitempty and embedded structs in apitypings (#1318)
* feat: Add support for json omitempty to apitypings* feat: Express embedded structs via extends in TypeScript* Handle unembedding via json field in apitypings* Add scripts/apitypings/main.go to Makefile
1 parent13fc406 commitaab400c

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ site/out/index.html: $(shell find ./site -not -path './site/node_modules/*' -typ
8383
# Restores GITKEEP files!
8484
git checkout HEAD site/out
8585

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

‎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-
Limitint`json:"limit"`
21+
Limitint`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-
Offsetint`json:"offset"`
26+
Offsetint`json:"offset,omitempty"`
2727
}
2828

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

‎scripts/apitypings/main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,31 @@ func (g *Generator) posLine(obj types.Object) string {
237237
func (g*Generator)buildStruct(obj types.Object,st*types.Struct) (string,error) {
238238
vars strings.Builder
239239
_,_=s.WriteString(g.posLine(obj))
240+
_,_=s.WriteString(fmt.Sprintf("export interface %s ",obj.Name()))
240241

241-
_,_=s.WriteString(fmt.Sprintf("export interface %s {\n",obj.Name()))
242+
// Handle named embedded structs in the codersdk package via extension.
243+
varextends []string
244+
extendedFields:=make(map[int]bool)
245+
fori:=0;i<st.NumFields();i++ {
246+
field:=st.Field(i)
247+
tag:=reflect.StructTag(st.Tag(i))
248+
// Adding a json struct tag causes the json package to consider
249+
// the field unembedded.
250+
iffield.Embedded()&&tag.Get("json")==""&&field.Pkg().Name()=="codersdk" {
251+
extendedFields[i]=true
252+
extends=append(extends,field.Name())
253+
}
254+
}
255+
iflen(extends)>0 {
256+
_,_=s.WriteString(fmt.Sprintf("extends %s ",strings.Join(extends,", ")))
257+
}
258+
259+
_,_=s.WriteString("{\n")
242260
// For each field in the struct, we print 1 line of the typescript interface
243261
fori:=0;i<st.NumFields();i++ {
262+
ifextendedFields[i] {
263+
continue
264+
}
244265
field:=st.Field(i)
245266
tag:=reflect.StructTag(st.Tag(i))
246267

@@ -251,6 +272,10 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
251272
ifjsonName=="" {
252273
jsonName=field.Name()
253274
}
275+
jsonOptional:=false
276+
iflen(arr)>1&&arr[1]=="omitempty" {
277+
jsonOptional=true
278+
}
254279

255280
vartsTypeTypescriptType
256281
// If a `typescript:"string"` exists, we take this, and do not try to infer.
@@ -273,7 +298,7 @@ func (g *Generator) buildStruct(obj types.Object, st *types.Struct) (string, err
273298
_,_=s.WriteRune('\n')
274299
}
275300
optional:=""
276-
iftsType.Optional {
301+
ifjsonOptional||tsType.Optional {
277302
optional="?"
278303
}
279304
_,_=s.WriteString(fmt.Sprintf("%sreadonly %s%s: %s\n",indent,jsonName,optional,tsType.ValueType))
@@ -322,7 +347,7 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
322347
returnTypescriptType{
323348
ValueType:"any",
324349
AboveTypeLine:fmt.Sprintf("%s\n%s",
325-
indentedComment("Embedded struct, please fix by naming it"),
350+
indentedComment("Embeddedanonymousstruct, please fix by naming it"),
326351
indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any"),
327352
),
328353
},nil

‎site/src/api/typesGenerated.ts

Lines changed: 11 additions & 13 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
readonlytransition:string
9393
readonlydry_run:boolean
94-
readonlystate:string
94+
readonlystate?: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
exportinterfacePagination{
152-
readonlyafter_id:string
153-
readonlylimit:number
154-
readonlyoffset:number
152+
readonlyafter_id?:string
153+
readonlylimit?:number
154+
readonlyoffset?:number
155155
}
156156

157157
// From codersdk/parameters.go:26:6
@@ -185,7 +185,7 @@ export interface ProvisionerJob {
185185
readonlycreated_at:string
186186
readonlystarted_at?:string
187187
readonlycompleted_at?:string
188-
readonlyerror:string
188+
readonlyerror?:string
189189
readonlystatus:ProvisionerJobStatus
190190
readonlyworker_id?:string
191191
}
@@ -264,9 +264,8 @@ export interface TemplateVersionParameterSchema {
264264
}
265265

266266
// From codersdk/templates.go:74:6
267-
exportinterfaceTemplateVersionsByTemplateRequest{
267+
exportinterfaceTemplateVersionsByTemplateRequestextendsPagination{
268268
readonlytemplate_id:string
269-
readonlyPagination:Pagination
270269
}
271270

272271
// From codersdk/templates.go:28:6
@@ -323,10 +322,9 @@ export interface UserRoles {
323322
}
324323

325324
// From codersdk/users.go:23:6
326-
exportinterfaceUsersRequest{
325+
exportinterfaceUsersRequestextendsPagination{
327326
readonlysearch:string
328327
readonlystatus:string
329-
readonlyPagination:Pagination
330328
}
331329

332330
// From codersdk/workspaces.go:18:6
@@ -355,12 +353,12 @@ export interface WorkspaceAgent {
355353
readonlystatus:WorkspaceAgentStatus
356354
readonlyname:string
357355
readonlyresource_id:string
358-
readonlyinstance_id:string
356+
readonlyinstance_id?:string
359357
readonlyarchitecture:string
360358
readonlyenvironment_variables:Record<string,string>
361359
readonlyoperating_system:string
362-
readonlystartup_script:string
363-
readonlydirectory:string
360+
readonlystartup_script?:string
361+
readonlydirectory?:string
364362
}
365363

366364
// From codersdk/workspaceagents.go:47:6
@@ -415,7 +413,7 @@ export interface WorkspaceResource {
415413
readonlyworkspace_transition:string
416414
readonlytype:string
417415
readonlyname:string
418-
readonlyagents:WorkspaceAgent[]
416+
readonlyagents?:WorkspaceAgent[]
419417
}
420418

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp