@@ -2,30 +2,76 @@ package main
22
33import (
44 "flag"
5+ "strings"
56
67 "google.golang.org/protobuf/compiler/protogen"
8+ "google.golang.org/protobuf/encoding/protojson"
9+ "google.golang.org/protobuf/encoding/prototext"
710 "google.golang.org/protobuf/proto"
811 "google.golang.org/protobuf/types/pluginpb"
912)
1013
14+ type PluginOptions struct {
15+ DumpBinary bool
16+ DumpJson bool
17+ DumpText bool
18+ FileBinary string
19+ FileJson string
20+ FileText string
21+ Parameter string
22+ }
23+
1124func main () {
1225 var flags flag.FlagSet
1326 opts := protogen.Options {
1427 ParamFunc : flags .Set ,
1528 }
1629
17- dumpFile := flags .String ("dump_file" , "request.pb.bin" , `dump file path` )
18- parameter := flags .String ("parameter" , "" , `parameter` )
30+ options := PluginOptions {}
31+ flags .BoolVar (& options .DumpBinary , "dump_binary" , true , "Enable dump protobuf request as binary format" )
32+ flags .BoolVar (& options .DumpJson , "dump_json" , false , "Enable dump protobuf request as json format" )
33+ flags .BoolVar (& options .DumpText , "dump_text" , false , "Enable dump protobuf request as text format" )
34+ flags .StringVar (& options .FileBinary , "file_binary" , "request.pb.bin" , "binary file path" )
35+ flags .StringVar (& options .FileJson , "file_json" , "request.pb.json" , "json file path" )
36+ flags .StringVar (& options .FileText , "file_text" , "request.pb.txt" , "text file path" )
37+ flags .StringVar (& options .Parameter , "parameter" , "" , "parameter" )
1938
2039 opts .Run (func (plugin * protogen.Plugin ) error {
21- plugin .Request .Parameter = parameter
22- buf , err := proto .Marshal (plugin .Request )
23- if err != nil {
24- return err
40+ normalizedParam := strings .Replace (options .Parameter , ":" , "," , - 1 )
41+ plugin .Request .Parameter = & normalizedParam
42+
43+ if options .DumpBinary {
44+ buf , err := proto .Marshal (plugin .Request )
45+ if err != nil {
46+ return err
47+ }
48+ _ , err = plugin .NewGeneratedFile (options .FileBinary , "" ).Write (buf )
49+ if err != nil {
50+ return err
51+ }
52+
2553 }
26- _ , err = plugin .NewGeneratedFile (* dumpFile , "" ).Write (buf )
27- if err != nil {
28- return err
54+
55+ if options .DumpJson {
56+ buf , err := protojson .Marshal (plugin .Request )
57+ if err != nil {
58+ return err
59+ }
60+ _ , err = plugin .NewGeneratedFile (options .FileJson , "" ).Write (buf )
61+ if err != nil {
62+ return err
63+ }
64+ }
65+
66+ if options .DumpText {
67+ buf , err := prototext .Marshal (plugin .Request )
68+ if err != nil {
69+ return err
70+ }
71+ _ , err = plugin .NewGeneratedFile (options .FileText , "" ).Write (buf )
72+ if err != nil {
73+ return err
74+ }
2975 }
3076
3177 plugin .SupportedFeatures = uint64 (pluginpb .CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL ) | uint64 (pluginpb .CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS )
0 commit comments