mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 16:30:01 +08:00
feat(goctl/rpc): support external proto imports with cross-package ty… (#5472)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
{{.pbPackage}}
|
||||
{{if ne .pbPackage .protoGoPackage}}{{.protoGoPackage}}{{end}}
|
||||
{{.extraImports}}
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
||||
@@ -35,6 +36,9 @@ type ZRpcContext struct {
|
||||
// NameFromFilename uses proto filename instead of package name for service naming.
|
||||
// Default is false (uses package name, which supports multi-proto files).
|
||||
NameFromFilename bool
|
||||
// ProtoPaths are the directories to search for imported proto files,
|
||||
// equivalent to protoc -I flags. When empty the directory of Src is used.
|
||||
ProtoPaths []string
|
||||
}
|
||||
|
||||
// Generate generates a rpc service, through the proto file,
|
||||
@@ -72,6 +76,12 @@ func (g *Generator) Generate(zctx *ZRpcContext) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Populate ImportedProtos so that generators can resolve dotted type references.
|
||||
proto.ImportedProtos, err = resolveImportedProtos(zctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -120,3 +130,27 @@ func (g *Generator) Generate(zctx *ZRpcContext) error {
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// resolveImportedProtos builds the full list of transitively imported proto
|
||||
// files for zctx and returns their parsed metadata. This mirrors the proto
|
||||
// path computation in genpb.go so both use the same search paths.
|
||||
func resolveImportedProtos(zctx *ZRpcContext) ([]parser.ImportedProto, error) {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
protoPaths := make([]string, 0, len(zctx.ProtoPaths)+1)
|
||||
srcDir := filepath.Dir(zctx.Src)
|
||||
if !filepath.IsAbs(srcDir) {
|
||||
srcDir = filepath.Join(pwd, srcDir)
|
||||
}
|
||||
protoPaths = append(protoPaths, srcDir)
|
||||
for _, p := range zctx.ProtoPaths {
|
||||
if !filepath.IsAbs(p) {
|
||||
p = filepath.Join(pwd, p)
|
||||
}
|
||||
protoPaths = append(protoPaths, p)
|
||||
}
|
||||
return parser.ParseImportedProtos(zctx.Src, protoPaths)
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ func (g *Generator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config
|
||||
func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
dir := ctx.GetCall()
|
||||
head := util.GetHead(proto.Name)
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
for _, service := range proto.Service {
|
||||
childPkg, err := dir.GetChildPackage(service.Name)
|
||||
if err != nil {
|
||||
@@ -90,12 +91,13 @@ func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.C
|
||||
serviceName = stringx.From(service.Name + "_zrpc_client").ToCamel()
|
||||
}
|
||||
|
||||
functions, err := g.genFunction(proto.PbPackage, serviceName, service, isCallPkgSameToGrpcPkg)
|
||||
extraImports := collection.NewSet[string]()
|
||||
functions, err := g.genFunction(proto.PbPackage, proto.GoPackage, serviceName, service, isCallPkgSameToGrpcPkg, pkgMap, alias, extraImports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
iFunctions, err := g.getInterfaceFuncs(proto.PbPackage, service, isCallPkgSameToGrpcPkg)
|
||||
iFunctions, err := g.getInterfaceFuncs(proto.PbPackage, proto.GoPackage, service, isCallPkgSameToGrpcPkg, pkgMap, extraImports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -112,6 +114,7 @@ func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.C
|
||||
protoGoPackage = ""
|
||||
}
|
||||
|
||||
extraImportLines := buildExtraImportLines(extraImports)
|
||||
aliasKeys := alias.Keys()
|
||||
sort.Strings(aliasKeys)
|
||||
if err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]any{
|
||||
@@ -121,6 +124,7 @@ func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.C
|
||||
"filePackage": childDir,
|
||||
"pbPackage": pbPackage,
|
||||
"protoGoPackage": protoGoPackage,
|
||||
"extraImports": extraImportLines,
|
||||
"serviceName": serviceName,
|
||||
"functions": strings.Join(functions, pathx.NL),
|
||||
"interface": strings.Join(iFunctions, pathx.NL),
|
||||
@@ -162,13 +166,15 @@ func (g *Generator) genCallInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
serviceName = stringx.From(service.Name + "_zrpc_client").ToCamel()
|
||||
}
|
||||
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
extraImports := collection.NewSet[string]()
|
||||
filename := filepath.Join(dir.Filename, fmt.Sprintf("%s.go", callFilename))
|
||||
functions, err := g.genFunction(proto.PbPackage, serviceName, service, isCallPkgSameToGrpcPkg)
|
||||
functions, err := g.genFunction(proto.PbPackage, proto.GoPackage, serviceName, service, isCallPkgSameToGrpcPkg, pkgMap, alias, extraImports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
iFunctions, err := g.getInterfaceFuncs(proto.PbPackage, service, isCallPkgSameToGrpcPkg)
|
||||
iFunctions, err := g.getInterfaceFuncs(proto.PbPackage, proto.GoPackage, service, isCallPkgSameToGrpcPkg, pkgMap, extraImports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -184,6 +190,7 @@ func (g *Generator) genCallInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
pbPackage = ""
|
||||
protoGoPackage = ""
|
||||
}
|
||||
extraImportLines := buildExtraImportLines(extraImports)
|
||||
aliasKeys := alias.Keys()
|
||||
sort.Strings(aliasKeys)
|
||||
return util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]any{
|
||||
@@ -193,6 +200,7 @@ func (g *Generator) genCallInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
"filePackage": dir.Base,
|
||||
"pbPackage": pbPackage,
|
||||
"protoGoPackage": protoGoPackage,
|
||||
"extraImports": extraImportLines,
|
||||
"serviceName": serviceName,
|
||||
"functions": strings.Join(functions, pathx.NL),
|
||||
"interface": strings.Join(iFunctions, pathx.NL),
|
||||
@@ -221,8 +229,9 @@ func getMessageName(msg proto.Message) string {
|
||||
return strings.Join(list, "_")
|
||||
}
|
||||
|
||||
func (g *Generator) genFunction(goPackage string, serviceName string, service parser.Service,
|
||||
isCallPkgSameToGrpcPkg bool) ([]string, error) {
|
||||
func (g *Generator) genFunction(goPackage, mainGoPackage, serviceName string, service parser.Service,
|
||||
isCallPkgSameToGrpcPkg bool, pkgMap map[string]parser.ImportedProto,
|
||||
alias, extraImports *collection.Set[string]) ([]string, error) {
|
||||
functions := make([]string, 0)
|
||||
|
||||
for _, rpc := range service.RPC {
|
||||
@@ -238,13 +247,29 @@ func (g *Generator) genFunction(goPackage string, serviceName string, service pa
|
||||
streamServer = fmt.Sprintf("%s_%s%s", parser.CamelCase(service.Name),
|
||||
parser.CamelCase(rpc.Name), "Client")
|
||||
}
|
||||
|
||||
reqName, reqAlias, reqImport := resolveCallTypeRef(rpc.RequestType, goPackage, mainGoPackage, pkgMap)
|
||||
respName, respAlias, respImport := resolveCallTypeRef(rpc.ReturnsType, goPackage, mainGoPackage, pkgMap)
|
||||
if reqAlias != "" {
|
||||
alias.Add(reqAlias)
|
||||
}
|
||||
if respAlias != "" {
|
||||
alias.Add(respAlias)
|
||||
}
|
||||
if reqImport != "" {
|
||||
extraImports.Add(reqImport)
|
||||
}
|
||||
if respImport != "" {
|
||||
extraImports.Add(respImport)
|
||||
}
|
||||
|
||||
buffer, err := util.With("sharedFn").Parse(text).Execute(map[string]any{
|
||||
"serviceName": serviceName,
|
||||
"rpcServiceName": parser.CamelCase(service.Name),
|
||||
"method": parser.CamelCase(rpc.Name),
|
||||
"package": goPackage,
|
||||
"pbRequest": parser.CamelCase(rpc.RequestType),
|
||||
"pbResponse": parser.CamelCase(rpc.ReturnsType),
|
||||
"pbRequest": reqName,
|
||||
"pbResponse": respName,
|
||||
"hasComment": len(comment) > 0,
|
||||
"comment": comment,
|
||||
"hasReq": !rpc.StreamsRequest,
|
||||
@@ -262,8 +287,9 @@ func (g *Generator) genFunction(goPackage string, serviceName string, service pa
|
||||
return functions, nil
|
||||
}
|
||||
|
||||
func (g *Generator) getInterfaceFuncs(goPackage string, service parser.Service,
|
||||
isCallPkgSameToGrpcPkg bool) ([]string, error) {
|
||||
func (g *Generator) getInterfaceFuncs(goPackage, mainGoPackage string, service parser.Service,
|
||||
isCallPkgSameToGrpcPkg bool, pkgMap map[string]parser.ImportedProto,
|
||||
extraImports *collection.Set[string]) ([]string, error) {
|
||||
functions := make([]string, 0)
|
||||
|
||||
for _, rpc := range service.RPC {
|
||||
@@ -280,15 +306,25 @@ func (g *Generator) getInterfaceFuncs(goPackage string, service parser.Service,
|
||||
streamServer = fmt.Sprintf("%s_%s%s", parser.CamelCase(service.Name),
|
||||
parser.CamelCase(rpc.Name), "Client")
|
||||
}
|
||||
|
||||
reqName, _, reqImport := resolveCallTypeRef(rpc.RequestType, goPackage, mainGoPackage, pkgMap)
|
||||
respName, _, respImport := resolveCallTypeRef(rpc.ReturnsType, goPackage, mainGoPackage, pkgMap)
|
||||
if reqImport != "" {
|
||||
extraImports.Add(reqImport)
|
||||
}
|
||||
if respImport != "" {
|
||||
extraImports.Add(respImport)
|
||||
}
|
||||
|
||||
buffer, err := util.With("interfaceFn").Parse(text).Execute(
|
||||
map[string]any{
|
||||
"hasComment": len(comment) > 0,
|
||||
"comment": comment,
|
||||
"method": parser.CamelCase(rpc.Name),
|
||||
"hasReq": !rpc.StreamsRequest,
|
||||
"pbRequest": parser.CamelCase(rpc.RequestType),
|
||||
"pbRequest": reqName,
|
||||
"notStream": !rpc.StreamsRequest && !rpc.StreamsReturns,
|
||||
"pbResponse": parser.CamelCase(rpc.ReturnsType),
|
||||
"pbResponse": respName,
|
||||
"streamBody": streamServer,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -300,3 +336,18 @@ func (g *Generator) getInterfaceFuncs(goPackage string, service parser.Service,
|
||||
|
||||
return functions, nil
|
||||
}
|
||||
|
||||
// buildExtraImportLines converts a set of import paths into quoted import lines
|
||||
// for use in the call.tpl {{.extraImports}} placeholder.
|
||||
func buildExtraImportLines(extraImports *collection.Set[string]) string {
|
||||
if extraImports.Count() == 0 {
|
||||
return ""
|
||||
}
|
||||
keys := extraImports.Keys()
|
||||
sort.Strings(keys)
|
||||
lines := make([]string, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
lines = append(lines, fmt.Sprintf(`"%s"`, k))
|
||||
}
|
||||
return strings.Join(lines, "\n\t")
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
cfg *conf.Config) error {
|
||||
dir := ctx.GetLogic()
|
||||
service := proto.Service[0].Service.Name
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
for _, rpc := range proto.Service[0].RPC {
|
||||
logicName := fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel())
|
||||
logicFilename, err := format.FileNamingFormat(cfg.NamingFormat, rpc.Name+"_logic")
|
||||
@@ -48,14 +49,15 @@ func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
}
|
||||
|
||||
filename := filepath.Join(dir.Filename, logicFilename+".go")
|
||||
functions, err := g.genLogicFunction(service, proto.PbPackage, logicName, rpc)
|
||||
functions, err := g.genLogicFunction(service, proto.PbPackage, proto.GoPackage, logicName, rpc, pkgMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imports := collection.NewSet[string]()
|
||||
imports.Add(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
|
||||
imports.Add(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
|
||||
addLogicImports(imports, ctx.GetPb().Package, proto.PbPackage, proto.GoPackage, rpc, pkgMap)
|
||||
|
||||
text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -75,6 +77,7 @@ func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
|
||||
func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
dir := ctx.GetLogic()
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
for _, item := range proto.Service {
|
||||
serviceName := item.Name
|
||||
for _, rpc := range item.RPC {
|
||||
@@ -101,14 +104,15 @@ func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.
|
||||
}
|
||||
|
||||
filename = filepath.Join(dir.Filename, serviceDir, logicFilename+".go")
|
||||
functions, err := g.genLogicFunction(serviceName, proto.PbPackage, logicName, rpc)
|
||||
functions, err := g.genLogicFunction(serviceName, proto.PbPackage, proto.GoPackage, logicName, rpc, pkgMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
imports := collection.NewSet[string]()
|
||||
imports.Add(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
|
||||
imports.Add(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
|
||||
addLogicImports(imports, ctx.GetPb().Package, proto.PbPackage, proto.GoPackage, rpc, pkgMap)
|
||||
|
||||
text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -127,9 +131,8 @@ func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) genLogicFunction(serviceName, goPackage, logicName string,
|
||||
rpc *parser.RPC) (string,
|
||||
error) {
|
||||
func (g *Generator) genLogicFunction(serviceName, goPackage, mainGoPackage, logicName string,
|
||||
rpc *parser.RPC, pkgMap map[string]parser.ImportedProto) (string, error) {
|
||||
functions := make([]string, 0)
|
||||
text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
|
||||
if err != nil {
|
||||
@@ -139,14 +142,18 @@ func (g *Generator) genLogicFunction(serviceName, goPackage, logicName string,
|
||||
comment := parser.GetComment(rpc.Doc())
|
||||
streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(serviceName),
|
||||
parser.CamelCase(rpc.Name), "Server")
|
||||
|
||||
reqRef := resolveRPCTypeRef(rpc.RequestType, goPackage, mainGoPackage, pkgMap)
|
||||
respRef := resolveRPCTypeRef(rpc.ReturnsType, goPackage, mainGoPackage, pkgMap)
|
||||
|
||||
buffer, err := util.With("fun").Parse(text).Execute(map[string]any{
|
||||
"logicName": logicName,
|
||||
"method": parser.CamelCase(rpc.Name),
|
||||
"hasReq": !rpc.StreamsRequest,
|
||||
"request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
|
||||
"request": "*" + reqRef.GoRef,
|
||||
"hasReply": !rpc.StreamsRequest && !rpc.StreamsReturns,
|
||||
"response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
|
||||
"responseType": fmt.Sprintf("%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
|
||||
"response": "*" + respRef.GoRef,
|
||||
"responseType": respRef.GoRef,
|
||||
"stream": rpc.StreamsRequest || rpc.StreamsReturns,
|
||||
"streamBody": streamServer,
|
||||
"hasComment": len(comment) > 0,
|
||||
@@ -159,3 +166,29 @@ func (g *Generator) genLogicFunction(serviceName, goPackage, logicName string,
|
||||
functions = append(functions, buffer.String())
|
||||
return strings.Join(functions, pathx.NL), nil
|
||||
}
|
||||
|
||||
// addLogicImports adds the correct import paths to imports for a single RPC's
|
||||
// logic file. The main pb package is only included when it is actually referenced
|
||||
// (i.e. when the request or response type lives in that package, or the RPC streams).
|
||||
func addLogicImports(imports *collection.Set[string], pbImportPath, goPackage, mainGoPackage string,
|
||||
rpc *parser.RPC, pkgMap map[string]parser.ImportedProto) {
|
||||
// Streaming RPCs always reference the main pb package (for the stream type).
|
||||
if rpc.StreamsRequest || rpc.StreamsReturns {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, pbImportPath))
|
||||
return
|
||||
}
|
||||
|
||||
reqRef := resolveRPCTypeRef(rpc.RequestType, goPackage, mainGoPackage, pkgMap)
|
||||
respRef := resolveRPCTypeRef(rpc.ReturnsType, goPackage, mainGoPackage, pkgMap)
|
||||
|
||||
// Add main pb import if any type ref is from the main package (no extra import path).
|
||||
if reqRef.ImportPath == "" || respRef.ImportPath == "" {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, pbImportPath))
|
||||
}
|
||||
if reqRef.ImportPath != "" {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, reqRef.ImportPath))
|
||||
}
|
||||
if respRef.ImportPath != "" {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, respRef.ImportPath))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
@@ -19,19 +20,89 @@ func (g *Generator) GenPb(ctx DirContext, c *ZRpcContext) error {
|
||||
}
|
||||
|
||||
func (g *Generator) genPbDirect(ctx DirContext, c *ZRpcContext) error {
|
||||
g.log.Debug("[command]: %s", c.ProtocCmd)
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = execx.Run(c.ProtocCmd, pwd)
|
||||
protocCmd, err := g.buildProtocCmd(c, pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.log.Debug("[command]: %s", protocCmd)
|
||||
_, err = execx.Run(protocCmd, pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.setPbDir(ctx, c)
|
||||
}
|
||||
|
||||
// buildProtocCmd resolves all transitively imported proto files and appends
|
||||
// them to the protoc command so that their pb.go files are also generated.
|
||||
func (g *Generator) buildProtocCmd(c *ZRpcContext, pwd string) (string, error) {
|
||||
// Build the full list of proto search paths (absolute).
|
||||
protoPaths := make([]string, 0, len(c.ProtoPaths)+1)
|
||||
|
||||
// Always include the directory of the source proto so that imports
|
||||
// relative to the source file can be found.
|
||||
srcDir := filepath.Dir(c.Src)
|
||||
if !filepath.IsAbs(srcDir) {
|
||||
srcDir = filepath.Join(pwd, srcDir)
|
||||
}
|
||||
protoPaths = append(protoPaths, srcDir)
|
||||
|
||||
for _, p := range c.ProtoPaths {
|
||||
if !filepath.IsAbs(p) {
|
||||
p = filepath.Join(pwd, p)
|
||||
}
|
||||
protoPaths = append(protoPaths, p)
|
||||
}
|
||||
|
||||
importedFiles, err := parser.ResolveImports(c.Src, protoPaths)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(importedFiles) == 0 {
|
||||
return c.ProtocCmd, nil
|
||||
}
|
||||
|
||||
cmd := c.ProtocCmd
|
||||
for _, f := range importedFiles {
|
||||
// Use the path relative to the best-matching --proto_path entry so that
|
||||
// protoc's source_relative output lands in the correct directory.
|
||||
// e.g. if --proto_path=./ext and the file is ext/common/types.proto,
|
||||
// we pass "common/types.proto" rather than "ext/common/types.proto".
|
||||
rel := relativeToProtoPath(f, protoPaths, pwd)
|
||||
cmd += " " + rel
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// relativeToProtoPath returns the path of f relative to the most specific
|
||||
// (longest) proto_path entry that is a parent of f. Falls back to relative
|
||||
// to pwd when no proto_path matches.
|
||||
func relativeToProtoPath(f string, protoPaths []string, pwd string) string {
|
||||
bestRel := ""
|
||||
bestLen := 0
|
||||
for _, pp := range protoPaths {
|
||||
prefix := pp + string(filepath.Separator)
|
||||
if strings.HasPrefix(f, prefix) && len(pp) > bestLen {
|
||||
if rel, err := filepath.Rel(pp, f); err == nil {
|
||||
bestRel = rel
|
||||
bestLen = len(pp)
|
||||
}
|
||||
}
|
||||
}
|
||||
if bestRel != "" {
|
||||
return bestRel
|
||||
}
|
||||
if rel, err := filepath.Rel(pwd, f); err == nil {
|
||||
return rel
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (g *Generator) setPbDir(ctx DirContext, c *ZRpcContext) error {
|
||||
pbDir, err := findPbFile(c.GoOutput, c.Src, false)
|
||||
if err != nil {
|
||||
|
||||
@@ -38,6 +38,7 @@ func (g *Generator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Conf
|
||||
|
||||
func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
|
||||
dir := ctx.GetServer()
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
for _, service := range proto.Service {
|
||||
var (
|
||||
serverFile string
|
||||
@@ -71,10 +72,13 @@ func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf
|
||||
|
||||
head := util.GetHead(proto.Name)
|
||||
|
||||
funcList, err := g.genFunctions(proto.PbPackage, service, true)
|
||||
funcList, extraImportPaths, err := g.genFunctions(proto.PbPackage, proto.GoPackage, service, true, pkgMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, imp := range extraImportPaths {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, imp))
|
||||
}
|
||||
|
||||
text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
||||
if err != nil {
|
||||
@@ -114,6 +118,7 @@ func (g *Generator) genServerInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
imports := collection.NewSet[string]()
|
||||
imports.Add(logicImport, svcImport, pbImport)
|
||||
|
||||
pkgMap := parser.BuildProtoPackageMap(proto.ImportedProtos)
|
||||
head := util.GetHead(proto.Name)
|
||||
service := proto.Service[0]
|
||||
serverFilename, err := format.FileNamingFormat(cfg.NamingFormat, service.Name+"_server")
|
||||
@@ -122,10 +127,13 @@ func (g *Generator) genServerInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
}
|
||||
|
||||
serverFile := filepath.Join(dir.Filename, serverFilename+".go")
|
||||
funcList, err := g.genFunctions(proto.PbPackage, service, false)
|
||||
funcList, extraImportPaths, err := g.genFunctions(proto.PbPackage, proto.GoPackage, service, false, pkgMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, imp := range extraImportPaths {
|
||||
imports.Add(fmt.Sprintf(`"%s"`, imp))
|
||||
}
|
||||
|
||||
text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
|
||||
if err != nil {
|
||||
@@ -151,15 +159,17 @@ func (g *Generator) genServerInCompatibility(ctx DirContext, proto parser.Proto,
|
||||
}, serverFile, true)
|
||||
}
|
||||
|
||||
func (g *Generator) genFunctions(goPackage string, service parser.Service, multiple bool) ([]string, error) {
|
||||
func (g *Generator) genFunctions(goPackage, mainGoPackage string, service parser.Service,
|
||||
multiple bool, pkgMap map[string]parser.ImportedProto) ([]string, []string, error) {
|
||||
var (
|
||||
functionList []string
|
||||
logicPkg string
|
||||
extraImports []string
|
||||
)
|
||||
for _, rpc := range service.RPC {
|
||||
text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var logicName string
|
||||
@@ -175,12 +185,22 @@ func (g *Generator) genFunctions(goPackage string, service parser.Service, multi
|
||||
comment := parser.GetComment(rpc.Doc())
|
||||
streamServer := fmt.Sprintf("%s.%s_%s%s", goPackage, parser.CamelCase(service.Name),
|
||||
parser.CamelCase(rpc.Name), "Server")
|
||||
|
||||
reqRef := resolveRPCTypeRef(rpc.RequestType, goPackage, mainGoPackage, pkgMap)
|
||||
respRef := resolveRPCTypeRef(rpc.ReturnsType, goPackage, mainGoPackage, pkgMap)
|
||||
if reqRef.ImportPath != "" {
|
||||
extraImports = append(extraImports, reqRef.ImportPath)
|
||||
}
|
||||
if respRef.ImportPath != "" {
|
||||
extraImports = append(extraImports, respRef.ImportPath)
|
||||
}
|
||||
|
||||
buffer, err := util.With("func").Parse(text).Execute(map[string]any{
|
||||
"server": stringx.From(service.Name).ToCamel(),
|
||||
"logicName": logicName,
|
||||
"method": parser.CamelCase(rpc.Name),
|
||||
"request": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.RequestType)),
|
||||
"response": fmt.Sprintf("*%s.%s", goPackage, parser.CamelCase(rpc.ReturnsType)),
|
||||
"request": "*" + reqRef.GoRef,
|
||||
"response": "*" + respRef.GoRef,
|
||||
"hasComment": len(comment) > 0,
|
||||
"comment": comment,
|
||||
"hasReq": !rpc.StreamsRequest,
|
||||
@@ -190,10 +210,10 @@ func (g *Generator) genFunctions(goPackage string, service parser.Service, multi
|
||||
"logicPkg": logicPkg,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
functionList = append(functionList, buffer.String())
|
||||
}
|
||||
return functionList, nil
|
||||
return functionList, extraImports, nil
|
||||
}
|
||||
|
||||
115
tools/goctl/rpc/generator/typeref.go
Normal file
115
tools/goctl/rpc/generator/typeref.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
||||
)
|
||||
|
||||
// rpcTypeRef holds the resolved Go type reference for an RPC request/response type.
|
||||
type rpcTypeRef struct {
|
||||
// GoRef is the qualified Go type name, e.g. "pb.GetReq", "common.TypesReq", "emptypb.Empty".
|
||||
GoRef string
|
||||
// ImportPath is an extra Go import path required for cross-package types.
|
||||
// Empty when the type lives in a package that is already imported.
|
||||
ImportPath string
|
||||
}
|
||||
|
||||
// resolveRPCTypeRef resolves a proto RPC type (possibly dotted) to its Go type
|
||||
// reference and the optional extra import it requires.
|
||||
//
|
||||
// - Simple types ("GetReq") → mainPbPackage.GetReq, no extra import.
|
||||
// - Same-package dotted types ("ext.ExtReq", same go_package) → mainPbPackage.ExtReq.
|
||||
// - Cross-package dotted types ("common.TypesReq") → common.TypesReq + import path.
|
||||
// - google.protobuf.X types → well-known Go type + import path.
|
||||
func resolveRPCTypeRef(protoType, mainPbPackage, mainGoPackage string, pkgMap map[string]parser.ImportedProto) rpcTypeRef {
|
||||
if !strings.Contains(protoType, ".") {
|
||||
return rpcTypeRef{GoRef: fmt.Sprintf("%s.%s", mainPbPackage, parser.CamelCase(protoType))}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(protoType, "google.protobuf.") {
|
||||
typeName := strings.TrimPrefix(protoType, "google.protobuf.")
|
||||
return resolveGoogleWKT(typeName)
|
||||
}
|
||||
|
||||
dot := strings.Index(protoType, ".")
|
||||
protoPkg, typeName := protoType[:dot], protoType[dot+1:]
|
||||
|
||||
if imp, ok := pkgMap[protoPkg]; ok {
|
||||
camelType := parser.CamelCase(typeName)
|
||||
if imp.GoPackage == mainGoPackage {
|
||||
// Same Go package as main proto — no extra import needed.
|
||||
return rpcTypeRef{GoRef: fmt.Sprintf("%s.%s", mainPbPackage, camelType)}
|
||||
}
|
||||
return rpcTypeRef{
|
||||
GoRef: fmt.Sprintf("%s.%s", imp.PbPackage, camelType),
|
||||
ImportPath: imp.GoPackage,
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: treat as same package with CamelCase applied to full dotted name.
|
||||
return rpcTypeRef{GoRef: fmt.Sprintf("%s.%s", mainPbPackage, parser.CamelCase(protoType))}
|
||||
}
|
||||
|
||||
// resolveCallTypeRef is tailored for gencall.go's type-alias system.
|
||||
// Returns:
|
||||
// - typeName: the identifier to place in function signatures (alias name or full "pkg.Type" ref).
|
||||
// - aliasEntry: if non-empty, a "TypeName = pkg.TypeName" alias declaration to add to the type block.
|
||||
// - importPath: if non-empty, the extra import path needed.
|
||||
func resolveCallTypeRef(protoType, mainPbPackage, mainGoPackage string, pkgMap map[string]parser.ImportedProto) (typeName, aliasEntry, importPath string) {
|
||||
if !strings.Contains(protoType, ".") {
|
||||
// Simple type — alias is produced by the existing proto.Message loop.
|
||||
return parser.CamelCase(protoType), "", ""
|
||||
}
|
||||
|
||||
if strings.HasPrefix(protoType, "google.protobuf.") {
|
||||
tn := strings.TrimPrefix(protoType, "google.protobuf.")
|
||||
ref := resolveGoogleWKT(tn)
|
||||
return ref.GoRef, "", ref.ImportPath
|
||||
}
|
||||
|
||||
dot := strings.Index(protoType, ".")
|
||||
protoPkg, tn := protoType[:dot], protoType[dot+1:]
|
||||
camelType := parser.CamelCase(tn)
|
||||
|
||||
if imp, ok := pkgMap[protoPkg]; ok {
|
||||
if imp.GoPackage == mainGoPackage {
|
||||
// Same Go package: add an alias so the function signature uses a simple name.
|
||||
entry := fmt.Sprintf("%s = %s.%s", camelType, mainPbPackage, camelType)
|
||||
return camelType, entry, ""
|
||||
}
|
||||
// Different Go package: use fully-qualified ref directly; no alias needed.
|
||||
return fmt.Sprintf("%s.%s", imp.PbPackage, camelType), "", imp.GoPackage
|
||||
}
|
||||
|
||||
return parser.CamelCase(protoType), "", ""
|
||||
}
|
||||
|
||||
// googleWKTTable maps google.protobuf type names to their generated Go equivalents.
|
||||
var googleWKTTable = map[string]rpcTypeRef{
|
||||
"Empty": {GoRef: "emptypb.Empty", ImportPath: "google.golang.org/protobuf/types/known/emptypb"},
|
||||
"Timestamp": {GoRef: "timestamppb.Timestamp", ImportPath: "google.golang.org/protobuf/types/known/timestamppb"},
|
||||
"Duration": {GoRef: "durationpb.Duration", ImportPath: "google.golang.org/protobuf/types/known/durationpb"},
|
||||
"Any": {GoRef: "anypb.Any", ImportPath: "google.golang.org/protobuf/types/known/anypb"},
|
||||
"StringValue": {GoRef: "wrapperspb.StringValue", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"Int32Value": {GoRef: "wrapperspb.Int32Value", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"Int64Value": {GoRef: "wrapperspb.Int64Value", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"BoolValue": {GoRef: "wrapperspb.BoolValue", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"BytesValue": {GoRef: "wrapperspb.BytesValue", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"FloatValue": {GoRef: "wrapperspb.FloatValue", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"DoubleValue": {GoRef: "wrapperspb.DoubleValue", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"UInt32Value": {GoRef: "wrapperspb.UInt32Value", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"UInt64Value": {GoRef: "wrapperspb.UInt64Value", ImportPath: "google.golang.org/protobuf/types/known/wrapperspb"},
|
||||
"Struct": {GoRef: "structpb.Struct", ImportPath: "google.golang.org/protobuf/types/known/structpb"},
|
||||
"Value": {GoRef: "structpb.Value", ImportPath: "google.golang.org/protobuf/types/known/structpb"},
|
||||
"ListValue": {GoRef: "structpb.ListValue", ImportPath: "google.golang.org/protobuf/types/known/structpb"},
|
||||
"FieldMask": {GoRef: "fieldmaskpb.FieldMask", ImportPath: "google.golang.org/protobuf/types/known/fieldmaskpb"},
|
||||
}
|
||||
|
||||
func resolveGoogleWKT(typeName string) rpcTypeRef {
|
||||
if r, ok := googleWKTTable[typeName]; ok {
|
||||
return r
|
||||
}
|
||||
return rpcTypeRef{GoRef: "interface{}"}
|
||||
}
|
||||
Reference in New Issue
Block a user