Skip to content

Commit

Permalink
chore(tool): simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost committed Jan 21, 2025
1 parent 1de9e03 commit 0196782
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 52 deletions.
12 changes: 6 additions & 6 deletions tool/internal_pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func (g *generator) setImports(name string, pkg *PackageInfo) {
if g.StreamX || (!m.ServerStreaming && !m.ClientStreaming) {
pkg.AddImports("context")
}
if g.StreamX && m.Streaming.IsStreaming {
if g.StreamX && m.IsStreaming() {
pkg.AddImports("github.com/cloudwego/kitex/pkg/streamx")
}
for _, a := range m.Args {
Expand All @@ -587,7 +587,7 @@ func (g *generator) setImports(name string, pkg *PackageInfo) {
// for StreamX, if there is streaming method, generate Server Interface in server.go
if g.StreamX {
for _, method := range pkg.AllMethods() {
if method.Streaming.IsStreaming {
if method.IsStreaming() {
pkg.AddImports("context")
pkg.AddImports("github.com/cloudwego/kitex/pkg/streamx")
}
Expand Down Expand Up @@ -619,15 +619,15 @@ func (g *generator) setImports(name string, pkg *PackageInfo) {
}
// streaming imports
if !g.StreamX {
if m.Streaming.IsStreaming || pkg.Codec == "protobuf" {
if m.IsStreaming() || pkg.Codec == "protobuf" {
// protobuf handler support both PingPong and Unary (streaming) requests
pkg.AddImport("streaming", "github.com/cloudwego/kitex/pkg/streaming")
}
if m.ClientStreaming || m.ServerStreaming {
if m.IsStreaming() {
pkg.AddImports("fmt")
}
} else {
if m.Streaming.IsStreaming {
if m.IsStreaming() {
pkg.AddImports("github.com/cloudwego/kitex/client/streamxclient")
pkg.AddImports("github.com/cloudwego/kitex/client/streamxclient/streamxcallopt")
pkg.AddImports("github.com/cloudwego/kitex/pkg/streamx")
Expand Down Expand Up @@ -671,7 +671,7 @@ func needCallOpt(pkg *PackageInfo) bool {
switch pkg.Codec {
case "thrift":
for _, m := range pkg.ServiceInfo.AllMethods() {
if !m.Streaming.IsStreaming {
if !m.IsStreaming() {
needCallOpt = true
break
}
Expand Down
18 changes: 3 additions & 15 deletions tool/internal_pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"testing"
"time"

"github.com/cloudwego/thriftgo/generator/golang/streaming"

"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/tool/internal_pkg/util"
)
Expand Down Expand Up @@ -202,9 +200,7 @@ func Test_needCallOpt(t *testing.T) {
Codec: "thrift",
ServiceInfo: &ServiceInfo{
Methods: []*MethodInfo{{
Streaming: &streaming.Streaming{
IsStreaming: true,
},
ServerStreaming: true,
}},
},
}
Expand All @@ -215,16 +211,8 @@ func Test_needCallOpt(t *testing.T) {
Codec: "thrift",
ServiceInfo: &ServiceInfo{
Methods: []*MethodInfo{
{
Streaming: &streaming.Streaming{
IsStreaming: true,
},
},
{
Streaming: &streaming.Streaming{
IsStreaming: false,
},
},
{ServerStreaming: true},
{},
},
},
}
Expand Down
5 changes: 2 additions & 3 deletions tool/internal_pkg/generator/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"strings"
"text/template"

"github.com/cloudwego/thriftgo/generator/golang/streaming"

"github.com/cloudwego/kitex/tool/internal_pkg/util"
"github.com/cloudwego/kitex/transport"
)
Expand Down Expand Up @@ -213,10 +211,11 @@ type MethodInfo struct {
GenArgResultStruct bool
ClientStreaming bool
ServerStreaming bool
Streaming *streaming.Streaming
StreamX bool
}

func (m *MethodInfo) IsStreaming() bool { return m.ClientStreaming || m.ServerStreaming }

// Parameter .
type Parameter struct {
Deps []PkgInfo
Expand Down
27 changes: 0 additions & 27 deletions tool/internal_pkg/pluginmode/protoc/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"text/template"

genfastpb "github.com/cloudwego/fastpb/protoc-gen-fastpb/generator"
"github.com/cloudwego/thriftgo/generator/golang/streaming"
gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
"google.golang.org/protobuf/compiler/protogen"

Expand Down Expand Up @@ -301,9 +300,6 @@ func (pp *protocPlugin) convertTypes(file *protogen.File) (ss []*generator.Servi
si.HasStreaming = true
}
}
for _, m := range si.Methods {
BuildStreaming(m, si.HasStreaming)
}
if file.Generate {
si.GenerateHandler = true
}
Expand Down Expand Up @@ -348,29 +344,6 @@ func (pp *protocPlugin) convertTypes(file *protogen.File) (ss []*generator.Servi
return
}

// BuildStreaming builds protobuf MethodInfo.Streaming as for Thrift, to simplify codegen
func BuildStreaming(mi *generator.MethodInfo, serviceHasStreaming bool) {
s := &streaming.Streaming{
// pb: if one method is streaming, then the service is streaming, making all methods streaming
IsStreaming: serviceHasStreaming,
}
if mi.ClientStreaming && mi.ServerStreaming {
s.Mode = streaming.StreamingBidirectional
s.BidirectionalStreaming = true
s.ClientStreaming = true
s.ServerStreaming = true
} else if mi.ClientStreaming && !mi.ServerStreaming {
s.Mode = streaming.StreamingClientSide
s.ClientStreaming = true
} else if !mi.ClientStreaming && mi.ServerStreaming {
s.Mode = streaming.StreamingServerSide
s.ServerStreaming = true
} else if serviceHasStreaming {
s.Mode = streaming.StreamingUnary // Unary APIs over HTTP2
}
mi.Streaming = s
}

func (pp *protocPlugin) getCombineServiceName(name string, svcs []*generator.ServiceInfo) string {
for _, svc := range svcs {
if svc.ServiceName == name {
Expand Down
1 change: 0 additions & 1 deletion tool/internal_pkg/pluginmode/thriftgo/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ func (c *converter) makeMethod(si *generator.ServiceInfo, f *golang.Function) (*
Void: f.Void,
ArgStructName: f.ArgType().GoName().String(),
GenArgResultStruct: false,
Streaming: st,
ClientStreaming: st.ClientStreaming,
ServerStreaming: st.ServerStreaming,
ArgsLength: len(f.Arguments()),
Expand Down

0 comments on commit 0196782

Please sign in to comment.