fix protoc-gen-bm import bug

pull/298/head
felixhao 5 years ago
parent af3aaf073c
commit e28ff40df2
  1. 14
      example/protobuf/api.bm.go
  2. 2
      example/protobuf/api.proto
  3. 11
      tool/protobuf/protoc-gen-bm/generator/generator.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)
}

@ -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);
}

@ -125,19 +125,8 @@ 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()
t.P(`// to suppressed 'imported but not used warning'`)
t.P(`var _ *bm.Context`)

Loading…
Cancel
Save