Revert "Ensure empty serialized slices are zero-length, not null"

This commit is contained in:
Jordan Liggitt 2017-05-11 13:15:30 -04:00
parent 5f805a5e66
commit 1bb19dfcc5
No known key found for this signature in database
GPG Key ID: 39928704103C7229

View File

@ -29,8 +29,6 @@ import (
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"reflect"
"github.com/golang/glog"
)
@ -792,15 +790,6 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
outMemberType = &copied
}
// Determine if our destination field is a slice that should be output when empty.
// If it is, ensure a nil source slice converts to a zero-length destination slice.
// See http://issue.k8s.io/43203
persistEmptySlice := false
if outMemberType.Kind == types.Slice {
jsonTag := reflect.StructTag(outMember.Tags).Get("json")
persistEmptySlice = len(jsonTag) > 0 && !strings.Contains(jsonTag, ",omitempty")
}
args := argsFromType(inMemberType, outMemberType).With("name", inMember.Name)
// try a direct memory copy for any type that has exactly equivalent values
@ -816,15 +805,7 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
sw.Do("out.$.name$ = *(*$.outType|raw$)($.Pointer|raw$(&in.$.name$))\n", args)
continue
case types.Slice:
if persistEmptySlice {
sw.Do("if in.$.name$ == nil {\n", args)
sw.Do("out.$.name$ = make($.outType|raw$, 0)\n", args)
sw.Do("} else {\n", nil)
sw.Do("out.$.name$ = *(*$.outType|raw$)($.Pointer|raw$(&in.$.name$))\n", args)
sw.Do("}\n", nil)
} else {
sw.Do("out.$.name$ = *(*$.outType|raw$)($.Pointer|raw$(&in.$.name$))\n", args)
}
sw.Do("out.$.name$ = *(*$.outType|raw$)($.Pointer|raw$(&in.$.name$))\n", args)
continue
}
}
@ -874,11 +855,7 @@ func (g *genConversion) doStruct(inType, outType *types.Type, sw *generator.Snip
sw.Do("in, out := &in.$.name$, &out.$.name$\n", args)
g.generateFor(inMemberType, outMemberType, sw)
sw.Do("} else {\n", nil)
if persistEmptySlice {
sw.Do("out.$.name$ = make($.outType|raw$, 0)\n", args)
} else {
sw.Do("out.$.name$ = nil\n", args)
}
sw.Do("out.$.name$ = nil\n", args)
sw.Do("}\n", nil)
case types.Struct:
if g.isDirectlyAssignable(inMemberType, outMemberType) {