protoc-gen-go-micro/main.go

149 lines
3.5 KiB
Go
Raw Normal View History

2016-11-04 23:45:25 +03:00
package main
import (
"io/ioutil"
2016-11-07 10:45:02 +03:00
"log"
2016-11-04 23:45:25 +03:00
"os"
2016-11-07 10:45:02 +03:00
"strings"
2016-11-04 23:45:25 +03:00
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
2017-01-17 13:48:50 +03:00
"github.com/golang/protobuf/protoc-gen-go/plugin"
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers"
)
var (
registry *ggdescriptor.Registry // some helpers need access to registry
2016-11-04 23:45:25 +03:00
)
func main() {
g := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
g.Error(err, "reading input")
}
if err := proto.Unmarshal(data, g.Request); err != nil {
g.Error(err, "parsing input proto")
}
if len(g.Request.FileToGenerate) == 0 {
g.Fail("no files to generate")
}
g.CommandLineParameters(g.Request.GetParameter())
2016-11-07 10:45:02 +03:00
// Parse parameters
var (
templateDir = "./templates"
destinationDir = "."
debug = false
all = false
singlePackageMode = false
)
2016-11-07 10:45:02 +03:00
if parameter := g.Request.GetParameter(); parameter != "" {
for _, param := range strings.Split(parameter, ",") {
parts := strings.Split(param, "=")
if len(parts) != 2 {
log.Printf("Err: invalid parameter: %q", param)
continue
}
switch parts[0] {
case "template_dir":
templateDir = parts[1]
break
2016-12-15 18:25:44 +03:00
case "destination_dir":
destinationDir = parts[1]
break
case "single-package-mode":
switch strings.ToLower(parts[1]) {
case "true", "t":
singlePackageMode = true
case "false", "f":
default:
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
}
break
2016-11-07 10:45:02 +03:00
case "debug":
2016-12-14 13:08:51 +03:00
switch strings.ToLower(parts[1]) {
case "true", "t":
2016-11-07 10:45:02 +03:00
debug = true
2016-12-14 13:08:51 +03:00
case "false", "f":
default:
2016-11-07 10:45:02 +03:00
log.Printf("Err: invalid value for debug: %q", parts[1])
}
break
2017-01-12 16:46:43 +03:00
case "all":
switch strings.ToLower(parts[1]) {
case "true", "t":
all = true
case "false", "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
break
2016-11-07 10:45:02 +03:00
default:
log.Printf("Err: unknown parameter: %q", param)
}
}
}
2017-01-17 13:48:50 +03:00
tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File)
concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) {
if val, ok := tmplMap[file.GetName()]; ok {
*val.Content += file.GetContent()
2017-01-17 13:48:50 +03:00
} else {
tmplMap[file.GetName()] = file
2017-01-17 13:48:50 +03:00
g.Response.File = append(g.Response.File, file)
}
}
if singlePackageMode {
registry = ggdescriptor.NewRegistry()
pgghelpers.SetRegistry(registry)
if err := registry.Load(g.Request); err != nil {
g.Error(err, "registry: failed to load the request")
}
}
2016-11-07 10:45:02 +03:00
// Generate the encoders
2016-11-04 23:45:25 +03:00
for _, file := range g.Request.GetProtoFile() {
2017-01-12 16:46:43 +03:00
if all {
if singlePackageMode {
if _, err := registry.LookupFile(file.GetName()); err != nil {
g.Error(err, "registry: failed to lookup file %q", file.GetName())
}
}
2017-01-12 16:46:43 +03:00
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
2017-01-17 13:48:50 +03:00
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
2017-01-12 16:46:43 +03:00
continue
}
2016-11-04 23:45:25 +03:00
for _, service := range file.GetService() {
2017-01-12 16:46:43 +03:00
encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
2017-01-17 13:48:50 +03:00
for _, tmpl := range encoder.Files() {
concatOrAppend(tmpl)
}
2016-11-04 23:45:25 +03:00
}
}
// Generate the protobufs
g.GenerateAllFiles()
data, err = proto.Marshal(g.Response)
if err != nil {
g.Error(err, "failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
g.Error(err, "failed to write output proto")
}
}