Skip to content

Commit

Permalink
Reuse types.TypeString
Browse files Browse the repository at this point in the history
Use a custom qualifier to grab any packages that need importing.
  • Loading branch information
Viktor authored and urandom committed Apr 28, 2021
1 parent da2dd74 commit bfdfaf5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
43 changes: 15 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func walkType(source, sink, x string, m types.Type, w io.Writer, imports map[str
walkType(source+"."+fname, sink+"."+fname, x, field.Type(), w, imports, skips, generating, depth)
}
case *types.Slice:
kind := getElemType(v.Elem(), x, imports, false)
kind := getElemType(v.Elem(), x, imports)

idx := "i"
if depth > 1 {
Expand Down Expand Up @@ -400,7 +400,7 @@ func walkType(source, sink, x string, m types.Type, w io.Writer, imports map[str
fmt.Fprintf(w, "if %s != nil {\n", source)

if e, ok := v.Elem().(methoder); !ok || initial || !reuseDeepCopy(source, sink, e, true, generating, w) {
kind := getElemType(v.Elem(), x, imports, true)
kind := getElemType(v.Elem(), x, imports)

fmt.Fprintf(w, `%s = new(%s)
*%s = *%s
Expand All @@ -411,15 +411,15 @@ func walkType(source, sink, x string, m types.Type, w io.Writer, imports map[str

fmt.Fprintf(w, "}\n")
case *types.Chan:
kind := getElemType(v.Elem(), x, imports, false)
kind := getElemType(v.Elem(), x, imports)

fmt.Fprintf(w, `if %s != nil {
%s = make(chan %s, cap(%s))
}
`, source, sink, kind, source)
case *types.Map:
kkind := getElemType(v.Key(), x, imports, false)
vkind := getElemType(v.Elem(), x, imports, false)
kkind := getElemType(v.Key(), x, imports)
vkind := getElemType(v.Elem(), x, imports)

key, val := "k", "v"

Expand Down Expand Up @@ -480,31 +480,18 @@ func walkType(source, sink, x string, m types.Type, w io.Writer, imports map[str

}

func getElemType(t types.Type, x string, imports map[string]string, rawkind bool) string {
obj := objFromType(t)
var name, kind string
if obj != nil {
pkg := obj.Obj().Pkg()
if pkg != nil {
name = pkg.Name()
if name != x {
if path, ok := imports[name]; ok && path != pkg.Path() {
name = strings.ReplaceAll(pkg.Path(), "/", "_")
}
imports[name] = pkg.Path()
kind += name + "."
func getElemType(t types.Type, x string, imports map[string]string) string {
kind := types.TypeString(t, func(p *types.Package) string {
name := p.Name()
if name != x {
if path, ok := imports[name]; ok && path != p.Path() {
name = strings.ReplaceAll(p.Path(), "/", "_")
}
imports[name] = p.Path()
return name
}
kind += obj.Obj().Name()
} else {
kind += t.String()
}

_, pointer := t.(*types.Pointer)

if !rawkind && pointer && kind[0] != '*' {
kind = "*" + kind
}
return ""
})

return kind
}
Expand Down
28 changes: 28 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Test_run(t *testing.T) {
{name: "pointer that implements DeepCopy", types: typesVal{"SomeStruct"}, path: "./testdata/pointer_that_implements_deepcopy/somepkg", want: []byte(PointerThatImplementsDeepcopy)},
{name: "issue 10, slice with element that contains pointer and value", types: typesVal{"StructCH"}, path: "./testdata", want: []byte(Issue10StructCH)},
{name: "issue 12, nested slices", types: typesVal{"I12NestedSlices"}, path: "./testdata", want: []byte(Issue12NestedSlices)},
{name: "issue 12, map with slice value", types: typesVal{"I12StructWithMapOfSlices"}, path: "./testdata", want: []byte(Issue12MapWithSliceValues)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -394,4 +395,31 @@ func (o I12NestedSlices) DeepCopy() I12NestedSlices {
}
return cp
}`

Issue12MapWithSliceValues = `// generated by deep-copy; DO NOT EDIT.
package testdata
// DeepCopy generates a deep copy of I12StructWithMapOfSlices
func (o I12StructWithMapOfSlices) DeepCopy() I12StructWithMapOfSlices {
var cp I12StructWithMapOfSlices = o
if o.Sc1 != nil {
cp.Sc1 = make(map[string][]I12StructWithSlices, len(o.Sc1))
for k2, v2 := range o.Sc1 {
var cp_Sc1_v2 []I12StructWithSlices
if v2 != nil {
cp_Sc1_v2 = make([]I12StructWithSlices, len(v2))
copy(cp_Sc1_v2, v2)
for i3 := range v2 {
if v2[i3].Name != nil {
cp_Sc1_v2[i3].Name = make([]string, len(v2[i3].Name))
copy(cp_Sc1_v2[i3].Name, v2[i3].Name)
}
}
}
cp.Sc1[k2] = cp_Sc1_v2
}
}
return cp
}`
)
9 changes: 9 additions & 0 deletions testdata/issue_12_map_with_slice_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testdata

type I12StructWithMapOfSlices struct {
Sc1 map[string][]I12StructWithSlices
}

type I12StructWithSlices struct {
Name []string
}

0 comments on commit bfdfaf5

Please sign in to comment.