Fixes #6: Support marshallers for unexported types.

This commit is contained in:
Victor Starodub
2016-03-30 15:55:25 +03:00
parent 9a3d2c62e3
commit 2c16fcd215
4 changed files with 18 additions and 3 deletions
+4 -1
View File
@@ -56,6 +56,7 @@ func (g *Generator) writeStub() error {
fmt.Fprintln(f, ")")
for _, t := range g.Types {
fmt.Fprintln(f)
if !g.NoStdMarshalers {
fmt.Fprintln(f, "func (*", t, ") MarshalJSON() ([]byte, error) { return nil, nil }")
fmt.Fprintln(f, "func (*", t, ") UnmarshalJSON([]byte) error { return nil }")
@@ -63,6 +64,8 @@ func (g *Generator) writeStub() error {
fmt.Fprintln(f, "func (*", t, ") MarshalEasyJSON(w *jwriter.Writer) {}")
fmt.Fprintln(f, "func (*", t, ") UnmarshalEasyJSON(l *jlexer.Lexer) {}")
fmt.Fprintln(f)
fmt.Fprintln(f, "type EasyJSON_exporter_"+t+" *"+t)
}
return nil
}
@@ -104,7 +107,7 @@ func (g *Generator) writeMain() (path string, err error) {
fmt.Fprintln(f, " g.NoStdMarshalers()")
}
for _, v := range g.Types {
fmt.Fprintln(f, " g.Add(pkg."+v+"{})")
fmt.Fprintln(f, " g.Add(pkg.EasyJSON_exporter_"+v+"(nil))")
}
fmt.Fprintln(f, " if err := g.Run(os.Stdout); err != nil {")
+6 -2
View File
@@ -112,8 +112,12 @@ func (g *Generator) addType(t reflect.Type) {
// Add requests to generate (un-)marshallers and en-/decoding functions for the type of given object.
func (g *Generator) Add(obj interface{}) {
g.addType(reflect.TypeOf(obj))
g.marshallers[reflect.TypeOf(obj)] = true
t := reflect.TypeOf(obj)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
g.addType(t)
g.marshallers[t] = true
}
// printHeader prints package declaration and imports.
+1
View File
@@ -26,6 +26,7 @@ var testCases = []struct {
{&optsValue, optsString},
{&rawValue, rawString},
{&stdMarshalerValue, stdMarshalerString},
{&unexportedStructValue, unexportedStructString},
}
func TestMarshal(t *testing.T) {
+7
View File
@@ -313,3 +313,10 @@ type StdMarshaler struct {
var stdMarshalerValue = StdMarshaler{T: time.Date(2016, 01, 02, 14, 15, 10, 0, time.UTC)}
var stdMarshalerString = `{"T":"2016-01-02T14:15:10Z"}`
type unexportedStruct struct {
Value string
}
var unexportedStructValue = unexportedStruct{"test"}
var unexportedStructString = `{"Value":"test"}`