Skip to content

Commit c7d047b

Browse files
authored
Refactor rdb and http module generator (#3)
* fix build bug * ♻️ reafactor and generator
1 parent 0ed3d4f commit c7d047b

File tree

24 files changed

+341
-138
lines changed

24 files changed

+341
-138
lines changed

config/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ const (
2626
ColsGroupTmpl = "cols_group.tmpl"
2727

2828
RdbTmpl = "rdb.tmpl"
29-
RdbRepoTmpl = "rdb_repo.tmpl"
3029
RdbDeleteTmpl = "rdb_delete.tmpl"
3130
RdbFetchTmpl = "rdb_fetch.tmpl"
3231
RdbUpdateTmpl = "rdb_update.tmpl"
3332
RdbInsertTmpl = "rdb_insert.tmpl"
3433

3534
RepoTmpl = "repo.tmpl"
36-
RepoRepoTmpl = "repo_repo.tmpl"
35+
RepoExtTmpl = "repo_ext.tmpl"
3736
RepoDeleteTmpl = "repo_delete.tmpl"
3837
RepoFetchTmpl = "repo_fetch.tmpl"
3938
RepoUpdateTmpl = "repo_update.tmpl"
@@ -44,6 +43,7 @@ const (
4443
HttpProtocolTmpl = "http_protocol.tmpl"
4544
HttpProtocolMethodTmpl = "http_protocol_method.tmpl"
4645
HttpProtocolTierTmpl = "http_protocol_tier.tmpl"
46+
HttpParamImplTmpl = "http_param_impl.tmpl"
4747
)
4848

4949
var (
@@ -69,14 +69,13 @@ var templateNames = []string{
6969
ColsGroupTmpl,
7070

7171
RepoTmpl,
72-
RepoRepoTmpl,
72+
RepoExtTmpl,
7373
RepoDeleteTmpl,
7474
RepoFetchTmpl,
7575
RepoUpdateTmpl,
7676
RepoInsertTmpl,
7777

7878
RdbTmpl,
79-
RdbRepoTmpl,
8079
RdbDeleteTmpl,
8180
RdbFetchTmpl,
8281
RdbUpdateTmpl,
@@ -87,6 +86,7 @@ var templateNames = []string{
8786
HttpProtocolTmpl,
8887
HttpProtocolMethodTmpl,
8988
HttpProtocolTierTmpl,
89+
HttpParamImplTmpl,
9090
}
9191

9292
func init() {

config/templates/http_handler.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ func (handler *{{.ServiceName}}HttpHandler) GetRoutersMap() map[string]http.Hand
3333
func (handler *{{.ServiceName}}HttpHandler) GetRouterGroupBase() string {
3434
return "{{.RouterBasePath}}"
3535
}
36+
37+
func (handler *{{.ServiceName}}HttpHandler) DecodeKey(key string) (string, string, error) {
38+
return handler.container.DecodeKey(key)
39+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
handler.container.Set("{{.HttpMethod}}", "{{.HttpMethodPath}}", func(w http.ResponseWriter, r *http.Request) {
2-
ctx, request := handler.tier.Build{{.MethodName}}Request(r)
3-
response, err := handler.service.{{.MethodName}}(ctx, request)
4-
handler.tier.Wrap{{.MethodName}}Response(w, response, err)
1+
handler.container.Set(
2+
handler.container.EncodeKey("{{.HttpMethod}}", "{{.HttpMethodPath}}"),
3+
func(w http.ResponseWriter, r *http.Request) {
4+
ctx, request := handler.tier.Before{{.MethodName}}Request(r)
5+
response, err := handler.service.{{.MethodName}}(ctx, request)
6+
handler.tier.After{{.MethodName}}Response(w, response, err)
57
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func (m *{{.ModelType}}) GetCode() string {
2+
return m.Code
3+
}
4+
5+
func (m *{{.ModelType}}) SetCode(code string) {
6+
m.Code = code
7+
}
8+
9+
func (m *{{.ModelType}}) GetMessage() string {
10+
return m.Message
11+
}
12+
13+
func (m *{{.ModelType}}) SetMessage(message string) {
14+
m.Message = message
15+
}
16+
17+
func (m *{{.ModelType}}) GetBody() interface{} {
18+
return m.Body
19+
}
20+
21+
func (m *{{.ModelType}}) ResetBody(){
22+
m.Body = nil
23+
}

config/templates/http_protocol.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ type {{.ServiceName}}ServiceTier interface {
1111
}
1212

1313
type {{.ServiceName}}ServiceContainer interface {
14-
Set(method string, path string, runner func(w http.ResponseWriter, r *http.Request))
15-
Get(method string, path string) (func(w http.ResponseWriter, r *http.Request), bool)
14+
Set(key string, runner func(w http.ResponseWriter, r *http.Request))
15+
Get(key string) (func(w http.ResponseWriter, r *http.Request), bool)
1616
ToMap() map[string]http.HandlerFunc
17+
EncodeKey(method, path string) string
18+
DecodeKey(key string) (string, string, error)
1719
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Build{{.MethodName}}Request(input *http.Request) (context.Context, {{.RequestType}})
2-
Wrap{{.MethodName}}Response(output http.ResponseWriter, resp {{.ResponseType}}, err error)
1+
Before{{.MethodName}}Request(input *http.Request) (context.Context, {{.RequestType}})
2+
After{{.MethodName}}Response(output http.ResponseWriter, resp {{.ResponseType}}, err error)

config/templates/rdb.tmpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@ import (
66
"context"
77

88
"github.com/Masterminds/squirrel"
9+
"github.com/ncuhome/cato/core/rdb"
910

1011
{{- if not .IsModelAnotherPackage}}
1112
{{.ModelPackageAlias}} "{{.ModelPackage}}"
1213
{{- end}}
1314
)
1415

16+
type RdbRepo{{.ModelType}} struct {
17+
basicRepo
18+
e rdb.Engine[{{.ModelPackageAlias}}.{{.ModelType}}]
19+
}
20+
21+
func NewRdbRepo{{.ModelType}}(eg rdb.Engine[{{.ModelPackageAlias}}.{{.ModelType}}]) *RdbRepo{{.ModelType}} {
22+
return &RdbRepo{{.ModelType}}{basicRepo: basicRepo{eg}, e: eg}
23+
}
24+
25+
1526
type basicRepo struct {
16-
engine
27+
rdb.Engine[{{.ModelPackageAlias}}.{{.ModelType}}]
1728
}
1829

1930

config/templates/rdb_repo.tmpl

Lines changed: 0 additions & 44 deletions
This file was deleted.

config/templates/repo.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ import (
1010
{{- end}}
1111
)
1212

13+
var (
14+
repo Repo
15+
)
16+
17+
func GetRepo() Repo {
18+
return repo
19+
}
20+
21+
type Repo interface {
22+
basic
23+
extension
24+
}
25+
26+
1327
type basic interface {
1428
{{- range .RepoFuncs}}
1529
{{.}}

config/templates/repo_ext.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package {{.RepoPackageName}}
2+
3+
4+
type extension interface {}
5+

0 commit comments

Comments
 (0)