From 75f278cf921acc67b5e2f60727b896c09ab8a204 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 27 Mar 2017 11:44:07 +0200 Subject: [PATCH] use non-pointer receiver for Marshal* methods --- genmethod.go | 16 ++++++++++------ internal/tests/mapconv/output.go | 4 ++-- internal/tests/nameclash/output.go | 4 ++-- internal/tests/sliceconv/output.go | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/genmethod.go b/genmethod.go index 185e72b..69a66a8 100644 --- a/genmethod.go +++ b/genmethod.go @@ -48,7 +48,7 @@ func writeFunction(w io.Writer, fs *token.FileSet, fn Function) { func genUnmarshalJSON(mtyp *marshalerType) Function { var ( m = newMarshalMethod(mtyp) - recv = m.receiver() + recv = m.receiver(true) input = Name(m.scope.newIdent("input")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON")) dec = Name(m.scope.newIdent("dec")) @@ -80,7 +80,7 @@ func genUnmarshalJSON(mtyp *marshalerType) Function { func genMarshalJSON(mtyp *marshalerType) Function { var ( m = newMarshalMethod(mtyp) - recv = m.receiver() + recv = m.receiver(false) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON")) enc = Name(m.scope.newIdent("enc")) json = Name(m.scope.parent.packageName("encoding/json")) @@ -108,7 +108,7 @@ func genMarshalJSON(mtyp *marshalerType) Function { func genUnmarshalYAML(mtyp *marshalerType) Function { var ( m = newMarshalMethod(mtyp) - recv = m.receiver() + recv = m.receiver(true) unmarshal = Name(m.scope.newIdent("unmarshal")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML")) dec = Name(m.scope.newIdent("dec")) @@ -136,7 +136,7 @@ func genUnmarshalYAML(mtyp *marshalerType) Function { func genMarshalYAML(mtyp *marshalerType) Function { var ( m = newMarshalMethod(mtyp) - recv = m.receiver() + recv = m.receiver(false) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML")) enc = Name(m.scope.newIdent("enc")) ) @@ -154,9 +154,13 @@ func genMarshalYAML(mtyp *marshalerType) Function { return fn } -func (m *marshalMethod) receiver() Receiver { +func (m *marshalMethod) receiver(pointer bool) Receiver { letter := strings.ToLower(m.mtyp.name[:1]) - return Receiver{Name: m.scope.newIdent(letter), Type: Star{Value: Name(m.mtyp.name)}} + r := Receiver{Name: m.scope.newIdent(letter), Type: Name(m.mtyp.name)} + if pointer { + r.Type = Star{Value: r.Type} + } + return r } func (m *marshalMethod) intermediateType(name string) Struct { diff --git a/internal/tests/mapconv/output.go b/internal/tests/mapconv/output.go index d69b391..9a8d2f0 100644 --- a/internal/tests/mapconv/output.go +++ b/internal/tests/mapconv/output.go @@ -7,7 +7,7 @@ import ( "errors" ) -func (x *X) MarshalJSON() ([]byte, error) { +func (x X) MarshalJSON() ([]byte, error) { type XJSON struct { Map map[replacedString]replacedInt Named namedMap2 @@ -56,7 +56,7 @@ func (x *X) UnmarshalJSON(input []byte) error { return nil } -func (x *X) MarshalYAML() (interface{}, error) { +func (x X) MarshalYAML() (interface{}, error) { type XYAML struct { Map map[replacedString]replacedInt Named namedMap2 diff --git a/internal/tests/nameclash/output.go b/internal/tests/nameclash/output.go index d8a3a36..6b67fac 100644 --- a/internal/tests/nameclash/output.go +++ b/internal/tests/nameclash/output.go @@ -9,7 +9,7 @@ import ( json0 "github.com/fjl/gencodec/internal/clashjson" ) -func (y *Y) MarshalJSON() ([]byte, error) { +func (y Y) MarshalJSON() ([]byte, error) { type YJSON0 struct { Foo *json0.Foo `optional:"true"` Foo2 *json0.Foo `optional:"true"` @@ -58,7 +58,7 @@ func (y *Y) UnmarshalJSON(input []byte) error { return nil } -func (y *Y) MarshalYAML() (interface{}, error) { +func (y Y) MarshalYAML() (interface{}, error) { type YYAML struct { Foo *json0.Foo `optional:"true"` Foo2 *json0.Foo `optional:"true"` diff --git a/internal/tests/sliceconv/output.go b/internal/tests/sliceconv/output.go index b7f7971..9c7fcea 100644 --- a/internal/tests/sliceconv/output.go +++ b/internal/tests/sliceconv/output.go @@ -7,7 +7,7 @@ import ( "errors" ) -func (x *X) MarshalJSON() ([]byte, error) { +func (x X) MarshalJSON() ([]byte, error) { type XJSON struct { Slice []replacedInt Named namedSlice2 @@ -56,7 +56,7 @@ func (x *X) UnmarshalJSON(input []byte) error { return nil } -func (x *X) MarshalYAML() (interface{}, error) { +func (x X) MarshalYAML() (interface{}, error) { type XYAML struct { Slice []replacedInt Named namedSlice2