Skip to content

Commit 922f04d

Browse files
committed
feat(gen): 生成 mq 进行校验,如果没有使用 mq 的 proto 不生成 mq
1 parent 1b272e5 commit 922f04d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

protoc-gen-mq/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ func main() {
1818
return
1919
}
2020

21-
var flags flag.FlagSet
2221
options := protogen.Options{
23-
ParamFunc: flags.Set,
22+
ParamFunc: flag.CommandLine.Set,
2423
}
2524
options.Run(func(gen *protogen.Plugin) error {
2625
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)

protoc-gen-mq/template.go

+20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/Ccheers/kratos-mq/protoc-gen-mq/proto/kmq/v1/options"
67
"google.golang.org/protobuf/compiler/protogen"
8+
"google.golang.org/protobuf/proto"
79
"google.golang.org/protobuf/types/descriptorpb"
810
)
911

@@ -22,6 +24,9 @@ var methodSets = make(map[string]int)
2224

2325
// generateFile generates a _gin.pb.go file.
2426
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
27+
if len(file.Services) == 0 || !hasKMQRule(file.Services) {
28+
return nil
29+
}
2530
if len(file.Services) == 0 {
2631
return nil
2732
}
@@ -197,3 +202,18 @@ func generateClientMethodList(g *protogen.GeneratedFile, s *service) {
197202
g.P("}")
198203
}
199204
}
205+
206+
func hasKMQRule(services []*protogen.Service) bool {
207+
for _, service := range services {
208+
for _, method := range service.Methods {
209+
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
210+
continue
211+
}
212+
rule, ok := proto.GetExtension(method.Desc.Options(), options.E_Mq).(*options.MQ)
213+
if rule != nil && ok {
214+
return true
215+
}
216+
}
217+
}
218+
return false
219+
}

0 commit comments

Comments
 (0)