diff --git a/genmethod.go b/genmethod.go index 70222bc..185e72b 100644 --- a/genmethod.go +++ b/genmethod.go @@ -206,7 +206,15 @@ func (m *marshalMethod) marshalConversions(from, to Var, format string) (s []Sta for _, f := range m.mtyp.Fields { accessFrom := Dotted{Receiver: from, Name: f.name} accessTo := Dotted{Receiver: to, Name: f.name} - s = append(s, m.convert(accessFrom, accessTo, f.origTyp, f.typ)...) + conversion := m.convert(accessFrom, accessTo, f.origTyp, f.typ) + if underlyingMap(f.origTyp) != nil || underlyingSlice(f.origTyp) != nil { + // Preserve nil maps and slices. + conversion = []Statement{If{ + Condition: NotEqual{Lhs: accessFrom, Rhs: NIL}, + Body: conversion, + }} + } + s = append(s, conversion...) } return s } diff --git a/internal/tests/mapconv/output.go b/internal/tests/mapconv/output.go index 801c7d7..d69b391 100644 --- a/internal/tests/mapconv/output.go +++ b/internal/tests/mapconv/output.go @@ -13,13 +13,17 @@ func (x *X) MarshalJSON() ([]byte, error) { Named namedMap2 } var enc XJSON - enc.Map = make(map[replacedString]replacedInt, len(x.Map)) - for k, v := range x.Map { - enc.Map[replacedString(k)] = replacedInt(v) - } - enc.Named = make(namedMap2, len(x.Named)) - for k, v := range x.Named { - enc.Named[replacedString(k)] = replacedInt(v) + if x.Map != nil { + enc.Map = make(map[replacedString]replacedInt, len(x.Map)) + for k, v := range x.Map { + enc.Map[replacedString(k)] = replacedInt(v) + } + } + if x.Named != nil { + enc.Named = make(namedMap2, len(x.Named)) + for k, v := range x.Named { + enc.Named[replacedString(k)] = replacedInt(v) + } } return json.Marshal(&enc) } @@ -58,13 +62,17 @@ func (x *X) MarshalYAML() (interface{}, error) { Named namedMap2 } var enc XYAML - enc.Map = make(map[replacedString]replacedInt, len(x.Map)) - for k, v := range x.Map { - enc.Map[replacedString(k)] = replacedInt(v) - } - enc.Named = make(namedMap2, len(x.Named)) - for k, v := range x.Named { - enc.Named[replacedString(k)] = replacedInt(v) + if x.Map != nil { + enc.Map = make(map[replacedString]replacedInt, len(x.Map)) + for k, v := range x.Map { + enc.Map[replacedString(k)] = replacedInt(v) + } + } + if x.Named != nil { + enc.Named = make(namedMap2, len(x.Named)) + for k, v := range x.Named { + enc.Named[replacedString(k)] = replacedInt(v) + } } return &enc, nil } diff --git a/internal/tests/sliceconv/output.go b/internal/tests/sliceconv/output.go index 44626d1..b7f7971 100644 --- a/internal/tests/sliceconv/output.go +++ b/internal/tests/sliceconv/output.go @@ -13,13 +13,17 @@ func (x *X) MarshalJSON() ([]byte, error) { Named namedSlice2 } var enc XJSON - enc.Slice = make([]replacedInt, len(x.Slice)) - for k, v := range x.Slice { - enc.Slice[k] = replacedInt(v) - } - enc.Named = make(namedSlice2, len(x.Named)) - for k, v := range x.Named { - enc.Named[k] = replacedInt(v) + if x.Slice != nil { + enc.Slice = make([]replacedInt, len(x.Slice)) + for k, v := range x.Slice { + enc.Slice[k] = replacedInt(v) + } + } + if x.Named != nil { + enc.Named = make(namedSlice2, len(x.Named)) + for k, v := range x.Named { + enc.Named[k] = replacedInt(v) + } } return json.Marshal(&enc) } @@ -58,13 +62,17 @@ func (x *X) MarshalYAML() (interface{}, error) { Named namedSlice2 } var enc XYAML - enc.Slice = make([]replacedInt, len(x.Slice)) - for k, v := range x.Slice { - enc.Slice[k] = replacedInt(v) - } - enc.Named = make(namedSlice2, len(x.Named)) - for k, v := range x.Named { - enc.Named[k] = replacedInt(v) + if x.Slice != nil { + enc.Slice = make([]replacedInt, len(x.Slice)) + for k, v := range x.Slice { + enc.Slice[k] = replacedInt(v) + } + } + if x.Named != nil { + enc.Named = make(namedSlice2, len(x.Named)) + for k, v := range x.Named { + enc.Named[k] = replacedInt(v) + } } return &enc, nil }