Skip to content

Commit

Permalink
proxy: support standalone mode
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Aug 11, 2022
1 parent 686825f commit d770dbe
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proxy

import (
"fmt"
"path/filepath"
"sort"

"github.com/iancoleman/strcase"
Expand All @@ -22,6 +23,8 @@ const (
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
)

var pbPackage protogen.GoImportPath

// FileNameSuffix is the suffix added to files generated by deepcopy
const FileNameSuffix = "_proxy.pb.go"

Expand All @@ -31,39 +34,63 @@ type Config struct {
Out string
}

// GenerateFile generates DeepCopyInto() and DeepCopy() functions for .pb.go types.
// GenerateFile generates RPC service proxy from .pb.go types.
func GenerateFile(p *protogen.Plugin, f *protogen.File, cfg *Config) *protogen.GeneratedFile {
if len(f.Services) == 0 {
return nil
}

filename := f.GeneratedFilenamePrefix + FileNameSuffix
goImportPath := f.GoImportPath

if cfg.Standalone && cfg.Out != "" {
goImportPath = protogen.GoImportPath(cfg.Out)
}
g := p.NewGeneratedFile(filename, goImportPath)

g.QualifiedGoIdent(contextPackage.Ident(""))
g.QualifiedGoIdent(errorsPackage.Ident(""))
g.QualifiedGoIdent(netPackage.Ident(""))
g.QualifiedGoIdent(grpcPackage.Ident(""))
g.QualifiedGoIdent(emptyPackage.Ident(""))
g.QualifiedGoIdent(contextPackage.Ident("Context"))
g.QualifiedGoIdent(errorsPackage.Ident("New"))
g.QualifiedGoIdent(netPackage.Ident("Addr"))
g.QualifiedGoIdent(grpcPackage.Ident("CallOption"))
g.QualifiedGoIdent(emptyPackage.Ident("Empty"))
if cfg.Standalone {
pbPackage = protogen.GoImportPath(f.GoImportPath)
g.QualifiedGoIdent(pbPackage.Ident(""))
}

g.P(`// Code generated by protoc-gen-proxy. DO NOT EDIT.`)
g.P()
g.P(`package `, f.GoPackageName)
g.P()

g.P()
g.P(`var _ `, emptyPackage.Ident("Empty"))
g.P()
for _, service := range f.Services {
serviceName := service.GoName
lowerServiceName := strcase.ToLowerCamel(serviceName)
serverName := lowerServiceName + "Server"

methods := make(map[string]string)
idStr := pbPackage.String()
importPath := filepath.Base(idStr[1 : len(idStr)-1])
registerServerName := fmt.Sprintf("Register%sServer", serviceName)
if cfg.Standalone {
registerServerName = importPath + "." + registerServerName
}

for _, method := range service.Methods {
input := method.Input.GoIdent.GoName
output := method.Output.GoIdent.GoName

if cfg.Standalone {
// handle emptypb
if input != "Empty" {
input = importPath + "." + input
}
if output != "Empty" {
output = importPath + "." + output
}
}

// handle emptypb
if input == "Empty" {
input = "emptypb.Empty"
Expand All @@ -72,7 +99,11 @@ func GenerateFile(p *protogen.Plugin, f *protogen.File, cfg *Config) *protogen.G
output = "emptypb.Empty"
}

methods[method.GoName] = fmt.Sprintf(`(ctx context.Context, req *%s) (*%s, error)`, input, output)
args := fmt.Sprintf(`(ctx context.Context, req *%s) (*%s, error)`, input, output)
if cfg.Standalone && method.Desc.IsStreamingServer() {
args = fmt.Sprintf(`(req *%s, srv %s_%sServer)`, input, importPath+"."+serviceName, method.GoName)
}
methods[method.GoName] = args
}

// sort RPC methods
Expand Down Expand Up @@ -114,7 +145,7 @@ func GenerateFile(p *protogen.Plugin, f *protogen.File, cfg *Config) *protogen.G
g.P(`// Serve starts serving the proxy server on the given listener with the specified options.`)
g.P(`func (p *Proxy) Serve(l net.Listener, opts ...grpc.ServerOption) error {`)
g.P(` srv := grpc.NewServer(opts...)`)
g.P(` Register`, serviceName, `Server(srv, &`, serverName, `{proxy: p})`)
g.P(` `, registerServerName, `(srv, &`, serverName, `{proxy: p})`)
g.P()
g.P(` return srv.Serve(l)`)
g.P(`}`)
Expand Down

0 comments on commit d770dbe

Please sign in to comment.