mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Fixes #6: Support marshallers for unexported types.
This commit is contained in:
@@ -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
@@ -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.
|
||||
|
||||
@@ -26,6 +26,7 @@ var testCases = []struct {
|
||||
{&optsValue, optsString},
|
||||
{&rawValue, rawString},
|
||||
{&stdMarshalerValue, stdMarshalerString},
|
||||
{&unexportedStructValue, unexportedStructString},
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
|
||||
@@ -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"}`
|
||||
|
||||
Reference in New Issue
Block a user