use helper to detect import paths

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-23 23:54:53 +03:00
parent 8d4aac8b8b
commit 6b8fa89a69
6 changed files with 176 additions and 131 deletions

43
chi.go
View File

@ -23,52 +23,49 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
chiPackageFiles[file.GoPackageName] = struct{}{} chiPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go" gname := "micro" + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro") gfile.P("// Code generated by protoc-gen-micro")
gfile.P("package ", file.GoPackageName) gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.P() gfile.Import(contextPackage)
gfile.P("import (") gfile.Import(fmtPackage)
gfile.P(`"context"`) gfile.Import(httpPackage)
gfile.P(`"fmt"`) gfile.Import(reflectPackage)
gfile.P(`"net/http"`) gfile.Import(stringsPackage)
gfile.P(`"reflect"`) gfile.Import(chiPackage)
gfile.P(`"strings"`) gfile.Import(chiMiddlewarePackage)
gfile.P(`chi "github.com/go-chi/chi/v4"`) gfile.Import(microApiPackage)
gfile.P(`middleware "github.com/go-chi/chi/v4/middleware"`)
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
gfile.P(")")
gfile.P()
gfile.P("type routeKey struct{}") gfile.P("type routeKey struct{}")
gfile.P("func RouteName(ctx context.Context) (string, bool) {") gfile.P("func RouteName(ctx ", contextPackage.Ident("Context"), ") (string, bool) {")
gfile.P("value, ok := ctx.Value(routeKey{}).(string)") gfile.P("value, ok := ctx.Value(routeKey{}).(string)")
gfile.P("return value, ok") gfile.P("return value, ok")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func RegisterHandlers(r *chi.Mux, h interface{}, eps []*micro_api.Endpoint) error {") gfile.P("func RegisterHandlers(r *", chiPackage.Ident("Mux"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {")
gfile.P("v := reflect.ValueOf(h)") gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)")
gfile.P("if v.NumMethod() < 1 {") gfile.P("if v.NumMethod() < 1 {")
gfile.P(`return fmt.Errorf("handler has no methods: %T", h)`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`)
gfile.P("}") gfile.P("}")
gfile.P("for _, ep := range eps {") gfile.P("for _, ep := range eps {")
gfile.P(`idx := strings.Index(ep.Name, ".")`) gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`)
gfile.P("if idx < 1 || len(ep.Name) <= idx {") gfile.P("if idx < 1 || len(ep.Name) <= idx {")
gfile.P(`return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`)
gfile.P("}") gfile.P("}")
gfile.P("name := ep.Name[idx+1:]") gfile.P("name := ep.Name[idx+1:]")
gfile.P("m := v.MethodByName(name)") gfile.P("m := v.MethodByName(name)")
gfile.P("if !m.IsValid() || m.IsZero() {") gfile.P("if !m.IsValid() || m.IsZero() {")
gfile.P(`return fmt.Errorf("invalid handler, method %s not found", name)`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`)
gfile.P("}") gfile.P("}")
gfile.P("rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))") gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))")
gfile.P("if !ok {") gfile.P("if !ok {")
gfile.P(`return fmt.Errorf("invalid handler: %#+v", m.Interface())`) gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`)
gfile.P("}") gfile.P("}")
gfile.P("for _, method := range ep.Method {") gfile.P("for _, method := range ep.Method {")
gfile.P("r.With(middleware.WithValue(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)") gfile.P("r.With(", chiMiddlewarePackage.Ident("WithValue"), "(routeKey{}, ep.Name)).MethodFunc(method, ep.Path[0], rh)")
gfile.P("}") gfile.P("}")
gfile.P("}") gfile.P("}")
gfile.P("return nil") gfile.P("return nil")

View File

@ -23,22 +23,18 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
gorillaPackageFiles[file.GoPackageName] = struct{}{} gorillaPackageFiles[file.GoPackageName] = struct{}{}
gname := "micro" + "_" + component + ".pb.go" gname := "micro" + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro") gfile.P("// Code generated by protoc-gen-micro")
gfile.P("package ", file.GoPackageName) gfile.P("package ", file.GoPackageName)
gfile.P() gfile.P()
gfile.P("import (") gfile.Import(fmtPackage)
gfile.P(`"fmt"`) gfile.Import(httpPackage)
gfile.P(`"net/http"`) gfile.Import(reflectPackage)
gfile.P(`"reflect"`) gfile.Import(stringsPackage)
gfile.P(`"strings"`) gfile.Import(microApiPackage)
gfile.P() gfile.Import(gorillaMuxPackage)
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
gfile.P(`mux "github.com/gorilla/mux"`)
gfile.P(")")
gfile.P()
gfile.P("func RegisterHandlers(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {") gfile.P("func RegisterHandlers(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {")
gfile.P("v := reflect.ValueOf(h)") gfile.P("v := reflect.ValueOf(h)")

