Skip to content

Commit 269e1e8

Browse files
committed
fix(cliengen): types should be sorted
1 parent 36439a5 commit 269e1e8

File tree

2 files changed

+98
-99
lines changed

2 files changed

+98
-99
lines changed

devpkg/clientgen/gen.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"slices"
1515
"strconv"
1616
"strings"
17-
"sync"
1817

1918
"github.com/octohelm/courier/pkg/courierhttp/client"
2019
"github.com/octohelm/courier/pkg/openapi"
@@ -28,7 +27,7 @@ func init() {
2827
}
2928

3029
type clientGen struct {
31-
types sync.Map
30+
types map[string]*typ
3231
oas openapi.Payload
3332
}
3433

@@ -43,6 +42,8 @@ func (g *clientGen) Name() string {
4342
}
4443

4544
func (g *clientGen) GenerateType(c gengo.Context, named *types.Named) error {
45+
g.types = map[string]*typ{}
46+
4647
return g.generateClient(c, named)
4748
}
4849

@@ -150,18 +151,15 @@ func (g *clientGen) generateClient(c gengo.Context, named *types.Named) error {
150151
}
151152
}
152153

153-
var e error
154+
for _, k := range slices.Sorted(maps.Keys(g.types)) {
155+
t := g.types[k]
154156

155-
g.types.Range(func(k, value any) bool {
156-
t := value.(*typ)
157-
if err := g.genDef(c, k.(string), t, o); err != nil {
158-
e = err
159-
return false
157+
if err := g.genDef(c, k, t, o); err != nil {
158+
return err
160159
}
161-
return true
162-
})
160+
}
163161

164-
return e
162+
return nil
165163
}
166164

