Skip to content

Commit

Permalink
Merge pull request mitchellh#9 from chkohner/protogen-plugin-update
Browse files Browse the repository at this point in the history
Convert to protogen Plugin and protojson instead of jsonpb
  • Loading branch information
mitchellh authored Aug 10, 2021
2 parents 364b693 + abed81c commit 2263edb
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 379 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
protoc-gen-go-json*
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a plugin for the Google Protocol Buffers compiler
[protoc](https://github.com/protocolbuffers/protobuf) that generates
code to implement [json.Marshaler](https://golang.org/pkg/encoding/json/#Marshaler)
and [json.Unmarshaler](https://golang.org/pkg/encoding/json/#Unmarshaler)
using [jsonpb](https://godoc.org/github.com/golang/protobuf/jsonpb).
using [protojson](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson).

This enables Go-generated protobuf messages to be embedded directly within
other structs and encoded with the standard JSON library, since the standard
Expand Down Expand Up @@ -67,19 +67,31 @@ json.Unmarshal(bs, &result)

The generator supports options you can specify via the command-line:

* `enums_as_ints` - Render enums as integers instead of strings.
* `emit_defaults` - Render fields with zero values.
* `orig_name` - Use original (.proto file) name for fields.
* `allow_unknown` - Allow messages to contain unknown fields when unmarshaling
- `enums_as_ints={bool}` - Render enums as integers instead of strings.
- `emit_defaults={bool}` - Render fields with zero values.
- `orig_name={bool}` - Use original (.proto file) name for fields.
- `allow_unknown={bool}` - Allow messages to contain unknown fields when unmarshaling

It also includes the "standard" options available to all [protogen](https://pkg.go.dev/google.golang.org/protobuf/compiler/protogen?tab=doc)-based plugins:

- `import_path={path}` - Override the import path
- `paths=source_relative` - Derive the output path from the input path
- etc.

These can be set as part of the `--go-json_out` value:

```
protoc --go-json_out=emit_defaults:.
```sh
protoc --go-json_opt=emit_defaults=true:.
```

You can specify multiple using a `,`:

```sh
protoc --go-json_out=enums_as_ints=true,emit_defaults=true:.
```
protoc --go-json_out=enums_as_ints,emit_defaults:.

Alternatively, you may also specify options using the `--go-json_opt` value:

```sh
protoc --go-json_out:. --go-json_opt=emit_defaults=true,enums_as_ints=true
```
58 changes: 25 additions & 33 deletions e2e/e2e.pb.json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 0 additions & 58 deletions gen/camelcase.go

This file was deleted.

72 changes: 0 additions & 72 deletions gen/generator.go

This file was deleted.

Loading

0 comments on commit 2263edb

Please sign in to comment.