diff --git a/plugin/deepcopy/deepcopy.go b/plugin/deepcopy/deepcopy.go index debf42d..2a4f4a6 100644 --- a/plugin/deepcopy/deepcopy.go +++ b/plugin/deepcopy/deepcopy.go @@ -369,7 +369,10 @@ func (g *gen) genField(fieldType types.Type, thisField, thatField string) error p.P("%s = make(%s, len(%s))", thatField, g.TypeString(typ), thisField) p.Out() p.P("}") // not nil - if canCopy(typ.Elem()) { + named, isNamed := fieldType.(*types.Named) + if isNamed && hasDeepCopyMethod(named) { + p.P("%s.DeepCopy(%s)", wrap(thisField), thatField) + } else if canCopy(typ.Elem()) { p.P("copy(%s, %s)", thatField, thisField) } else { p.P("%s(%s, %s)", g.GetFuncName(typ), thatField, thisField) @@ -381,7 +384,12 @@ func (g *gen) genField(fieldType types.Type, thisField, thatField string) error p.P("if %s != nil {", thisField) p.In() p.P("%s = make(%s, len(%s))", thatField, g.TypeString(typ), thisField) - p.P("%s(%s, %s)", g.GetFuncName(typ), thatField, thisField) + named, isNamed := fieldType.(*types.Named) + if isNamed && hasDeepCopyMethod(named) { + p.P("%s.DeepCopy(%s)", wrap(thisField), thatField) + } else { + p.P("%s(%s, %s)", g.GetFuncName(typ), thatField, thisField) + } p.Out() p.P("} else {") p.In() diff --git a/test/normal/deepcopy_test.go b/test/normal/deepcopy_test.go index 79f04bd..43a1525 100644 --- a/test/normal/deepcopy_test.go +++ b/test/normal/deepcopy_test.go @@ -84,3 +84,22 @@ func TestDeepCopyMapNilEntry(t *testing.T) { t.Fatalf("want %v got %v\n this = %#v, that = %#v\n", want, got, this, that) } } + +func TestDeepCopyCustomMap(t *testing.T) { + this := customMap{ + "a": "A", + } + that := customMap{} + deepcopy(this, that) + if that["a"] != "copy of A" { + t.Fatalf("expected use of customMap.DeepCopy") + } +} + +type customMap map[string]string + +func (c customMap) DeepCopy(to customMap) { + for k, v := range c { + to[k] = "copy of " + v + } +}