167165
func (g *clientGen) genOperation(c gengo.Context, path string, method string, operation *openapi.OperationObject, o option) error {
@@ -184,11 +182,12 @@ func (g *clientGen) genOperation(c gengo.Context, path string, method string, op
184182
for _, mt := range operation.ResponsesObject.Responses[statusOrStr].Content {
185183
typeName := fmt.Sprintf("%sResponse", operationID)
186184

187-
g.types.Store(typeName, &typ{
185+
g.types[typeName] = &typ{
188186
Alias: true,
189187
Schema: mt.Schema,
190188
Decl: g.typeOfSchema(c, mt.Schema, typeName, o),
191-
})
189+
}
190+
192191
hasResponse = true
193192
}
194193
}
@@ -572,14 +571,16 @@ func (g *clientGen) typeOfSchema(c gengo.Context, schema jsonschema.Schema, decl
572571
case *jsonschema.RefType:
573572
name := x.Ref.RefName()
574573

575-
if _, ok := g.types.Load(name); !ok {
574+
if _, ok := g.types[name]; !ok {
576575
s := g.oas.Schemas[name]
577-
g.types.Store(name, nil)
578576

579-
g.types.Store(name, &typ{
577+
// holder for self ref
578+
g.types[name] = nil
579+
580+
g.types[name] = &typ{
580581
Schema: s,
581582
Decl: g.typeOfSchema(c, s, declTypeName, o),
582-
})
583+
}
583584
}
584585
if name == declTypeName {
585586
return snippet.ID("*" + name)

example/client/example/zz_generated.client.go

Lines changed: 80 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,76 @@ import (
1515
courierhttp "github.com/octohelm/courier/pkg/courierhttp"
1616
)
1717

18-
type GetStoreBlob struct {
19-
courierhttp.MethodGet `path:"/api/example/v0/store/{scope}/blobs/{digest}"`
18+
type Cookie struct {
19+
courierhttp.MethodPost `path:"/api/example/v0/cookie-ping-pong"`
2020

21-
GetStoreBlobParameters
21+
CookieParameters
2222
}
2323

24-
type GetStoreBlobParameters struct {
25-
Scope string `name:"scope" in:"path"`
26-
27-
Digest string `name:"digest" in:"path"`
24+
type CookieParameters struct {
25+
Token string `name:"token,omitzero" in:"cookie"`
2826
}
2927

30-
func (GetStoreBlob) ResponseData() *GetStoreBlobResponse {
31-
return new(GetStoreBlobResponse)
28+
func (Cookie) ResponseData() *courier.NoContent {
29+
return new(courier.NoContent)
3230
}
3331

34-
type UploadStoreBlob struct {
35-
courierhttp.MethodPost `path:"/api/example/v0/store/{scope}/blobs/uploads"`
32+
type CreateOrg struct {
33+
courierhttp.MethodPost `path:"/api/example/v0/orgs"`
3634

37-
UploadStoreBlobParameters
35+
CreateOrgParameters
3836
}
3937

40-
type UploadStoreBlobParameters struct {
41-
Scope string `name:"scope" in:"path"`
42-
43-
RequestBody IoReadCloser `in:"body" mime:"application/octet-stream"`
38+
type CreateOrgParameters struct {
39+
RequestBody OrgInfo `in:"body" mime:"application/json"`
4440
}
4541

46-
func (UploadStoreBlob) ResponseData() *courier.NoContent {
42+
func (CreateOrg) ResponseData() *courier.NoContent {
4743
return new(courier.NoContent)
4844
}
4945

50-
type GetFile struct {
51-
courierhttp.MethodPost `path:"/api/example/v0/blobs/{path}"`
46+
type ListOrg struct {
47+
courierhttp.MethodGet `path:"/api/example/v0/orgs"`
5248

53-
GetFileParameters
49+
ListOrgParameters
5450
}
5551

56-
type GetFileParameters struct {
57-
Path string `name:"path" in:"path"`
52+
type ListOrgParameters struct {
53+
OrgID *OrgIDAsFilter `name:"org~id,omitzero" in:"query"`
5854
}
5955

60-
func (GetFile) ResponseData() *GetFileResponse {
61-
return new(GetFileResponse)
56+
func (ListOrg) ResponseData() *ListOrgResponse {
57+
return new(ListOrgResponse)
6258
}
6359

64-
type UploadBlob struct {
65-
courierhttp.MethodPost `path:"/api/example/v0/blobs"`
60+
type DeleteOrg struct {
61+
courierhttp.MethodDelete `path:"/api/example/v0/orgs/{orgName}"`
6662

67-
UploadBlobParameters
63+
DeleteOrgParameters
6864
}
6965

70-
type UploadBlobParameters struct {
71-
RequestBody IoReadCloser `in:"body" mime:"application/octet-stream"`
66+
type DeleteOrgParameters struct {
67+
OrgName string `name:"orgName" in:"path"`
7268
}
7369

74-
func (UploadBlob) ResponseData() *courier.NoContent {
70+
func (DeleteOrg) ResponseData() *courier.NoContent {
7571
return new(courier.NoContent)
7672
}
7773

74+
type GetOrg struct {
75+
courierhttp.MethodGet `path:"/api/example/v0/orgs/{orgName}"`
76+
77+
GetOrgParameters
78+
}
79+
80+
type GetOrgParameters struct {
81+
OrgName OrgName `name:"orgName" in:"path"`
82+
}
83+
84+
func (GetOrg) ResponseData() *GetOrgResponse {
85+
return new(GetOrgResponse)
86+
}
87+
7888
type ListOrgOld struct {
7989
courierhttp.MethodGet `path:"/api/example/v0/org"`
8090

@@ -87,93 +97,81 @@ func (ListOrgOld) ResponseData() *courier.NoContent {
8797
return new(courier.NoContent)
8898
}
8999

90-
type Cookie struct {
91-
courierhttp.MethodPost `path:"/api/example/v0/cookie-ping-pong"`
100+
type GetStoreBlob struct {
101+
courierhttp.MethodGet `path:"/api/example/v0/store/{scope}/blobs/{digest}"`
92102

93-
CookieParameters
103+
GetStoreBlobParameters
94104
}
95105

96-
type CookieParameters struct {
97-
Token string `name:"token,omitzero" in:"cookie"`
106+
type GetStoreBlobParameters struct {
107+
Scope string `name:"scope" in:"path"`
108+
109+
Digest string `name:"digest" in:"path"`
98110
}
99111

100-
func (Cookie) ResponseData() *CookieResponse {
101-
return new(CookieResponse)
112+
func (GetStoreBlob) ResponseData() *GetStoreBlobResponse {
113+
return new(GetStoreBlobResponse)
102114
}
103115

104-
type CreateOrg struct {
105-
courierhttp.MethodPost `path:"/api/example/v0/orgs"`
116+
type UploadStoreBlob struct {
117+
courierhttp.MethodPost `path:"/api/example/v0/store/{scope}/blobs/uploads"`
106118

107-
CreateOrgParameters
119+
UploadStoreBlobParameters
108120
}
109121

110-
type CreateOrgParameters struct {
111-
RequestBody OrgInfo `in:"body" mime:"application/json"`
122+
type UploadStoreBlobParameters struct {
123+
Scope string `name:"scope" in:"path"`
124+
125+
RequestBody IoReadCloser `in:"body" mime:"application/octet-stream"`
112126
}
113127

114-
func (CreateOrg) ResponseData() *courier.NoContent {
128+
func (UploadStoreBlob) ResponseData() *courier.NoContent {
115129
return new(courier.NoContent)
116130
}
117131

118-
type ListOrg struct {
119-
courierhttp.MethodGet `path:"/api/example/v0/orgs"`
132+
type GetFile struct {
133+
courierhttp.MethodPost `path:"/api/example/v0/blobs/{path}"`
120134

121-
ListOrgParameters
135+
GetFileParameters
122136
}
123137

124-
type ListOrgParameters struct {
125-
OrgID *OrgIDAsFilter `name:"org~id,omitzero" in:"query"`
138+
type GetFileParameters struct {
139+
Path string `name:"path" in:"path"`
126140
}
127141

128-
func (ListOrg) ResponseData() *ListOrgResponse {
129-
return new(ListOrgResponse)
142+
func (GetFile) ResponseData() *GetFileResponse {
143+
return new(GetFileResponse)
130144
}
131145

132-
type DeleteOrg struct {
133-
courierhttp.MethodDelete `path:"/api/example/v0/orgs/{orgName}"`
146+
type UploadBlob struct {
147+
courierhttp.MethodPost `path:"/api/example/v0/blobs"`
134148

135-
DeleteOrgParameters
149+
UploadBlobParameters
136150
}
137151

138-
type DeleteOrgParameters struct {
139-
OrgName string `name:"orgName" in:"path"`
152+
type UploadBlobParameters struct {
153+
RequestBody IoReadCloser `in:"body" mime:"application/octet-stream"`
140154
}
141155

142-
func (DeleteOrg) ResponseData() *courier.NoContent {
156+
func (UploadBlob) ResponseData() *courier.NoContent {
143157
return new(courier.NoContent)
144158
}
145159

146-
type GetOrg struct {
147-
courierhttp.MethodGet `path:"/api/example/v0/orgs/{orgName}"`
148-
149-
GetOrgParameters
150-
}
151-
152-
type GetOrgParameters struct {
153-
OrgName OrgName `name:"orgName" in:"path"`
154-
}
155-
156-
func (GetOrg) ResponseData() *GetOrgResponse {
157-
return new(GetOrgResponse)
158-
}
160+
type GetFileResponse = string
159161

160162
type (
161-
OrgDetail = org.Detail
162-
OrgName = org.Name
163-
Time = time.Time
164-
OrgType = domainorg.Type
165-
IoReadCloser = io.ReadCloser
166-
OrgInfo = org.Info
167-
OrgIDAsFilter = filter.Filter[org.ID]
168-
GetOrgResponse = org.Detail
169-
GetFileResponse = string
163+
GetOrgResponse = org.Detail
164+
GetStoreBlobResponse = string
170165
)
171166

172-
type GetStoreBlobResponse = string
173-
174-
type CookieResponse = any
175-
176167
type (
168+
IoReadCloser = io.ReadCloser
177169
ListOrgResponse = org.DataList[org.Info]
170+
OrgDetail = org.Detail
171+
OrgIDAsFilter = filter.Filter[org.ID]
172+
OrgInfo = org.Info
178173
OrgInfoAsDataList = org.DataList[org.Info]
174+
OrgName = org.Name
175+
OrgType = domainorg.Type
176+
Time = time.Time
179177
)

0 commit comments

Comments
 (0)