2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-12-26 01:09:11 +03:00
|
|
|
"os"
|
2021-02-22 18:11:08 +03:00
|
|
|
"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-12-26 01:09:11 +03:00
|
|
|
flagSet = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
|
|
|
flagDebug = flagSet.Bool("debug", false, "debug output")
|
|
|
|
flagStandalone = flagSet.Bool("standalone", false, "generate file to standalone dir")
|
|
|
|
flagFieldaligment = flagSet.Bool("fieldaligment", false, "align struct fields in generated code")
|
2024-06-11 20:45:09 +03:00
|
|
|
flagComponents = flagSet.String("components", "micro|rpc|http|client|server|openapiv3|graphql", "specify components to generate")
|
2021-12-26 01:09:11 +03:00
|
|
|
flagTagPath = flagSet.String("tag_path", "", "tag rewriting dir")
|
2024-06-11 20:45:09 +03:00
|
|
|
flagOpenapiFile = flagSet.String("openapi_file", "apidocs.swagger.yaml", "openapi file name")
|
|
|
|
flagGraphqlFile = flagSet.String("openapi_file", "schema.graphqls", "graphql file name")
|
2022-11-15 12:53:04 +03:00
|
|
|
flagReflection = flagSet.Bool("reflection", false, "enable server reflection support")
|
2021-12-26 01:09:11 +03:00
|
|
|
flagHelp = flagSet.Bool("help", false, "display help message")
|
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-12-26 01:09:11 +03:00
|
|
|
ParamFunc: flagSet.Set,
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
2024-06-11 20:45:09 +03:00
|
|
|
_ = flagSet.Parse(os.Args[1:])
|
2021-10-23 02:55:43 +03:00
|
|
|
|
|
|
|
if *flagHelp {
|
2021-12-26 01:09:11 +03:00
|
|
|
flagSet.PrintDefaults()
|
2021-10-23 02:55:43 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-12-26 01:09:11 +03:00
|
|
|
components string
|
|
|
|
standalone bool
|
|
|
|
debug bool
|
|
|
|
fieldaligment bool
|
|
|
|
tagPath string
|
|
|
|
openapiFile string
|
2022-11-15 12:53:04 +03:00
|
|
|
reflection bool
|
2022-03-19 15:13:34 +03:00
|
|
|
plugin *protogen.Plugin
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
|
|
|
var err error
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
g.plugin = plugin
|
2021-02-25 14:19:09 +03:00
|
|
|
g.standalone = *flagStandalone
|
|
|
|
g.debug = *flagDebug
|
|
|
|
g.components = *flagComponents
|
2021-12-26 01:09:11 +03:00
|
|
|
g.fieldaligment = *flagFieldaligment
|
2021-05-08 00:06:40 +03:00
|
|
|
g.tagPath = *flagTagPath
|
2021-10-23 02:55:43 +03:00
|
|
|
g.openapiFile = *flagOpenapiFile
|
2022-11-15 12:53:04 +03:00
|
|
|
g.reflection = *flagReflection
|
2021-02-25 22:11:08 +03:00
|
|
|
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
|
2021-02-25 14:19:09 +03:00
|
|
|
|
2023-07-31 00:54:44 +03:00
|
|
|
genClient := true
|
|
|
|
genServer := true
|
2021-12-26 01:09:11 +03:00
|
|
|
var genNone bool
|
2021-03-26 15:00:55 +03:00
|
|
|
|
|
|
|
if strings.Contains(g.components, "server") {
|
|
|
|
genServer = true
|
|
|
|
}
|
|
|
|
if strings.Contains(g.components, "client") {
|
|
|
|
genClient = true
|
|
|
|
}
|
2021-12-26 01:09:11 +03:00
|
|
|
if strings.Contains(g.components, "none") {
|
|
|
|
genNone = true
|
|
|
|
}
|
2021-03-30 18:30:58 +03:00
|
|
|
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
|
2021-12-26 01:09:11 +03:00
|
|
|
if !genServer && !genClient && !genNone {
|
2021-03-30 18:30:58 +03:00
|
|
|
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)
|
2023-10-18 01:01:30 +03:00
|
|
|
if err == nil {
|
|
|
|
err = g.writeErrors(plugin)
|
|
|
|
}
|
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)
|
2022-03-20 14:18:35 +03:00
|
|
|
case "grpc", "drpc", "rpc":
|
2022-03-20 15:08:04 +03:00
|
|
|
err = g.rpcGenerate(component, 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-10-23 02:55:43 +03:00
|
|
|
case "openapiv3":
|
2024-06-11 20:45:09 +03:00
|
|
|
err = g.openapiv3Generate(plugin)
|
|
|
|
case "graphqls":
|
|
|
|
err = g.graphqlsGenerate(plugin)
|
2021-12-26 01:09:11 +03:00
|
|
|
case "none":
|
|
|
|
break
|
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-12-26 01:09:11 +03:00
|
|
|
if err = g.fieldAlign(plugin); err != nil {
|
|
|
|
plugin.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
return nil
|
|
|
|
}
|