2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
|
|
|
gfile.P("// Code generated by protoc-gen-micro")
|
|
|
|
gfile.P("// source: ", *file.Proto.Name)
|
|
|
|
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)
|
|
|
|
gfile.Import(microApiPackage)
|
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 {
|
2021-02-22 23:42:49 +03:00
|
|
|
generateServiceEndpoints(gfile, service)
|
2021-03-26 15:00:55 +03:00
|
|
|
if genClient {
|
|
|
|
generateServiceClientInterface(gfile, service)
|
|
|
|
generateServiceClientStreamInterface(gfile, service)
|
|
|
|
}
|
|
|
|
if genServer {
|
|
|
|
generateServiceServerInterface(gfile, service)
|
|
|
|
generateServiceServerStreamInterface(gfile, service)
|
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|