mirror of
https://github.com/go-micro/go-micro.git
synced 2025-04-23 11:07:43 +02:00
Support for -micro_out=module=<module_prefix>
for protoc-gen-micro (#2435)
This commit is contained in:
parent
d47c2d984b
commit
69b4b5d1c6
@ -305,7 +305,7 @@ func (d *FileDescriptor) goPackageOption() (impPath GoImportPath, pkg GoPackageN
|
|||||||
}
|
}
|
||||||
|
|
||||||
// goFileName returns the output name for the generated Go file.
|
// goFileName returns the output name for the generated Go file.
|
||||||
func (d *FileDescriptor) goFileName(pathType pathType) string {
|
func (d *FileDescriptor) goFileName(pathType pathType, moduleRoot string) string {
|
||||||
name := *d.Name
|
name := *d.Name
|
||||||
if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" {
|
if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" {
|
||||||
name = name[:len(name)-len(ext)]
|
name = name[:len(name)-len(ext)]
|
||||||
@ -319,9 +319,18 @@ func (d *FileDescriptor) goFileName(pathType pathType) string {
|
|||||||
// Does the file have a "go_package" option?
|
// Does the file have a "go_package" option?
|
||||||
// If it does, it may override the filename.
|
// If it does, it may override the filename.
|
||||||
if impPath, _, ok := d.goPackageOption(); ok && impPath != "" {
|
if impPath, _, ok := d.goPackageOption(); ok && impPath != "" {
|
||||||
|
if pathType == pathModuleRoot && moduleRoot != "" {
|
||||||
|
root := moduleRoot
|
||||||
|
if !strings.HasSuffix(root, "/") {
|
||||||
|
root = root + "/"
|
||||||
|
}
|
||||||
|
name = strings.TrimPrefix(name, root)
|
||||||
|
} else {
|
||||||
// Replace the existing dirname with the declared import path.
|
// Replace the existing dirname with the declared import path.
|
||||||
_, name = path.Split(name)
|
_, name = path.Split(name)
|
||||||
name = path.Join(string(impPath), name)
|
name = path.Join(string(impPath), name)
|
||||||
|
}
|
||||||
|
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -405,6 +414,7 @@ type Generator struct {
|
|||||||
PackageImportPath string // Go import path of the package we're generating code for
|
PackageImportPath string // Go import path of the package we're generating code for
|
||||||
ImportPrefix string // String to prefix to imported package file names.
|
ImportPrefix string // String to prefix to imported package file names.
|
||||||
ImportMap map[string]string // Mapping from .proto file name to import path
|
ImportMap map[string]string // Mapping from .proto file name to import path
|
||||||
|
ModuleRoot string // Mapping from the module prefix
|
||||||
|
|
||||||
Pkg map[string]string // The names under which we import support packages
|
Pkg map[string]string // The names under which we import support packages
|
||||||
|
|
||||||
@ -429,6 +439,7 @@ type pathType int
|
|||||||
const (
|
const (
|
||||||
pathTypeImport pathType = iota
|
pathTypeImport pathType = iota
|
||||||
pathTypeSourceRelative
|
pathTypeSourceRelative
|
||||||
|
pathModuleRoot
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new generator and allocates the request and response protobufs.
|
// New creates a new generator and allocates the request and response protobufs.
|
||||||
@ -475,11 +486,20 @@ func (g *Generator) CommandLineParameters(parameter string) {
|
|||||||
g.ImportPrefix = v
|
g.ImportPrefix = v
|
||||||
case "import_path":
|
case "import_path":
|
||||||
g.PackageImportPath = v
|
g.PackageImportPath = v
|
||||||
|
case "module":
|
||||||
|
if g.pathType == pathTypeSourceRelative {
|
||||||
|
g.Fail(fmt.Sprintf(`Cannot set module=%q after paths=source_relative`, v))
|
||||||
|
}
|
||||||
|
g.pathType = pathModuleRoot
|
||||||
|
g.ModuleRoot = v
|
||||||
case "paths":
|
case "paths":
|
||||||
switch v {
|
switch v {
|
||||||
case "import":
|
case "import":
|
||||||
g.pathType = pathTypeImport
|
g.pathType = pathTypeImport
|
||||||
case "source_relative":
|
case "source_relative":
|
||||||
|
if g.pathType == pathModuleRoot {
|
||||||
|
g.Fail("Cannot set paths=source_relative after setting module=<module_root>")
|
||||||
|
}
|
||||||
g.pathType = pathTypeSourceRelative
|
g.pathType = pathTypeSourceRelative
|
||||||
default:
|
default:
|
||||||
g.Fail(fmt.Sprintf(`Unknown path type %q: want "import" or "source_relative".`, v))
|
g.Fail(fmt.Sprintf(`Unknown path type %q: want "import" or "source_relative".`, v))
|
||||||
@ -1069,7 +1089,7 @@ func (g *Generator) GenerateAllFiles() {
|
|||||||
if !g.writeOutput {
|
if !g.writeOutput {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fname := file.goFileName(g.pathType)
|
fname := file.goFileName(g.pathType, g.ModuleRoot)
|
||||||
g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{
|
g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{
|
||||||
Name: proto.String(fname),
|
Name: proto.String(fname),
|
||||||
Content: proto.String(g.String()),
|
Content: proto.String(g.String()),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user