use non-pointer receiver for Marshal* methods

master
Felix Lange 8 years ago
parent 72fbea27b0
commit 75f278cf92
  1. 16
      genmethod.go
  2. 4
      internal/tests/mapconv/output.go
  3. 4
      internal/tests/nameclash/output.go
  4. 4
      internal/tests/sliceconv/output.go

@ -48,7 +48,7 @@ func writeFunction(w io.Writer, fs *token.FileSet, fn Function) {
func genUnmarshalJSON(mtyp *marshalerType) Function { func genUnmarshalJSON(mtyp *marshalerType) Function {
var ( var (
m = newMarshalMethod(mtyp) m = newMarshalMethod(mtyp)
recv = m.receiver() recv = m.receiver(true)
input = Name(m.scope.newIdent("input")) input = Name(m.scope.newIdent("input"))
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON"))
dec = Name(m.scope.newIdent("dec")) dec = Name(m.scope.newIdent("dec"))
@ -80,7 +80,7 @@ func genUnmarshalJSON(mtyp *marshalerType) Function {
func genMarshalJSON(mtyp *marshalerType) Function { func genMarshalJSON(mtyp *marshalerType) Function {
var ( var (
m = newMarshalMethod(mtyp) m = newMarshalMethod(mtyp)
recv = m.receiver() recv = m.receiver(false)
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "JSON"))
enc = Name(m.scope.newIdent("enc")) enc = Name(m.scope.newIdent("enc"))
json = Name(m.scope.parent.packageName("encoding/json")) json = Name(m.scope.parent.packageName("encoding/json"))
@ -108,7 +108,7 @@ func genMarshalJSON(mtyp *marshalerType) Function {
func genUnmarshalYAML(mtyp *marshalerType) Function { func genUnmarshalYAML(mtyp *marshalerType) Function {
var ( var (
m = newMarshalMethod(mtyp) m = newMarshalMethod(mtyp)
recv = m.receiver() recv = m.receiver(true)
unmarshal = Name(m.scope.newIdent("unmarshal")) unmarshal = Name(m.scope.newIdent("unmarshal"))
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML"))
dec = Name(m.scope.newIdent("dec")) dec = Name(m.scope.newIdent("dec"))
@ -136,7 +136,7 @@ func genUnmarshalYAML(mtyp *marshalerType) Function {
func genMarshalYAML(mtyp *marshalerType) Function { func genMarshalYAML(mtyp *marshalerType) Function {
var ( var (
m = newMarshalMethod(mtyp) m = newMarshalMethod(mtyp)
recv = m.receiver() recv = m.receiver(false)
intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML")) intertyp = m.intermediateType(m.scope.newIdent(m.mtyp.orig.Obj().Name() + "YAML"))
enc = Name(m.scope.newIdent("enc")) enc = Name(m.scope.newIdent("enc"))
) )
@ -154,9 +154,13 @@ func genMarshalYAML(mtyp *marshalerType) Function {
return fn return fn
} }
func (m *marshalMethod) receiver() Receiver { func (m *marshalMethod) receiver(pointer bool) Receiver {
letter := strings.ToLower(m.mtyp.name[:1]) 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 { func (m *marshalMethod) intermediateType(name string) Struct {

@ -7,7 +7,7 @@ import (
"errors" "errors"
) )
func (x *X) MarshalJSON() ([]byte, error) { func (x X) MarshalJSON() ([]byte, error) {
type XJSON struct { type XJSON struct {
Map map[replacedString]replacedInt Map map[replacedString]replacedInt
Named namedMap2 Named namedMap2
@ -56,7 +56,7 @@ func (x *X) UnmarshalJSON(input []byte) error {
return nil return nil
} }
func (x *X) MarshalYAML() (interface{}, error) { func (x X) MarshalYAML() (interface{}, error) {
type XYAML struct { type XYAML struct {
Map map[replacedString]replacedInt Map map[replacedString]replacedInt
Named namedMap2 Named namedMap2

@ -9,7 +9,7 @@ import (
json0 "github.com/fjl/gencodec/internal/clashjson" json0 "github.com/fjl/gencodec/internal/clashjson"
) )
func (y *Y) MarshalJSON() ([]byte, error) { func (y Y) MarshalJSON() ([]byte, error) {
type YJSON0 struct { type YJSON0 struct {
Foo *json0.Foo `optional:"true"` Foo *json0.Foo `optional:"true"`
Foo2 *json0.Foo `optional:"true"` Foo2 *json0.Foo `optional:"true"`
@ -58,7 +58,7 @@ func (y *Y) UnmarshalJSON(input []byte) error {
return nil return nil
} }
func (y *Y) MarshalYAML() (interface{}, error) { func (y Y) MarshalYAML() (interface{}, error) {
type YYAML struct { type YYAML struct {
Foo *json0.Foo `optional:"true"` Foo *json0.Foo `optional:"true"`
Foo2 *json0.Foo `optional:"true"` Foo2 *json0.Foo `optional:"true"`

@ -7,7 +7,7 @@ import (
"errors" "errors"
) )
func (x *X) MarshalJSON() ([]byte, error) { func (x X) MarshalJSON() ([]byte, error) {
type XJSON struct { type XJSON struct {
Slice []replacedInt Slice []replacedInt
Named namedSlice2 Named namedSlice2
@ -56,7 +56,7 @@ func (x *X) UnmarshalJSON(input []byte) error {
return nil return nil
} }
func (x *X) MarshalYAML() (interface{}, error) { func (x X) MarshalYAML() (interface{}, error) {
type XYAML struct { type XYAML struct {
Slice []replacedInt Slice []replacedInt
Named namedSlice2 Named namedSlice2

Loading…
Cancel
Save