Skip to content

Commit 2fef526

Browse files
authored
refactor: remove config package from dubbogo-cli sample generator (#3218)
* refactor: remove config package from dubbogo-cli sample generator - Migrate sample generator from config-based to programmatic API - Update client template to use client.NewClient() - Update server template to use server.NewServer() - Remove conf/dubbogo.yaml configuration files - Disable config file generation (comment out init() in gen_c_conf.go and gen_s_conf.go) - Fix import order to match project standards - Handle license suffix in constants.go using strings.TrimSuffix - Update README.md and README_CN.md documentation * fix: format constants.go imports to use multi-line style * fix: move licenseForTemplates to var to allow function call * fix: add blank line between license and generated code in api files * fix: add blank line between license and proto file content * fix: add blank line between license and generated code Fix test failures by adding blank lines between the license header and the generated code content in all template files: - api.go: Add blank line at beginning of apiFile constant - api_tripe.go: Add blank line at beginning of apiTripleFile constant - proto.go: Add blank line at beginning of protoFile constant - gen_client.go: Add blank line at beginning of clientCode constant and add newline separator in concatenation - gen_server.go: Add blank line at beginning of serverCode constant and add newline separator in concatenation All tests now pass with these changes.
1 parent 77428fe commit 2fef526

File tree

14 files changed

+104
-113
lines changed

14 files changed

+104
-113
lines changed

tools/dubbogo-cli/README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,11 @@ The Demo uses the direct connection mode, without relying on the registration ce
105105
│   ├── samples_api.proto
106106
│   └── samples_api_triple.pb.go
107107
├── go-client
108-
│   ├── cmd
109-
│   │   └── client.go
110-
│   └── conf
111-
│   └── dubbogo.yaml
108+
│   └── cmd
109+
│   └── client.go
112110
├── go-server
113-
│   ├── cmd
114-
│   │   └── server.go
115-
│   └── conf
116-
│   └── dubbogo.yaml
111+
│   └── cmd
112+
│   └── server.go
117113
└── go.mod
118114
```
119115

tools/dubbogo-cli/README_CN.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,11 @@ dubbogo-cli newDemo .
109109
│   ├── samples_api.proto
110110
│   └── samples_api_triple.pb.go
111111
├── go-client
112-
│   ├── cmd
113-
│   │   └── client.go
114-
│   └── conf
115-
│   └── dubbogo.yaml
112+
│   └── cmd
113+
│   └── client.go
116114
├── go-server
117-
│   ├── cmd
118-
│   │   └── server.go
119-
│   └── conf
120-
│   └── dubbogo.yaml
115+
│   └── cmd
116+
│   └── server.go
121117
└── go.mod
122118
```
123119

tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/cmd/client.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@ import (
2626
)
2727

2828
import (
29+
"dubbo.apache.org/dubbo-go/v3/client"
2930
"dubbo.apache.org/dubbo-go/v3/common/logger"
30-
"dubbo.apache.org/dubbo-go/v3/config"
3131
_ "dubbo.apache.org/dubbo-go/v3/imports"
3232
)
3333

34-
var grpcGreeterImpl = new(api.GreeterClientImpl)
35-
36-
// export DUBBO_GO_CONFIG_PATH= PATH_TO_SAMPLES/helloworld/go-client/conf/dubbogo.yaml
3734
func main() {
38-
config.SetConsumerService(grpcGreeterImpl)
39-
if err := config.Load(); err != nil {
35+
cli, err := client.NewClient(
36+
client.WithClientURL("tri://localhost:20000"),
37+
)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
greeterClient, err := api.NewGreeterClient(cli)
43+
if err != nil {
4044
panic(err)
4145
}
4246

4347
logger.Info("start to test dubbo")
4448
req := &api.HelloRequest{
4549
Name: "laurence",
4650
}
47-
reply, err := grpcGreeterImpl.SayHello(context.Background(), req)
51+
reply, err := greeterClient.SayHello(context.Background(), req)
4852
if err != nil {
4953
logger.Error(err)
5054
}

tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-client/conf/dubbogo.yaml

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

tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/cmd/server.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ import (
2727

2828
import (
2929
"dubbo.apache.org/dubbo-go/v3/common/logger"
30-
"dubbo.apache.org/dubbo-go/v3/config"
3130
_ "dubbo.apache.org/dubbo-go/v3/imports"
31+
"dubbo.apache.org/dubbo-go/v3/protocol"
32+
"dubbo.apache.org/dubbo-go/v3/server"
3233
)
3334

3435
type GreeterProvider struct {
@@ -40,11 +41,22 @@ func (s *GreeterProvider) SayHello(ctx context.Context, in *api.HelloRequest) (*
4041
return &api.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
4142
}
4243

43-
// export DUBBO_GO_CONFIG_PATH= PATH_TO_SAMPLES/helloworld/go-server/conf/dubbogo.yaml
4444
func main() {
45-
config.SetProviderService(&GreeterProvider{})
46-
if err := config.Load(); err != nil {
45+
srv, err := server.NewServer(
46+
server.WithServerProtocol(
47+
protocol.WithPort(20000),
48+
protocol.WithTriple(),
49+
),
50+
)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
if err := api.RegisterGreeterServer(srv, &GreeterProvider{}); err != nil {
56+
panic(err)
57+
}
58+
59+
if err := srv.Serve(); err != nil {
4760
panic(err)
4861
}
49-
select {}
5062
}

tools/dubbogo-cli/cmd/testGenCode/template/newDemo/go-server/conf/dubbogo.yaml

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

tools/dubbogo-cli/generator/sample/api.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/dubbogo-cli/generator/sample/api_tripe.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/dubbogo-cli/generator/sample/constants.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package sample
1919

20+
import (
21+
"strings"
22+
)
23+
2024
const (
2125
license = `/*
2226
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -33,7 +37,7 @@ const (
3337
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3438
* See the License for the specific language governing permissions and
3539
* limitations under the License.
36-
*/
37-
38-
`
40+
*/`
3941
)
42+
43+
var licenseForTemplates = strings.TrimSuffix(license, " \n")

tools/dubbogo-cli/generator/sample/gen_c_conf.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ dubbo:
4242
interface: "" # read from pb`
4343
)
4444

45-
func init() {
46-
fileMap["clientConfigFile"] = &fileGenerator{
47-
path: "./go-client/conf",
48-
file: "dubbogo.yaml",
49-
context: clientConfigFile,
50-
}
51-
}
45+
// Config files are no longer needed in the new config-free pattern
46+
// The client now uses programmatic configuration via client.NewClient()
47+
// func init() {
48+
// fileMap["clientConfigFile"] = &fileGenerator{
49+
// path: "./go-client/conf",
50+
// file: "dubbogo.yaml",
51+
// context: clientConfigFile,
52+
// }
53+
// }

0 commit comments

Comments
 (0)