Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tools): add dubbo protoc plugin to implement Message interface #93

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/protoc-gen-kitex-dubbo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
31 changes: 31 additions & 0 deletions tools/protoc-gen-kitex-dubbo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
protoc-gen-kitex-dubbo
========

`protoc-gen-kitex-dubbo` is a protoc plugin that can generate go file to implement the dubbo extension or java dubbo API.

Installation
------------

Note: before executing the following commands, **make sure your `GOPATH` environment is properly set**.

Using `go install`:

`GO111MODULE=on go install github.com/kitex-contrib/codec-dubbo/tools/protoc-gen-kitex-dubbo`

Or build from source:

```shell
git clone https://github.com/kitex-contrib/codec-dubbo.git
cd codec-dubbo/tools/protoc-gen-kitex-dubbo
export GO111MODULE=on
go mod tidy && go mod vendor
go build
go install
```

Usage
-----

```
kitex -type protobuf -protobuf-plugin=kitex-dubbo::. -service GreetService ./api.proto
```
77 changes: 77 additions & 0 deletions tools/protoc-gen-kitex-dubbo/generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 CloudWeGo Authors
//
// 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.

package generator

import (
"html/template"

"google.golang.org/protobuf/compiler/protogen"
)

var extTemplates []string

// RegisterTemplate add templates to the generator
func RegisterTemplate(t ...string) {
extTemplates = append(extTemplates, t...)
}

const (
filePrefix = "kitex_gen/"
fileExt = "hessian2_ext.go"
version = "v0.0.1"
)

type Generator interface {
Generate(gen *protogen.Plugin) error
}

type generator struct {
extLang string
tpl *template.Template
funcs []func(g *generator, gen *protogen.Plugin) error
}

func (g *generator) Generate(gen *protogen.Plugin) error {
for _, f := range g.funcs {
err := f(g, gen)
if err != nil {
return err
}
}
return nil
}

// New implements Generator
func New(req *protogen.Plugin, lang string) (Generator, error) {
tpl := template.New("kitex-dubbo")
var err error
for _, temp := range extTemplates {
tpl, err = tpl.Parse(temp)
if err != nil {
return nil, err
}
}
funcs := []func(g *generator, gen *protogen.Plugin) error{
generateHessian2Ext,
}
return &generator{
tpl: tpl,
extLang: lang,
funcs: funcs,
}, nil
}
125 changes: 125 additions & 0 deletions tools/protoc-gen-kitex-dubbo/generator/hessian2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2024 CloudWeGo Authors
//
// 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.

package generator

import (
"strings"

"google.golang.org/protobuf/compiler/protogen"
)

func generateHessian2Ext(g *generator, gen *protogen.Plugin) error {
for _, file := range gen.Files {
err := g.generateAPIFile(gen, file)
if err != nil {
return err
}

for _, svr := range file.Services {
err := g.generateServiceExt(gen, svr, file)
if err != nil {
return err
}
}
}
return nil
}

// generateAPIFile implements dubbo extension interface every message.
// ref: https://github.com/kitex-contrib/codec-dubbo/blob/dcc4d83669b139d5daa75823d2eda5584d92ebc7/pkg/iface/protocol.go#L22
func (g *generator) generateAPIFile(gen *protogen.Plugin, file *protogen.File) error {
if len(file.Messages) == 0 {
return nil
}
fileName := filePrefix + file.GeneratedFilenamePrefix + "_" + fileExt
genFile := gen.NewGeneratedFile(fileName, "")
request := &ExtFile{
Version: version,
PkgName: file.GoPackageName,
}
if file.Proto != nil && file.Proto.Options != nil {
if file.Proto.Options.GoPackage != nil {
request.GoPackage = *file.Proto.Options.GoPackage
}
if file.Proto.Options.JavaPackage != nil {
request.JavaPackage = *file.Proto.Options.JavaPackage
}
}
messages := file.Messages
for len(messages) != 0 {
var nestedMessages []*protogen.Message
for _, message := range messages {
s := &StructLike{
Name: message.GoIdent.GoName,
GoPackage: request.GoPackage,
JavaPackage: request.JavaPackage,
}
for _, field := range message.Fields {
filed := &Field{
Name: field.GoName,
}
s.Fields = append(s.Fields, filed)
}
request.StructLikes = append(request.StructLikes, s)
nestedMessages = append(nestedMessages, message.Messages...)
}
messages = nestedMessages
}

for _, svr := range file.Services {
s := &ExtService{
Name: svr.GoName,
}
request.Services = append(request.Services, s)
}

var buf strings.Builder
err := g.tpl.ExecuteTemplate(&buf, "structlikes", request)
if err != nil {
return err
}
genFile.P(buf.String())
return nil
}

// generateServiceExt: implements dubbo extension interface for service method args and response
// ref: https://github.com/cloudwego/kitex/blob/8526b3af30fcd321db268cae59a3545a9c6f237f/tool/internal_pkg/generator/generator.go#L392
func (g *generator) generateServiceExt(gen *protogen.Plugin, svr *protogen.Service, file *protogen.File) error {
fileName := filePrefix + string(file.GoImportPath) + "/" + strings.ToLower(svr.GoName) + "/" + fileExt
genFile := gen.NewGeneratedFile(fileName, "")
rSvr := &ExtService{
Version: version,
PkgName: strings.ToLower(svr.GoName),
Name: svr.GoName,
}
for _, method := range svr.Methods {
s := &Function{
Method: method.GoName,
InputType: method.Input.GoIdent.GoName,
OutputType: method.Output.GoIdent.GoName,
}
rSvr.Functions = append(rSvr.Functions, s)
}
var buf strings.Builder
err := g.tpl.ExecuteTemplate(&buf, "service", rSvr)
if err != nil {
return err
}
genFile.P(buf.String())
return nil
}
57 changes: 57 additions & 0 deletions tools/protoc-gen-kitex-dubbo/generator/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 CloudWeGo Authors
//
// 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.

package generator

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

type Field struct {
Name string
Type string
FunctionName string
}

type StructLike struct {
Name string
Fields []*Field
JavaPackage string
GoPackage string
}

type ExtService struct {
Name string
PkgName string
Functions []*Function
Annotations map[string]string
Version string
}

type Function struct {
Method string
InputType string
OutputType string
}

type ExtFile struct {
JavaPackage string
GoPackage string
PkgName protogen.GoPackageName
IDLName string
StructLikes []*StructLike
Services []*ExtService
Version string
}
54 changes: 54 additions & 0 deletions tools/protoc-gen-kitex-dubbo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module github.com/kitex-contrib/codec-dubbo/tools/protoc-gen-kitex-dubbo

go 1.19

require (
github.com/cloudwego/fastpb v0.0.4
github.com/cloudwego/kitex v0.9.1
github.com/cloudwego/thriftgo v0.3.10
github.com/kitex-contrib/codec-dubbo v0.2.5
github.com/pkg/errors v0.9.1
golang.org/x/text v0.13.0
google.golang.org/protobuf v1.33.0
)

require (
github.com/apache/dubbo-go-hessian2 v1.12.4 // indirect
github.com/apache/thrift v0.13.0 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.11.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/choleraehyq/pid v0.0.18 // indirect
github.com/cloudwego/configmanager v0.2.0 // indirect
github.com/cloudwego/dynamicgo v0.2.0 // indirect
github.com/cloudwego/frugal v0.1.14 // indirect
github.com/cloudwego/localsession v0.0.2 // indirect
github.com/cloudwego/netpoll v0.6.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dubbogo/gost v1.13.1 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oleiade/lane v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/tidwall/gjson v1.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20210513213006-bf773b8c8384 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading
Loading