Skip to content

Commit

Permalink
Support json, text dump options
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Aug 18, 2023
1 parent 9a8fff1 commit b64b4ea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ go install github.com/pubg/protoc-gen-debug

## Example


### Dump protoc's input
```sh
protoc \
--debug_out=./ \
--debug_opt=dump_file=request.pb.bin \
--debug_opt=parameter=expose_all=true \
--debug_opt=dump_binary=true \
--debug_opt=dump_json=true \
--debug_opt=file_binary=request.pb.bin \
--debug_opt=file_json=request.pb.json \
--debug_opt=parameter=expose_all=true:foo=bar \
-I ./ \
./example.proto
```
Expand All @@ -38,10 +40,16 @@ cat request.pb.bin | go run ./cmd/main.go
1. Check `Redirect input from`
2. Set dumped file path
3. Run as Debug Mode
4. Happy Debugging!

## Options
| Option | Description | Default |
|-----------|---------------------------------------|----------------|
| dump_file | A file name to save protoc's input to | request.pb.bin |
| parameter | Parameters for other plugins | "" |
| Option | Description | Type | Default |
|-------------|-------------------------------------|------------------------|-----------------|
| dump_binary | Enable or not to dump binary format | bool | true |
| file_binary | File name to save protoc's input | string | request.pb.bin |
| dump_json | Enable or not to dump json format | bool | false |
| file_json | File name to save protoc's input | string | request.pb.json |
| dump_text | Enable or not to dump text format | bool | false |
| file_text | File name to save protoc's input | string | request.pb.txt |
| parameter | Parameters for other plugins | colon seperated string | "" |

64 changes: 55 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,76 @@ package main

import (
"flag"
"strings"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

type PluginOptions struct {
DumpBinary bool
DumpJson bool
DumpText bool
FileBinary string
FileJson string
FileText string
Parameter string
}

func main() {
var flags flag.FlagSet
opts := protogen.Options{
ParamFunc: flags.Set,
}

dumpFile := flags.String("dump_file", "request.pb.bin", `dump file path`)
parameter := flags.String("parameter", "", `parameter`)
options := PluginOptions{}
flags.BoolVar(&options.DumpBinary, "dump_binary", true, "Enable dump protobuf request as binary format")
flags.BoolVar(&options.DumpJson, "dump_json", false, "Enable dump protobuf request as json format")
flags.BoolVar(&options.DumpText, "dump_text", false, "Enable dump protobuf request as text format")
flags.StringVar(&options.FileBinary, "file_binary", "request.pb.bin", "binary file path")
flags.StringVar(&options.FileJson, "file_json", "request.pb.json", "json file path")
flags.StringVar(&options.FileText, "file_text", "request.pb.txt", "text file path")
flags.StringVar(&options.Parameter, "parameter", "", "parameter")

opts.Run(func(plugin *protogen.Plugin) error {
plugin.Request.Parameter = parameter
buf, err := proto.Marshal(plugin.Request)
if err != nil {
return err
normalizedParam := strings.Replace(options.Parameter, ":", ",", -1)
plugin.Request.Parameter = &normalizedParam

if options.DumpBinary {
buf, err := proto.Marshal(plugin.Request)
if err != nil {
return err
}
_, err = plugin.NewGeneratedFile(options.FileBinary, "").Write(buf)
if err != nil {
return err
}

}
_, err = plugin.NewGeneratedFile(*dumpFile, "").Write(buf)
if err != nil {
return err

if options.DumpJson {
buf, err := protojson.Marshal(plugin.Request)
if err != nil {
return err
}
_, err = plugin.NewGeneratedFile(options.FileJson, "").Write(buf)
if err != nil {
return err
}
}

if options.DumpText {
buf, err := prototext.Marshal(plugin.Request)
if err != nil {
return err
}
_, err = plugin.NewGeneratedFile(options.FileText, "").Write(buf)
if err != nil {
return err
}
}

plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) | uint64(pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS)
Expand Down

0 comments on commit b64b4ea

Please sign in to comment.