protoc-gen-go-micro/micro.go
Vasiliy Tolstov 388a58d998 replace map
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-02-22 00:05:54 +03:00

70 lines
1.6 KiB
Go

package main
import (
"fmt"
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = "."
}
gfile := plugin.NewGeneratedFile(gname, path)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// versions:")
gfile.P("// - protoc-gen-go-micro " + versionComment)
gfile.P("// - protoc ", protocVersion(plugin))
gfile.P("// source: ", file.Desc.Path())
gfile.P()
gfile.P("package ", file.GoPackageName)
gfile.P()
gfile.Import(contextPackage)
if genClient {
gfile.Import(microClientPackage)
}
// generate services
for _, service := range file.Services {
g.generateServiceName(gfile, service)
g.generateServiceEndpoints(gfile, service)
if genClient {
g.generateServiceClientInterface(gfile, service)
g.generateServiceClientStreamInterface(gfile, service)
}
if genServer {
g.generateServiceServerInterface(gfile, service)
g.generateServiceServerStreamInterface(gfile, service)
}
}
}
return nil
}
func protocVersion(plugin *protogen.Plugin) string {
v := plugin.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}