13
http.go
View File

@ -25,7 +25,7 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
} }
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go" gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro") gfile.P("// Code generated by protoc-gen-micro")
gfile.P("// source: ", *file.Proto.Name) gfile.P("// source: ", *file.Proto.Name)
@ -87,9 +87,14 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
} }
} }
for file, err := range files { for _, file := range plugin.Files {
gfile := plugin.NewGeneratedFile(file+"_micro_errors.pb.go", ".") for efile, err := range files {
generateServiceErrors(gfile, err) if file.GeneratedFilenamePrefix != efile {
continue
}
gfile := plugin.NewGeneratedFile(efile+"_micro_errors.pb.go", file.GoImportPath)
generateServiceErrors(gfile, err)
}
} }
return nil return nil

View File

@ -14,28 +14,16 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
} }
gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go" gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro") gfile.P("// Code generated by protoc-gen-micro")
gfile.P("// source: ", *file.Proto.Name) gfile.P("// source: ", *file.Proto.Name)
gfile.P("package ", file.GoPackageName) gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.P("import (")
gfile.P(`"context"`)
gfile.P()
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
gfile.P(`micro_client "github.com/unistack-org/micro/v3/client"`)
gfile.P(")")
gfile.P() gfile.P()
gfile.P("// Reference imports to suppress errors if they are not otherwise used.") gfile.Import(contextPackage)
gfile.P("var (") gfile.Import(microApiPackage)
gfile.P("_ ", "micro_api.Endpoint") gfile.Import(microClientPackage)
gfile.P("_ ", "context.Context")
gfile.P(" _ ", "micro_client.Option")
gfile.P(")")
gfile.P()
// generate services // generate services
for _, service := range file.Services { for _, service := range file.Services {

2
rpc.go
View File

@ -14,7 +14,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error
} }
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go" gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
gfile := plugin.NewGeneratedFile(gname, ".") gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
gfile.P("// Code generated by protoc-gen-micro") gfile.P("// Code generated by protoc-gen-micro")
gfile.P("// source: ", *file.Proto.Name) gfile.P("// source: ", *file.Proto.Name)

211
util.go
View File

