From e28ff40df2c19bf4882fd7139be18f1338fb1f31 Mon Sep 17 00:00:00 2001 From: felixhao Date: Tue, 27 Aug 2019 00:41:47 +0800 Subject: [PATCH] fix protoc-gen-bm import bug --- example/protobuf/api.bm.go | 14 ++++++++++++++ example/protobuf/api.proto | 2 ++ tool/protobuf/protoc-gen-bm/generator/generator.go | 13 +------------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/example/protobuf/api.bm.go b/example/protobuf/api.bm.go index 4e29bad26..6e3ce12a7 100644 --- a/example/protobuf/api.bm.go +++ b/example/protobuf/api.bm.go @@ -9,6 +9,7 @@ import ( bm "github.com/bilibili/kratos/pkg/net/http/blademaster" "github.com/bilibili/kratos/pkg/net/http/blademaster/binding" ) +import google_protobuf1 "github.com/golang/protobuf/ptypes/empty" // to suppressed 'imported but not used warning' var _ *bm.Context @@ -16,10 +17,13 @@ var _ context.Context var _ binding.StructValidator var PathUserInfo = "/user.api.User/Info" +var PathUserCard = "/user.api.User/Card" // UserBMServer is the server API for User service. type UserBMServer interface { Info(ctx context.Context, req *UserReq) (resp *InfoReply, err error) + + Card(ctx context.Context, req *UserReq) (resp *google_protobuf1.Empty, err error) } var UserSvc UserBMServer @@ -33,8 +37,18 @@ func userInfo(c *bm.Context) { c.JSON(resp, err) } +func userCard(c *bm.Context) { + p := new(UserReq) + if err := c.BindWith(p, binding.Default(c.Request.Method, c.Request.Header.Get("Content-Type"))); err != nil { + return + } + resp, err := UserSvc.Card(c, p) + c.JSON(resp, err) +} + // RegisterUserBMServer Register the blademaster route func RegisterUserBMServer(e *bm.Engine, server UserBMServer) { UserSvc = server e.GET("/user.api.User/Info", userInfo) + e.GET("/user.api.User/Card", userCard) } diff --git a/example/protobuf/api.proto b/example/protobuf/api.proto index ef3d04abd..23f65d47d 100644 --- a/example/protobuf/api.proto +++ b/example/protobuf/api.proto @@ -3,6 +3,7 @@ syntax = "proto3"; package user.api; import "github.com/gogo/protobuf/gogoproto/gogo.proto"; +import "google/protobuf/empty.proto"; option go_package = "api"; @@ -30,4 +31,5 @@ message InfoReply { service User { rpc Info(UserReq) returns (InfoReply); + rpc Card(UserReq) returns (google.protobuf.Empty); } diff --git a/tool/protobuf/protoc-gen-bm/generator/generator.go b/tool/protobuf/protoc-gen-bm/generator/generator.go index ca9a06b50..19c325bfe 100644 --- a/tool/protobuf/protoc-gen-bm/generator/generator.go +++ b/tool/protobuf/protoc-gen-bm/generator/generator.go @@ -125,18 +125,7 @@ func (t *bm) generateImports(file *descriptor.FileDescriptorProto) { deps := make(map[string]string) // Map of package name to quoted import path. deps = t.DeduceDeps(file) for pkg, importPath := range deps { - for _, service := range file.Service { - for _, method := range service.Method { - inputType := t.GoTypeName(method.GetInputType()) - outputType := t.GoTypeName(method.GetOutputType()) - if strings.HasPrefix(pkg, outputType) || strings.HasPrefix(pkg, inputType) { - t.P(`import `, pkg, ` `, importPath) - } - } - } - } - if len(deps) > 0 { - t.P() + t.P(`import `, pkg, ` `, importPath) } t.P() t.P(`// to suppressed 'imported but not used warning'`)