Skip to content

Commit

Permalink
sync latest changes in dubbo-go
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed May 16, 2024
1 parent 4c2004a commit f10d470
Show file tree
Hide file tree
Showing 13 changed files with 4,240 additions and 32 deletions.
30 changes: 13 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@

The `protoc-gen-go-triple` tool generates Go language bindings for Dubbo `service`s based on protobuf definition files.

For users of dubbo-go `v3.2.0` and above, please use `protoc-gen-go-triple` version `v3.0.0` or higher.
For users of dubbo-go version 3.2.0 and above, please use `protoc-gen-go-triple` version 3.0.0 or higher. It is also recommended for other dubbo-go users to use `protoc-gen-go-triple` version 3.0.0 or higher. To generate stubs compatible with dubbo-go version 3.1.x and below, please set the following option:

> It is also recommended for dubbo-go users `v3.1.x` or below to use `protoc-gen-go-triple` version `v3.0.0` or higher. To generate stubs compatible with dubbo-go `v3.1.x` and below, please set the following option:
>
> ```
> protoc --go-triple_out=useOldVersion=true[,other options...]:.
> ```
```
protoc --go-triple_out=useOldVersion=true[,other options...]:.
```

## Prerequisites

Before using `protoc-gen-go-triple`, make sure you have the following prerequisites installed on your system:

- Go (version 1.20 or higher)
- Go (version 1.17 or higher)
- Protocol Buffers (version 3.0 or higher)

## Installation

To install `protoc-gen-go-triple`, you can use the `go get` command:

```shell
go install github.com/dubbogo/protoc-gen-go-triple/v3@v3.0.0
go get dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple
```

Alternatively, you can clone the GitHub repository and build the binary manually:

```shell
git clone https://github.com/dubbogo/protoc-gen-go-triple.git
cd protoc-gen-go-triple
git clone https://github.com/apache/dubbo-go.git
cd cmd/protoc-gen-go-triple
go build
```

Expand All @@ -49,8 +47,6 @@ Both the `--go_out` flag and `--go-triple_out` flag should be set to `.`. Please

## Example

