Handle map[]struct{} special

This commit is contained in:
Clayton Coleman 2016-07-03 00:44:25 -04:00
parent 2f9feef9a6
commit 6eb04ae592
2 changed files with 18 additions and 4 deletions

View File

@ -356,10 +356,17 @@ func (g *genDeepCopy) doBuiltin(t *types.Type, sw *generator.SnippetWriter) {
func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
sw.Do("*out = make($.|raw$)\n", t)
if t.Key.IsAssignable() {
sw.Do("for key, val := range in {\n", nil)
if t.Elem.IsAssignable() {
switch {
case t.Elem.IsAnonymousStruct():
sw.Do("for key := range in {\n", nil)
sw.Do("(*out)[key] = struct{}{}\n", nil)
sw.Do("}\n", nil)
case t.Elem.IsAssignable():
sw.Do("for key, val := range in {\n", nil)
sw.Do("(*out)[key] = val\n", nil)
} else {
sw.Do("}\n", nil)
default:
sw.Do("for key, val := range in {\n", nil)
if g.canInlineTypeFn(g.context, t.Elem) {
sw.Do("newVal := new($.|raw$)\n", t.Elem)
funcName := g.funcNameTmpl(t.Elem)
@ -374,13 +381,14 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
sw.Do("(*out)[key] = newVal.($.|raw$)\n", t.Elem)
sw.Do("}\n", nil)
}
sw.Do("}\n", nil)
}
} else {
// TODO: Implement it when necessary.
sw.Do("for range in {\n", nil)
sw.Do("// FIXME: Copying unassignable keys unsupported $.|raw$\n", t.Key)
sw.Do("}\n", nil)
}
sw.Do("}\n", nil)
}
func (g *genDeepCopy) doSlice(t *types.Type, sw *generator.SnippetWriter) {

View File

@ -290,6 +290,12 @@ func (t *Type) IsAssignable() bool {
return t.Kind == Builtin || (t.Kind == Alias && t.Underlying.Kind == Builtin)
}
// IsAnonymousStruct returns true if the type is an anonymous struct or an alias
// to an anonymous struct.
func (t *Type) IsAnonymousStruct() bool {
return (t.Kind == Struct && t.Name.Name == "struct{}") || (t.Kind == Alias && t.Underlying.IsAnonymousStruct())
}
// A single struct member
type Member struct {
// The name of the member.