Skip to content

Commit 39454b0

Browse files
authored
feat(gen): fix gen route and always add http.Request in logic file (#159)
1 parent 862b667 commit 39454b0

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

.template/frame/api/app/internal/svc/service_context.go.tpl

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
2020
}
2121
return sc
2222
}
23+
24+
func (sc *ServiceContext) MustGetConfig() config.Config {
25+
return sc.Config
26+
}

docs/src/faq/upgrade.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: 版本升级兼容
3+
icon: fluent-mdl2:upgrade-analysis
4+
star: true
5+
order: 2
6+
category: faq
7+
tag:
8+
- faq
9+
---
10+
11+
## from old to v0.30.0+
12+
13+
add `MustGetConfig` method for `ServiceContext` return `config.Config`
14+
15+
```go
16+
func (sc *ServiceContext) MustGetConfig() config.Config {
17+
return sc.Config
18+
}
19+
```
20+

internal/gen/genapi/patch_logic.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (ja *JzeroApi) patchLogic(file LogicFile) error {
7979

8080
if pathx.FileExists(newFilePath) {
8181
_ = os.Remove(file.Path)
82-
return nil
82+
file.Path = newFilePath
8383
}
8484

8585
fset := token.NewFileSet()

internal/gen/genapi/types.go

+5-29
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,12 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
211211
}
212212
}
213213
}
214-
if structType != nil && requestType == nil && !lo.Contains(names, "r") {
214+
if structType != nil && !lo.Contains(names, "r") {
215215
newField := &ast.Field{
216216
Names: []*ast.Ident{ast.NewIdent("r")},
217217
Type: &ast.StarExpr{X: ast.NewIdent("http.Request")},
218218
}
219219
structType.Fields.List = append(structType.Fields.List, newField)
220-
} else if structType != nil && requestType != nil && lo.Contains(names, "r") {
221-
for i, v := range structType.Fields.List {
222-
if len(v.Names) > 0 {
223-
if v.Names[0].Name == "r" {
224-
// 删除这个元素
225-
structType.Fields.List = append(structType.Fields.List[:i], structType.Fields.List[i+1:]...)
226-
}
227-
}
228-
}
229220
}
230221

231222
if structType != nil && responseType == nil && !lo.Contains(names, "w") {
@@ -259,12 +250,12 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
259250
paramNames = append(paramNames, name.Name)
260251
}
261252
}
262-
if requestType == nil && !lo.Contains(paramNames, "r") {
253+
if !lo.Contains(paramNames, "r") {
263254
fn.Type.Params.List = append(fn.Type.Params.List, &ast.Field{
264255
Names: []*ast.Ident{ast.NewIdent("r")},
265256
Type: &ast.StarExpr{X: ast.NewIdent("http.Request")},
266257
})
267-
} else if requestType != nil && lo.Contains(paramNames, "r") {
258+
} else if lo.Contains(paramNames, "r") {
268259
for i, v := range fn.Type.Params.List {
269260
if len(v.Names) > 0 {
270261
if v.Names[0].Name == "r" {
@@ -311,24 +302,13 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
311302
}
312303
}
313304

314-
if requestType == nil && !hasR {
305+
if !hasR {
315306
// Add new field
316307
newField := &ast.KeyValueExpr{
317308
Key: ast.NewIdent("r"),
318309
Value: ast.NewIdent("r"), // or any default value you want
319310
}
320311
compositeLit.Elts = append(compositeLit.Elts, newField)
321-
} else if requestType != nil && hasR {
322-
for i, v := range compositeLit.Elts {
323-
if kv, ok := v.(*ast.KeyValueExpr); ok {
324-
if key, ok := kv.Key.(*ast.Ident); ok {
325-
if key.Name == "r" {
326-
// 删除这个元素
327-
compositeLit.Elts = append(compositeLit.Elts[:i], compositeLit.Elts[i+1:]...)
328-
}
329-
}
330-
}
331-
}
332312
}
333313

334314
if responseType == nil && !hasW {
@@ -361,11 +341,7 @@ func (ja *JzeroApi) changeLogicTypes(f *ast.File, fset *token.FileSet, file Logi
361341
})
362342

363343
// check `net/http` import
364-
if requestType == nil || responseType == nil {
365-
astutil.AddImport(fset, f, "net/http")
366-
} else if requestType != nil && responseType != nil {
367-
astutil.DeleteImport(fset, f, "net/http")
368-
}
344+
astutil.AddImport(fset, f, "net/http")
369345
}
370346
return nil
371347
}

pkg/gogen/gogen.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ func genRoutesConfig(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec)
136136

137137
var jwt string
138138
if g.jwtEnabled {
139-
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
139+
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.MustGetConfig().%s.AccessSecret),", g.authName)
140140
}
141141
if len(g.jwtTrans) > 0 {
142-
jwt = jwt + fmt.Sprintf("\n rest.WithJwtTransition(serverCtx.Config.%s.PrevSecret,serverCtx.Config.%s.Secret),", g.jwtTrans, g.jwtTrans)
142+
jwt = jwt + fmt.Sprintf("\n rest.WithJwtTransition(serverCtx.MustGetConfig().%s.PrevSecret,serverCtx.MustGetConfig().%s.Secret),", g.jwtTrans, g.jwtTrans)
143143
}
144144
var signature, prefix string
145145
if g.signatureEnabled {
146-
signature = "\n rest.WithSignature(serverCtx.Config.Signature),"
146+
signature = "\n rest.WithSignature(serverCtx.MustGetConfig().Signature),"
147147
}
148148
if len(g.prefix) > 0 {
149149
prefix = fmt.Sprintf(`

0 commit comments

Comments
 (0)