@ -10,20 +10,36 @@ import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
) )
func lowerFirst(s string) string { var (
reflectPackage = protogen.GoImportPath("reflect")
stringsPackage = protogen.GoImportPath("strings")
fmtPackage = protogen.GoImportPath("fmt")
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v4")
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v4/middleware")
microApiPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/api")
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
deprecationComment = "// Deprecated: Do not use."
)
func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:] return strings.ToLower(s[:1]) + s[1:]
} }
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", lowerFirst(serviceName), "Service struct {") gfile.P("type ", unexport(serviceName), "Service struct {")
gfile.P("c micro_client.Client") gfile.P("c ", microClientPackage.Ident("Client"))
gfile.P("name string") gfile.P("name string")
gfile.P("}") gfile.P("}")
gfile.P("// New", serviceName, "Service create new service client") gfile.P("// New", serviceName, "Service create new service client")
gfile.P("func New", serviceName, "Service(name string, c micro_client.Client) ", serviceName, "Service {") gfile.P("func New", serviceName, "Service(name string, c micro_client.Client) ", serviceName, "Service {")
gfile.P("return &", lowerFirst(serviceName), "Service{c: c, name: name}") gfile.P("return &", unexport(serviceName), "Service{c: c, name: name}")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
} }
@ -32,7 +48,7 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
for _, method := range service.Methods { for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName) methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
gfile.P("func (c *", lowerFirst(serviceName), "Service) ", generateClientSignature(serviceName, method), "{") generateClientFuncSignature(gfile, serviceName, method)
if http && method.Desc.Options() != nil { if http && method.Desc.Options() != nil {
if proto.HasExtension(method.Desc.Options(), openapi_options.E_Openapiv2Operation) { if proto.HasExtension(method.Desc.Options(), openapi_options.E_Openapiv2Operation) {
@ -41,14 +57,13 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
r := opts.(*openapi_options.Operation) r := opts.(*openapi_options.Operation)
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses), ")") gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses), ")")
for code, response := range r.Responses { for code, response := range r.Responses {
if response.Schema == nil || response.Schema.JsonSchema == nil { if response.Schema != nil && response.Schema.JsonSchema != nil {
continue ref := response.Schema.JsonSchema.Ref
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
}
gfile.P(`errmap["`, code, `"] = &`, ref, "{}")
} }
ref := response.Schema.JsonSchema.Ref
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
}
gfile.P(`errmap["`, code, `"] = &`, ref, "{}")
} }
} }
@ -76,7 +91,7 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("return rsp, nil") gfile.P("return rsp, nil")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
return continue
} }
gfile.P(`stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "`, methodName, `", &`, method.Input.Desc.Name(), `{}), opts...)`) gfile.P(`stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "`, methodName, `", &`, method.Input.Desc.Name(), `{}), opts...)`)
@ -89,18 +104,18 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("return nil, err") gfile.P("return nil, err")
gfile.P("}") gfile.P("}")
} }
gfile.P("return &", lowerFirst(serviceName), "Service", method.GoName, "{stream}, nil") gfile.P("return &", unexport(serviceName), "Service", method.GoName, "{stream}, nil")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() { if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
gfile.P("type ", lowerFirst(serviceName), "Service", method.GoName, " struct {") gfile.P("type ", unexport(serviceName), "Service", method.GoName, " struct {")
gfile.P("stream micro_client.Stream") gfile.P("stream micro_client.Stream")
gfile.P("}") gfile.P("}")
} }
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") RecvAndClose() (*", method.Output.Desc.Name(), ", error) {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") RecvAndClose() (*", method.Output.Desc.Name(), ", error) {")
gfile.P("m := &", method.Output.Desc.Name(), "{}") gfile.P("m := &", method.Output.Desc.Name(), "{}")
gfile.P("err := s.RecvMsg(m)") gfile.P("err := s.RecvMsg(m)")
gfile.P("if err == nil {") gfile.P("if err == nil {")
@ -114,32 +129,32 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
} }
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") Close() error {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") Close() error {")
gfile.P("return s.stream.Close()") gfile.P("return s.stream.Close()")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") Context() context.Context {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") Context() context.Context {")
gfile.P("return s.stream.Context()") gfile.P("return s.stream.Context()")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") SendMsg(m interface{}) error {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") SendMsg(m interface{}) error {")
gfile.P("return s.stream.Send(m)") gfile.P("return s.stream.Send(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") RecvMsg(m interface{}) error {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") RecvMsg(m interface{}) error {")
gfile.P("return s.stream.Recv(m)") gfile.P("return s.stream.Recv(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
if method.Desc.IsStreamingClient() { if method.Desc.IsStreamingClient() {
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") Send(m *", method.Input.Desc.Name(), ") error {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") Send(m *", method.Input.Desc.Name(), ") error {")
gfile.P("return s.stream.Send(m)") gfile.P("return s.stream.Send(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
} }
if method.Desc.IsStreamingServer() { if method.Desc.IsStreamingServer() {
gfile.P("func (s *", lowerFirst(serviceName), "Service", method.GoName, ") Recv() (*", method.Output.Desc.Name(), ", error) {") gfile.P("func (s *", unexport(serviceName), "Service", method.GoName, ") Recv() (*", method.Output.Desc.Name(), ", error) {")
gfile.P("m := &", method.Output.Desc.Name(), "{}") gfile.P("m := &", method.Output.Desc.Name(), "{}")
gfile.P("if err := s.stream.Recv(m); err != nil {") gfile.P("if err := s.stream.Recv(m); err != nil {")
gfile.P("return nil, err") gfile.P("return nil, err")
@ -153,7 +168,7 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
func generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", lowerFirst(serviceName), "Handler struct {") gfile.P("type ", unexport(serviceName), "Handler struct {")
gfile.P(serviceName, "Handler") gfile.P(serviceName, "Handler")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
@ -162,12 +177,7 @@ func generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Serv
func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
for _, method := range service.Methods { for _, method := range service.Methods {
//methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName) generateServerFuncSignature(gfile, serviceName, method)
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
gfile.P("func (h *", lowerFirst(serviceName), "Handler) ", method.GoName, "(ctx context.Context, stream micro_server.Stream) error {")
} else {
gfile.P("func (h *", lowerFirst(serviceName), "Handler) ", generateServerSignature(serviceName, method), "{")
}
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
if !method.Desc.IsStreamingClient() { if !method.Desc.IsStreamingClient() {
@ -175,9 +185,9 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("if err := stream.Recv(m); err != nil {") gfile.P("if err := stream.Recv(m); err != nil {")
gfile.P("return err") gfile.P("return err")
gfile.P("}") gfile.P("}")
gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, m, &", lowerFirst(serviceName), method.GoName, "Stream{stream})") gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, m, &", unexport(serviceName), method.GoName, "Stream{stream})")
} else { } else {
gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, &", lowerFirst(serviceName), method.GoName, "Stream{stream})") gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, &", unexport(serviceName), method.GoName, "Stream{stream})")
} }
} else { } else {
gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, req, rsp)") gfile.P("return h.", serviceName, "Handler.", method.GoName, "(ctx, req, rsp)")
@ -186,13 +196,13 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P() gfile.P()
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
gfile.P("type ", lowerFirst(serviceName), method.GoName, "Stream struct {") gfile.P("type ", unexport(serviceName), method.GoName, "Stream struct {")
gfile.P("stream micro_server.Stream") gfile.P("stream micro_server.Stream")
gfile.P("}") gfile.P("}")
} }
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) SendAndClose(m *", method.Output.Desc.Name(), ") error {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendAndClose(m *", method.Output.Desc.Name(), ") error {")
gfile.P("err := s.SendMsg(m)") gfile.P("err := s.SendMsg(m)")
gfile.P("if err == nil {") gfile.P("if err == nil {")
gfile.P("err = s.stream.Close()") gfile.P("err = s.stream.Close()")
@ -202,35 +212,35 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
} }
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) Close() error {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Close() error {")
gfile.P("return s.stream.Close()") gfile.P("return s.stream.Close()")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) Context() context.Context {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Context() context.Context {")
gfile.P("return s.stream.Context()") gfile.P("return s.stream.Context()")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) SendMsg(m interface{}) error {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendMsg(m interface{}) error {")
gfile.P("return s.stream.Send(m)") gfile.P("return s.stream.Send(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) RecvMsg(m interface{}) error {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) RecvMsg(m interface{}) error {")
gfile.P("return s.stream.Recv(m)") gfile.P("return s.stream.Recv(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
if method.Desc.IsStreamingServer() { if method.Desc.IsStreamingServer() {
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) Send(m *", method.Output.Desc.Name(), ") error {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Send(m *", method.Output.Desc.Name(), ") error {")
gfile.P("return s.stream.Send(m)") gfile.P("return s.stream.Send(m)")
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
} }
if method.Desc.IsStreamingClient() { if method.Desc.IsStreamingClient() {
gfile.P("func (s *", lowerFirst(serviceName), method.GoName, "Stream) Recv() (*", method.Input.Desc.Name(), ", error) {") gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Recv() (*", method.Input.Desc.Name(), ", error) {")
gfile.P("m := &", method.Input.Desc.Name(), "{}") gfile.P("m := &", method.Input.Desc.Name(), "{}")
gfile.P("if err := s.stream.Recv(m); err != nil {") gfile.P("if err := s.stream.Recv(m); err != nil {")
gfile.P("return nil, err") gfile.P("return nil, err")
@ -247,61 +257,110 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("func Register", serviceName, "Handler(s micro_server.Server, sh ", serviceName, "Handler, opts ...micro_server.HandlerOption) error {") gfile.P("func Register", serviceName, "Handler(s micro_server.Server, sh ", serviceName, "Handler, opts ...micro_server.HandlerOption) error {")
gfile.P("type ", lowerFirst(serviceName), " interface {") gfile.P("type ", unexport(serviceName), " interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { generateServerSignature(gfile, serviceName, method)
gfile.P(method.GoName, "(ctx context.Context, stream micro_server.Stream) error")
} else {
gfile.P(generateServerSignature(serviceName, method))
}
} }
gfile.P("}") gfile.P("}")
gfile.P("type ", serviceName, " struct {") gfile.P("type ", serviceName, " struct {")
gfile.P(lowerFirst(serviceName)) gfile.P(unexport(serviceName))
gfile.P("}") gfile.P("}")
gfile.P("h := &", lowerFirst(serviceName), "Handler{sh}") gfile.P("h := &", unexport(serviceName), "Handler{sh}")
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {") gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
gfile.P("opts = append(opts, micro_api.WithEndpoint(endpoint))") gfile.P("opts = append(opts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
gfile.P("}") gfile.P("}")
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, opts...))") gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, opts...))")
gfile.P("}") gfile.P("}")
} }
func generateServerSignature(serviceName string, method *protogen.Method) string { func generateServerFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
methodName := string(method.GoName) args := append([]interface{}{},
req := []string{"ctx context.Context"} "func (h *", unexport(serviceName), "Handler) ", method.GoName,
ret := "error" "(ctx ", contextPackage.Ident("Context"),
)
if !method.Desc.IsStreamingClient() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
req = append(req, "req *"+string(method.Input.Desc.Name())) args = append(args, ", stream ", microServerPackage.Ident("Stream"))
} else {
if !method.Desc.IsStreamingClient() {
args = append(args, ", req *", gfile.QualifiedGoIdent(method.Input.GoIdent))
}
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
args = append(args, ", stream ", serviceName, "_", method.GoName, "Stream")
}
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, ", rsp *", gfile.QualifiedGoIdent(method.Output.GoIdent))
}
} }
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() { args = append(args, ") error {")
req = append(req, "stream "+serviceName+"_"+methodName+"Stream") gfile.P(args...)
}
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
req = append(req, "rsp *"+string(method.Output.Desc.Name()))
}
return methodName + "(" + strings.Join(req, ", ") + ") " + ret
} }
func generateClientSignature(serviceName string, method *protogen.Method) string { func generateServerSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
methodName := string(method.GoName) args := append([]interface{}{},
req := ", req *" + string(method.Input.Desc.Name()) method.GoName,
if method.Desc.IsStreamingClient() { "(ctx ", contextPackage.Ident("Context"),
req = "" )
}
rsp := "*" + string(method.Output.Desc.Name())
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
rsp = serviceName + "_" + methodName + "Service" args = append(args, ", stream ", microServerPackage.Ident("Stream"))
} else {
if !method.Desc.IsStreamingClient() {
args = append(args, ", req *", gfile.QualifiedGoIdent(method.Input.GoIdent))
}
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
args = append(args, ", stream ", serviceName, "_", method.GoName, "Stream")
}
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, ", rsp *", gfile.QualifiedGoIdent(method.Output.GoIdent))
}
} }
return fmt.Sprintf("%s(ctx context.Context%s, opts ...micro_client.CallOption) (%s, error)", methodName, req, rsp) args = append(args, ") error")
gfile.P(args...)
}
func generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
args := append([]interface{}{},
"func (c *",
unexport(serviceName),
"Service) ",
method.GoName,
"(ctx ", contextPackage.Ident("Context"), ", ",
)
if !method.Desc.IsStreamingClient() {
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
}
args = append(args, "opts ...", microClientPackage.Ident("CallOption"), ") (")
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
args = append(args, serviceName, "_", method.GoName, "Service")
}
args = append(args, ", error) {")
gfile.P(args...)
}
func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
args := append([]interface{}{},
method.GoName,
"(ctx ", contextPackage.Ident("Context"), ", ",
)
if !method.Desc.IsStreamingClient() {
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
}
args = append(args, "opts ...", microClientPackage.Ident("CallOption"), ") (")
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
args = append(args, serviceName, "_", method.GoName, "Service")
}
args = append(args, ", error)")
gfile.P(args...)
} }
func generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", serviceName, "Service interface {") gfile.P("type ", serviceName, "Service interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
gfile.P(generateClientSignature(serviceName, method)) generateClientSignature(gfile, serviceName, method)
} }
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
@ -311,7 +370,7 @@ func generateServiceServerInterface(gfile *protogen.GeneratedFile, service *prot
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("type ", serviceName, "Handler interface {") gfile.P("type ", serviceName, "Handler interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
gfile.P(generateServerSignature(serviceName, method)) generateServerSignature(gfile, serviceName, method)
} }
gfile.P("}") gfile.P("}")
gfile.P() gfile.P()
@ -372,8 +431,8 @@ func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service
func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) { func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := strings.TrimSuffix(service.GoName, "Service") serviceName := strings.TrimSuffix(service.GoName, "Service")
gfile.P("// New", serviceName, "Endpoints provides api endpoints metdata for ", serviceName, " service") gfile.P("// New", serviceName, "Endpoints provides api endpoints metdata for ", serviceName, " service")
gfile.P("func New", serviceName, "Endpoints() []*micro_api.Endpoint {") gfile.P("func New", serviceName, "Endpoints() []*", microApiPackage.Ident("Endpoint"), " {")
gfile.P("return []*", "micro_api.Endpoint{") gfile.P("return []*", microApiPackage.Ident("Endpoint"), "{")
for _, method := range service.Methods { for _, method := range service.Methods {
if method.Desc.Options() == nil { if method.Desc.Options() == nil {
continue continue
@ -381,7 +440,7 @@ func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.S
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) { if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
endpoints, streaming := generateEndpoints(method) endpoints, streaming := generateEndpoints(method)
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
gfile.P("&", "micro_api.Endpoint{") gfile.P("&", microApiPackage.Ident("Endpoint"), "{")
generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming) generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming)
gfile.P("},") gfile.P("},")
} }