Skip to content

Commit

Permalink
Fixes #6: Support marshallers for unexported types.
Browse files Browse the repository at this point in the history
  • Loading branch information
vstarodub committed Mar 30, 2016
1 parent 9a3d2c6 commit 2c16fcd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ 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 }")
}

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
}
Expand Down Expand Up @@ -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 {")
Expand Down
8 changes: 6 additions & 2 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions tests/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var testCases = []struct {
{&optsValue, optsString},
{&rawValue, rawString},
{&stdMarshalerValue, stdMarshalerString},
{&unexportedStructValue, unexportedStructString},
}

func TestMarshal(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions tests/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`

0 comments on commit 2c16fcd

Please sign in to comment.