2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-15 12:53:04 +03:00
|
|
|
"fmt"
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
"google.golang.org/protobuf/compiler/protogen"
|
|
|
|
)
|
|
|
|
|
2021-03-26 15:00:55 +03:00
|
|
|
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
|
2021-02-22 18:11:08 +03:00
|
|
|
for _, file := range plugin.Files {
|
|
|
|
if !file.Generate {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-22 23:42:49 +03:00
|
|
|
if len(file.Services) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
|
|
|
|
gname := file.GeneratedFilenamePrefix + "_" + component + ".pb.go"
|
2021-02-25 14:19:09 +03:00
|
|
|
|
|
|
|
path := file.GoImportPath
|
|
|
|
if g.standalone {
|
|
|
|
path = "."
|
|
|
|
}
|
|
|
|
gfile := plugin.NewGeneratedFile(gname, path)
|
2021-02-22 18:11:08 +03:00
|
|
|
|
2021-06-22 01:34:06 +03:00
|
|
|
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
|
2022-11-15 12:53:04 +03:00
|
|
|
gfile.P("// versions:")
|
|
|
|
gfile.P("// - protoc-gen-go-micro " + versionComment)
|
|
|
|
gfile.P("// - protoc ", protocVersion(plugin))
|
|
|
|
gfile.P("// source: ", file.Desc.Path())
|
2021-06-23 00:23:38 +03:00
|
|
|
gfile.P()
|
2021-02-22 18:11:08 +03:00
|
|
|
gfile.P("package ", file.GoPackageName)
|
2021-02-22 23:46:19 +03:00
|
|
|
gfile.P()
|
2021-02-22 18:11:08 +03:00
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.Import(contextPackage)
|
2023-02-22 00:05:54 +03:00
|
|
|
|
2021-03-26 15:00:55 +03:00
|
|
|
if genClient {
|
|
|
|
gfile.Import(microClientPackage)
|
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
// generate services
|
|
|
|
for _, service := range file.Services {
|
2023-02-22 00:05:54 +03:00
|
|
|
g.generateServiceName(gfile, service)
|
2021-03-26 15:00:55 +03:00
|
|
|
if genClient {
|
2022-03-19 15:13:34 +03:00
|
|
|
g.generateServiceClientInterface(gfile, service)
|
|
|
|
g.generateServiceClientStreamInterface(gfile, service)
|
2021-03-26 15:00:55 +03:00
|
|
|
}
|
|
|
|
if genServer {
|
2022-03-19 15:13:34 +03:00
|
|
|
g.generateServiceServerInterface(gfile, service)
|
|
|
|
g.generateServiceServerStreamInterface(gfile, service)
|
2021-03-26 15:00:55 +03:00
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-15 12:53:04 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|