forked from mitchellh/protoc-gen-go-json
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to protogen Plugin and protojson instead of jsonpb
- Loading branch information
Showing
8 changed files
with
96 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
build | ||
protoc-gen-go-json* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
`)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.