1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00

fix protoc-gen-bm import bug

This commit is contained in:
felixhao 2019-08-27 00:41:47 +08:00
parent af3aaf073c
commit e28ff40df2
3 changed files with 17 additions and 12 deletions

View File

@ -9,6 +9,7 @@ import (
bm "github.com/bilibili/kratos/pkg/net/http/blademaster" bm "github.com/bilibili/kratos/pkg/net/http/blademaster"
"github.com/bilibili/kratos/pkg/net/http/blademaster/binding" "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' // to suppressed 'imported but not used warning'
var _ *bm.Context var _ *bm.Context
@ -16,10 +17,13 @@ var _ context.Context
var _ binding.StructValidator var _ binding.StructValidator
var PathUserInfo = "/user.api.User/Info" var PathUserInfo = "/user.api.User/Info"
var PathUserCard = "/user.api.User/Card"
// UserBMServer is the server API for User service. // UserBMServer is the server API for User service.
type UserBMServer interface { type UserBMServer interface {
Info(ctx context.Context, req *UserReq) (resp *InfoReply, err error) 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 var UserSvc UserBMServer
@ -33,8 +37,18 @@ func userInfo(c *bm.Context) {
c.JSON(resp, err) 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 // RegisterUserBMServer Register the blademaster route
func RegisterUserBMServer(e *bm.Engine, server UserBMServer) { func RegisterUserBMServer(e *bm.Engine, server UserBMServer) {
UserSvc = server UserSvc = server
e.GET("/user.api.User/Info", userInfo) e.GET("/user.api.User/Info", userInfo)
e.GET("/user.api.User/Card", userCard)
} }

View File

@ -3,6 +3,7 @@ syntax = "proto3";
package user.api; package user.api;
import "github.com/gogo/protobuf/gogoproto/gogo.proto"; import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/protobuf/empty.proto";
option go_package = "api"; option go_package = "api";
@ -30,4 +31,5 @@ message InfoReply {
service User { service User {
rpc Info(UserReq) returns (InfoReply); rpc Info(UserReq) returns (InfoReply);
rpc Card(UserReq) returns (google.protobuf.Empty);
} }

View File

@ -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 := make(map[string]string) // Map of package name to quoted import path.
deps = t.DeduceDeps(file) deps = t.DeduceDeps(file)
for pkg, importPath := range deps { for pkg, importPath := range deps {
for _, service := range file.Service { t.P(`import `, pkg, ` `, importPath)
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() t.P()
t.P(`// to suppressed 'imported but not used warning'`) t.P(`// to suppressed 'imported but not used warning'`)