More dubbo examples using this protoc plugin can be found here [apache/dubbo-go-samples](https://github.com/apache/dubbo-go-samples/)

Let's say you have a Protocol Buffer file named `greet.proto`, and you want to generate Triple Go code from it.

```proto
Expand All @@ -59,7 +55,7 @@ syntax = "proto3";
package greet;
option go_package = "github.com/apache/dubbo-go-samples/helloworld/proto;greet";
option go_package = "dubbo.apache.org/dubbo-go/v3/protocol/triple/internal/proto/dubbo3_gen;greet";
message GreetRequest {
string name = 1;
Expand Down Expand Up @@ -87,10 +83,10 @@ Both parts are indispensable. The directory for the file is determined before `;
Resulting in the following directory structure:

```
github.com/apache/dubbo-go-samples/helloworld
|-proto
greet.pb.go
greet.triple.go
dubbo-go/protocol/triple/internal/proto/
|-triple_gen
greet.pb.go
greet.triple.go
```

The package for `greet.pb.go` and `greet.triple.go` are `greet`
Expand Down
2 changes: 1 addition & 1 deletion gen/generator/genTriple.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

import (
"github.com/dubbogo/protoc-gen-go-triple/util"
"dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple/util"
)

func (g *Generator) parseTripleToString(t TripleGo) (string, error) {
Expand Down
19 changes: 11 additions & 8 deletions gen/generator/tripleTpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

import (
"github.com/dubbogo/protoc-gen-go-triple/util"
"dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple/util"
)

var (
Expand Down Expand Up @@ -190,7 +190,8 @@ const TypeCheckTpl = `var ({{$t := .}}{{range $s := .Services}}
`

const InterfaceTpl = `// {{$t := .}}{{range $s := .Services}}{{.ServiceName}} is a client for the {{$t.ProtoPackage}}.{{$s.ServiceName}} service.
const InterfaceTpl = `{{$t := .}}{{range $s := .Services}}
// {{.ServiceName}} is a client for the {{$t.ProtoPackage}}.{{$s.ServiceName}} service.
type {{$s.ServiceName}} interface { {{- range $s.Methods}}
{{upper .MethodName}}(ctx context.Context{{if .StreamsRequest}}{{else}}, req *{{.RequestType}}{{end}}, opts ...client.CallOption) {{if or .StreamsReturn .StreamsRequest}}({{$s.ServiceName}}_{{.MethodName}}Client, error){{else}}(*{{.ReturnType}}, error){{end}}{{end}}
}{{end}}
Expand All @@ -208,7 +209,7 @@ func New{{.ServiceName}}(cli *client.Client, opts ...client.ReferenceOption) ({{
}, nil
}
func SetConsumerService(srv common.RPCService) {
func SetConsumer{{.ServiceName}}(srv common.RPCService) {
dubbo.SetConsumerServiceWithInfo(srv,&{{.ServiceName}}_ClientInfo)
}
Expand Down Expand Up @@ -346,18 +347,19 @@ func (cli *{{$s.ServiceName}}{{.MethodName}}Client) Conn() (triple_protocol.Stre
`

const MethodInfoTpl = `{{$t := .}}{{range $i, $s := .Services}}var {{.ServiceName}}_ClientInfo = client.ClientInfo{
const MethodInfoTpl = `{{$t := .}}{{range $i, $s := .Services}}
var {{.ServiceName}}_ClientInfo = client.ClientInfo{
InterfaceName: "{{$t.ProtoPackage}}.{{.ServiceName}}",
MethodNames: []string{ {{- range $j, $m := .Methods}}"{{.MethodName}}"{{if last $j (len $s.Methods)}}{{else}},{{end}}{{end -}} },
ConnectionInjectFunc: func(dubboCliRaw interface{}, conn *client.Connection) {
dubboCli := dubboCliRaw.(*{{$s.ServiceName}}Impl)
dubboCli.conn = conn
},
}{{end}}
`

const HandlerTpl = `{{$t := .}}{{range $s := .Services}}// {{.ServiceName}}Handler is an implementation of the {{$t.ProtoPackage}}.{{.ServiceName}} service.
const HandlerTpl = `{{$t := .}}{{range $s := .Services}}
// {{.ServiceName}}Handler is an implementation of the {{$t.ProtoPackage}}.{{.ServiceName}} service.
type {{.ServiceName}}Handler interface { {{- range $s.Methods}}
{{upper .MethodName}}(context.Context, {{if .StreamsRequest}}{{$s.ServiceName}}_{{.MethodName}}Server{{else}}*{{.RequestType}}{{if .StreamsReturn}}, {{$s.ServiceName}}_{{.MethodName}}Server{{end}}{{end}}) {{if .StreamsReturn}}error{{else}}(*{{.ReturnType}}, error){{end}}{{end}}
}
Expand All @@ -366,7 +368,7 @@ func Register{{.ServiceName}}Handler(srv *server.Server, hdlr {{.ServiceName}}Ha
return srv.Register(hdlr, &{{.ServiceName}}_ServiceInfo, opts...)
}
func SetProviderService(srv common.RPCService) {
func SetProvider{{.ServiceName}}(srv common.RPCService) {
dubbo.SetProviderServiceWithInfo(srv,&{{.ServiceName}}_ServiceInfo)
}{{end}}
`
Expand Down Expand Up @@ -443,7 +445,8 @@ func (g *{{$s.ServiceName}}{{.MethodName}}Server) Send(msg *{{.ReturnType}}) err
{{end}}{{end}}{{end}}{{end}}
`

const ServiceInfoTpl = `{{$t := .}}{{range $s := .Services}}var {{.ServiceName}}_ServiceInfo = server.ServiceInfo{
const ServiceInfoTpl = `{{$t := .}}{{range $s := .Services}}
var {{.ServiceName}}_ServiceInfo = server.ServiceInfo{
InterfaceName: "{{$t.ProtoPackage}}.{{.ServiceName}}",
ServiceType: (*{{.ServiceName}}Handler)(nil),
Methods: []server.MethodInfo{ {{- range .Methods}}{{if .StreamsRequest}}{{if .StreamsReturn}}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/dubbogo/protoc-gen-go-triple/v3
module dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple

go 1.20

require (
github.com/golang/protobuf v1.5.3
github.com/golang/protobuf v1.5.4
github.com/pkg/errors v0.9.1
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
Expand All @@ -13,3 +14,4 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import (

import (
"google.golang.org/protobuf/compiler/protogen"

"google.golang.org/protobuf/types/pluginpb"

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (linux, 386)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (linux, amd64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (linux, arm64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (darwin, amd64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (darwin, arm64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (windows, 386)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (windows, amd64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:

Check failure on line 33 in main.go

View workflow job for this annotation

GitHub Actions / Release protoc-gen-plugin (windows, arm64)

missing go.sum entry for module providing package google.golang.org/protobuf/types/pluginpb (imported by dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple); to add:
)

import (
"github.com/dubbogo/protoc-gen-go-triple/gen/generator"
"github.com/dubbogo/protoc-gen-go-triple/internal/old_triple"
"github.com/dubbogo/protoc-gen-go-triple/internal/version"
"dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple/gen/generator"
"dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple/internal/old_triple"
"dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple/internal/version"
)

const (
Expand Down
38 changes: 38 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

go build

for dir in ./test/correctly/*/; do
cd "$dir" || exit 1

dir_name=$(basename "$dir")

protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-triple=../../../protoc-gen-go-triple --go-triple_out=. ./proto/greet.proto
go mod tidy

go vet ./proto/*.go
result=$?

if [ $result -ne 0 ]; then
echo "go vet found issues in $dir_name."
exit $result
fi

echo "No issues found in $dir_name."
cd - || exit 1
done
76 changes: 76 additions & 0 deletions test/correctly/multiple_services/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module multiple_service

require (
dubbo.apache.org/dubbo-go/v3 v3.1.1-0.20240202021041-198504bb0029
github.com/golang/protobuf v1.5.4
google.golang.org/protobuf v1.33.0
)

require (
github.com/RoaringBitmap/roaring v1.2.3 // indirect
github.com/Workiva/go-datastructures v1.0.52 // indirect
github.com/apache/dubbo-getty v1.4.9 // indirect
github.com/apache/dubbo-go-hessian2 v1.12.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/creasty/defaults v1.5.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dubbogo/gost v1.14.0 // indirect
github.com/dubbogo/grpc-go v1.42.10 // indirect
github.com/dubbogo/triple v1.2.2-rc3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.12.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/influxdata/tdigest v0.0.1 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/shirou/gopsutil/v3 v3.22.2 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.10.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20221227171554-f9683d7f8bef // indirect
google.golang.org/grpc v1.52.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.22
Loading

0 comments on commit f10d470

Please sign in to comment.