Skip to content

Commit ec85b3b

Browse files
committed
feat(gen): improve gen sdk
feat(gen): improve gen sdk
1 parent 8169692 commit ec85b3b

5 files changed

Lines changed: 13 additions & 103 deletions

File tree

cmd/jzero/.template/client/client-go/plugin.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type {{.PluginName | ToCamel | FirstUpper}} interface {
1515
{{range $resource := .Resources}} {{$resource | ToCamel | FirstUpper}}() {{$.PluginName | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}}.{{$resource | ToCamel | FirstUpper}}
16-
{{end}}{{range $k, $v := .UngroupedAPIs}} // {{$v.MethodName}} {{.Comments}}
16+
{{end}}{{range $k, $v := .UngroupedAPIs}} // {{$v.MethodName | FirstUpper}} {{.Comments}}
1717
// {{$v.Method}}:{{$v.URL}}
1818
{{template "methodDefine" $v}}
1919
{{end}}
Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,28 @@
1-
{{define "methodDefine"}}{{.MethodName}}({{if or .IsStreamServer .IsStreamClient }}{{else}}ctx context.Context,{{end}}{{ .Request.FullName }}) ({{if or .IsStreamServer .IsStreamClient}}*restc.Request{{else}}{{.Response.FullName}}{{end}}, error){{end}}
21
package plugins
32

43
import (
5-
{{range $plugin := .Plugins}}{{if $plugin.UngroupedAPIs}}"context"
6-
{{break}}{{end}}{{end}}"github.com/jzero-io/jzero/core/restc"
7-
{{range $plugin := .Plugins}}{{range $resource := $plugin.Resources}}
8-
{{$plugin.Name | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}} "{{$.Module}}/plugins/{{$plugin.Name}}/typed/{{$resource | lower}}"{{end}}
9-
{{range $v := $plugin.GoImportPaths | uniq}}"{{$v}}"
10-
{{end}}{{end}}
4+
"github.com/jzero-io/jzero/core/restc"
115
)
126

137
type Plugins interface {
14-
{{range $plugin := .Plugins}} {{$plugin.Name | ToCamel | FirstUpper}}() {{$plugin.Name | ToCamel | FirstUpper}}
8+
{{range $pluginName := .PluginNames}} {{$pluginName | ToCamel | FirstUpper}}() {{$pluginName | ToCamel | FirstUpper}}
159
{{end}}
1610
}
1711

18-
{{range $plugin := .Plugins}}
19-
type {{$plugin.Name | ToCamel | FirstUpper}} interface {
20-
{{range $resource := $plugin.Resources}} {{$resource | ToCamel | FirstUpper}}() {{$plugin.Name | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}}.{{$resource | ToCamel | FirstUpper}}
21-
{{end}}{{range $k, $v := $plugin.UngroupedAPIs}} // {{$v.MethodName}} {{.Comments}}
22-
// {{$v.Method}}:{{$v.URL}}
23-
{{template "methodDefine" $v}}
24-
{{end}}
25-
}
26-
27-
type {{$plugin.Name | ToCamel | FirstLower}} struct {
28-
client restc.Client
29-
{{range $resource := $plugin.Resources}} {{$resource | ToCamel | FirstLower}} {{$plugin.Name | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}}.{{$resource | ToCamel | FirstUpper}}
12+
type plugins struct {
13+
{{range $pluginName := .PluginNames}} {{$pluginName | ToCamel | FirstLower}} {{$pluginName | ToCamel | FirstUpper}}
3014
{{end}}
3115
}
3216

33-
{{range $resource := $plugin.Resources}}func (h {{$plugin.Name | ToCamel | FirstLower}}) {{$resource | ToCamel | FirstUpper}}() {{$plugin.Name | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}}.{{$resource | ToCamel | FirstUpper}} {
34-
return h.{{$resource | ToCamel | FirstLower}}
17+
{{range $pluginName := .PluginNames}}func (p *plugins) {{$pluginName | ToCamel | FirstUpper}}() {{$pluginName | ToCamel | FirstUpper}} {
18+
return p.{{$pluginName | ToCamel | FirstLower}}
3519
}
3620

37-
{{end}}{{range $k, $v := $plugin.UngroupedAPIs}}func (h {{$plugin.Name | ToCamel | FirstLower}}) {{template "methodDefine" $v}} {
38-
{{if or $v.IsStreamServer $v.IsStreamClient}}request := h.client.Verb("{{$v.Method}}").
39-
Path(
40-
"{{$v.URL}}",{{range $p := $v.PathParams}}
41-
restc.PathParam{Name: "{{$p.Name}}", Value: in.{{$p.GoName}}},{{end}}
42-
)
43-
return request, nil{{else}}var resp {{$v.Response.FullName}}
44-
err := h.client.Verb("{{$v.Method}}").
45-
Path(
46-
"{{$v.URL}}",{{range $p := $v.PathParams}}
47-
restc.PathParam{Name: "{{$p.Name}}", Value: in.{{$p.GoName}}},{{end}}
48-
).
49-
Params({{if eq $v.Request.Body "*"}}{{else}}{{range $q := $v.QueryParams}}
50-
restc.QueryParam{Name: "{{$q.Name}}", Value: in.{{$q.GoName}}},{{end}}{{end}}
51-
).
52-
{{ if or (eq $v.Method "GET") (eq $v.Method "DELETE") }}{{else}}Body({{if eq $v.Request.Body ""}}nil{{else if eq $v.Request.Body "*"}}in{{else if or (ne $v.Method "GET") (ne $v.Method "DELETE")}}in.{{$v.Request.RealBodyName}}{{else}}nil{{end}}).{{end}}
53-
Do(ctx).
54-
Into(&resp, {{if $v.WrapCodeMsg}}&restc.IntoOptions{
55-
WrapCodeMsg: true,
56-
{{if $v.WrapCodeMsgMapping}}WrapCodeMsgMapping: restc.WrapCodeMsgMapping{
57-
Code: "{{$v.WrapCodeMsgMapping.Code}}",
58-
Data: "{{$v.WrapCodeMsgMapping.Data}}",
59-
Msg: "{{$v.WrapCodeMsgMapping.Msg}}",
60-
},{{end}}
61-
}{{else}}nil{{end}})
62-
63-
if err != nil {
64-
return nil, err
65-
}
66-
67-
return resp, nil{{end}}
68-
}
69-
{{end}}{{end}}
21+
{{end}}
7022

7123
func NewPlugins(c restc.Client) Plugins {
7224
return &plugins{
73-
{{range $plugin := .Plugins}} {{$plugin.Name | ToCamel | FirstLower}}: &{{$plugin.Name | ToCamel | FirstLower}}{
74-
client: c,
75-
{{range $resource := $plugin.Resources}} {{$resource | ToCamel | FirstLower}}: {{$plugin.Name | ToCamel | FirstLower}}{{$resource | ToCamel | FirstUpper}}.New{{$resource | ToCamel | FirstUpper}}(c),
76-
{{end}} },
25+
{{range $pluginName := .PluginNames}} {{$pluginName | ToCamel | FirstLower}}: New{{$pluginName | ToCamel | FirstUpper}}(c),
7726
{{end}}
7827
}
79-
}
80-
81-
type plugins struct {
82-
{{range $plugin := .Plugins}} {{$plugin.Name | ToCamel | FirstLower}} {{$plugin.Name | ToCamel | FirstUpper}}
83-
{{end}}
84-
}
85-
86-
{{range $plugin := .Plugins}}func (p *plugins) {{$plugin.Name | ToCamel | FirstUpper}}() {{$plugin.Name | ToCamel | FirstUpper}} {
87-
return p.{{$plugin.Name | ToCamel | FirstLower}}
88-
}
89-
90-
{{end}}
28+
}

cmd/jzero/.template/client/client-go/plugins_main.go.tpl

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

cmd/jzero/.template/client/client-go/typed/resource.go.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919

2020
type (
2121
{{.Resource | FirstUpper| ToCamel}} interface {
22-
{{range $k, $v := .HTTPInterfaces}}// {{$v.MethodName}} {{.Comments}}
22+
{{range $k, $v := .HTTPInterfaces}}// {{$v.MethodName | FirstUpper}} {{.Comments}}
2323
// {{$v.Method}}:{{$v.URL}}
2424
{{template "methodDefine" $v}}
2525
{{end}}

cmd/jzero/internal/command/gen/gensdk/generator/golang.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,10 @@ func (g *Golang) genMainPluginsGoFile(plugins []plugin.Plugin) (*GeneratedFile,
717717
}
718718

719719
// 使用主插件模板文件生成 plugins.go
720-
pluginsGoBytes, err := templatex.ParseTemplate(filepath.Join("client", "client-go", "plugins_main.go.tpl"), map[string]any{
720+
pluginsGoBytes, err := templatex.ParseTemplate(filepath.Join("client", "client-go", "plugins.go.tpl"), map[string]any{
721721
"Module": gconfig.C.Gen.Sdk.GoModule,
722722
"PluginNames": pluginNames,
723-
}, embeded.ReadTemplateFile(filepath.Join("client", "client-go", "plugins_main.go.tpl")))
723+
}, embeded.ReadTemplateFile(filepath.Join("client", "client-go", "plugins.go.tpl")))
724724
if err != nil {
725725
return nil, err
726726
}

0 commit comments

Comments
 (0)