2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/compiler/protogen"
|
2021-02-25 22:11:08 +03:00
|
|
|
"google.golang.org/protobuf/types/pluginpb"
|
2021-02-22 18:11:08 +03:00
|
|
|
)
|
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
var (
|
2021-02-25 14:19:09 +03:00
|
|
|
flagDebug = flag.Bool("debug", false, "")
|
|
|
|
flagStandalone = flag.Bool("standalone", false, "")
|
2021-03-26 16:04:08 +03:00
|
|
|
flagComponents = flag.String("components", "micro|rpc|http|client|server", "")
|
2021-05-11 09:07:12 +03:00
|
|
|
flagTagPath = flag.String("tag_path", "", "")
|
2021-02-22 23:42:49 +03:00
|
|
|
)
|
2021-02-22 18:11:08 +03:00
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
func main() {
|
2021-02-22 18:11:08 +03:00
|
|
|
opts := &protogen.Options{
|
2021-02-25 14:19:09 +03:00
|
|
|
ParamFunc: flag.CommandLine.Set,
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 23:42:49 +03:00
|
|
|
g := &Generator{}
|
2021-02-22 18:11:08 +03:00
|
|
|
|
|
|
|
opts.Run(g.Generate)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Generator struct {
|
2021-02-25 14:19:09 +03:00
|
|
|
components string
|
|
|
|
standalone bool
|
|
|
|
debug bool
|
2021-05-08 00:06:40 +03:00
|
|
|
tagPath string
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
|
|
|
var err error
|
|
|
|
|
2021-02-25 14:19:09 +03:00
|
|
|
g.standalone = *flagStandalone
|
|
|
|
g.debug = *flagDebug
|
|
|
|
g.components = *flagComponents
|
2021-05-08 00:06:40 +03:00
|
|
|
g.tagPath = *flagTagPath
|
2021-02-25 22:11:08 +03:00
|
|
|
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
2021-02-25 14:19:09 +03:00
|
|
|
|
2021-03-26 15:00:55 +03:00
|
|
|
var genClient bool
|
|
|
|
var genServer bool
|
|
|
|
|
|
|
|
if strings.Contains(g.components, "server") {
|
|
|
|
genServer = true
|
|
|
|
}
|
|
|
|
if strings.Contains(g.components, "client") {
|
|
|
|
genClient = true
|
|
|
|
}
|
2021-03-30 18:30:58 +03:00
|
|
|
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
|
|
|
|
if !genServer && !genClient {
|
|
|
|
genServer = true
|
|
|
|
genClient = true
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 15:00:55 +03:00
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
// Protoc passes a slice of File structs for us to process
|
2021-02-25 14:19:09 +03:00
|
|
|
for _, component := range strings.Split(g.components, "|") {
|
2021-02-22 18:11:08 +03:00
|
|
|
switch component {
|
2021-03-26 15:00:55 +03:00
|
|
|
case "server", "client":
|
|
|
|
continue
|
2021-02-22 18:11:08 +03:00
|
|
|
case "micro":
|
2021-03-26 15:00:55 +03:00
|
|
|
err = g.microGenerate(component, plugin, genClient, genServer)
|
2021-02-22 18:11:08 +03:00
|
|
|
case "http":
|
2021-03-26 15:00:55 +03:00
|
|
|
err = g.httpGenerate(component, plugin, genClient, genServer)
|
2021-02-22 18:11:08 +03:00
|
|
|
case "grpc", "rpc":
|
2021-03-26 15:00:55 +03:00
|
|
|
err = g.rpcGenerate("rpc", plugin, genClient, genServer)
|
2021-02-22 23:42:49 +03:00
|
|
|
case "gorilla":
|
|
|
|
err = g.gorillaGenerate(component, plugin)
|
|
|
|
case "chi":
|
|
|
|
err = g.chiGenerate(component, plugin)
|
2021-02-22 18:11:08 +03:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown component: %s", component)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
plugin.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-08 00:06:40 +03:00
|
|
|
if err = g.astGenerate(plugin); err != nil {
|
|
|
|
plugin.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
return nil
|
|
|
|
}
|