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 {
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 {

@ -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

@ -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"`

@ -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

Loading…
Cancel
Save