Skip to content

Commit

Permalink
Convert to protogen Plugin and protojson instead of jsonpb
Browse files Browse the repository at this point in the history
  • Loading branch information
chkohner committed Jan 10, 2021
1 parent 364b693 commit 1643cb1
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 371 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*
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.

89 changes: 45 additions & 44 deletions gen/template.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,94 @@
package gen

import (
"bytes"
"strings"
"io"
"text/template"

"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"google.golang.org/protobuf/compiler/protogen"
)

// Options are the options to set for rendering the template.
type Options struct {
EnumsAsInts bool
EmitDefaults bool
OrigName bool
AllowUnknownFields bool
}

// This function is called with a param which contains the entire definition of a method.
func applyTemplate(f *descriptor.File, opts Options) (string, error) {
w := bytes.NewBuffer(nil)
func ApplyTemplate(w io.Writer, f *protogen.File, opts Options) error {

if err := headerTemplate.Execute(w, tplHeader{
File: f,
}); err != nil {
return "", err
return err
}

for _, msg := range f.Messages {
glog.V(2).Infof("Processing %s", msg.GetName())
if msg.Options != nil && msg.Options.GetMapEntry() {
glog.V(2).Infof("Skipping %s, mapentry message", msg.GetName())
return applyMessages(w, f.Messages, opts)
}

func applyMessages(w io.Writer, msgs []*protogen.Message, opts Options) error {
for _, m := range msgs {

if m.Desc.IsMapEntry() {
glog.V(2).Infof("Skipping %s, mapentry message", m.GoIdent.GoName)
continue
}
msgName := camelCase(*msg.Name)
msg.Name = &msgName

glog.V(2).Infof("Processing %s", m.GoIdent.GoName)
if err := messageTemplate.Execute(w, tplMessage{
Message: msg,
Message: m,
Options: opts,
}); err != nil {
return "", err
return err
}

if err := applyMessages(w, m.Messages, opts); err != nil {
return err
}

}

return w.String(), nil
return nil
}

type tplHeader struct {
*descriptor.File
*protogen.File
}

type tplMessage struct {
*descriptor.Message
*protogen.Message
Options
}

// TypeName returns the name of the type for this message. This logic
// is based on the logic of Descriptor.TypeName in golang/protobuf.
func (t tplMessage) TypeName() string {
if len(t.Outers) > 0 {
return strings.Join(t.Outers, "_") + "_" + *t.Name
}

return *t.Name
}

var (
headerTemplate = template.Must(template.New("header").Parse(`
// Code generated by protoc-gen-go-json. DO NOT EDIT.
// source: {{.GetName}}
// source: {{.Proto.Name}}
package {{.GoPkg.Name}}
package {{.GoPackageName}}
import (
"bytes"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/protobuf/encoding/protojson"
)
`))

messageTemplate = template.Must(template.New("message").Parse(`
// MarshalJSON implements json.Marshaler
func (msg *{{.TypeName}}) MarshalJSON() ([]byte,error) {
var buf bytes.Buffer
err := (&jsonpb.Marshaler{
EnumsAsInts: {{.EnumsAsInts}},
EmitDefaults: {{.EmitDefaults}},
OrigName: {{.OrigName}},
}).Marshal(&buf, msg)
return buf.Bytes(), err
func (msg *{{.GoIdent.GoName}}) MarshalJSON() ([]byte,error) {
return protojson.MarshalOptions {
UseEnumNumbers: {{.EnumsAsInts}},
EmitUnpopulated: {{.EmitDefaults}},
UseProtoNames: {{.OrigName}},
}.Marshal(msg)
}
// UnmarshalJSON implements json.Unmarshaler
func (msg *{{.TypeName}}) UnmarshalJSON(b []byte) error {
return (&jsonpb.Unmarshaler{
AllowUnknownFields: {{.AllowUnknownFields}},
}).Unmarshal(bytes.NewReader(b), msg)
func (msg *{{.GoIdent.GoName}}) UnmarshalJSON(b []byte) error {
return protojson.UnmarshalOptions {
DiscardUnknown: {{.AllowUnknownFields}},
}.Unmarshal(b, msg)
}
`))
)
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ go 1.12
require (
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.4.1
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.3.0
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
Loading

0 comments on commit 1643cb1

Please sign in to comment.