summaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
authorMaisem Ali <maisem@tailscale.com>2023-08-20 13:16:06 -0400
committerMaisem Ali <maisem@gmail.com>2023-08-20 13:47:26 -0400
commit8a5ec72c85de6d84ccdbf97ff6ea288cacb5a0a9 (patch)
tree539ac78746d1252e3df5fa2974de858265f678fb /cmd
parent4511e7d64e1b7483f8ac07298923cb27f2a863ce (diff)
downloadtailscale-8a5ec72c85de6d84ccdbf97ff6ea288cacb5a0a9.tar.xz
tailscale-8a5ec72c85de6d84ccdbf97ff6ea288cacb5a0a9.zip
cmd/cloner: use maps.Clone and ptr.To
Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cloner/cloner.go26
-rw-r--r--cmd/derper/depaware.txt2
-rw-r--r--cmd/tailscale/depaware.txt2
-rw-r--r--cmd/tailscaled/depaware.txt2
-rw-r--r--cmd/viewer/tests/tests_clone.go50
5 files changed, 28 insertions, 54 deletions
diff --git a/cmd/cloner/cloner.go b/cmd/cloner/cloner.go
index a2620c8d7..4e8e2119b 100644
--- a/cmd/cloner/cloner.go
+++ b/cmd/cloner/cloner.go
@@ -126,8 +126,8 @@ func gen(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named) {
writef("for i := range dst.%s {", fname)
if ptr, isPtr := ft.Elem().(*types.Pointer); isPtr {
if _, isBasic := ptr.Elem().Underlying().(*types.Basic); isBasic {
- writef("\tx := *src.%s[i]", fname)
- writef("\tdst.%s[i] = &x", fname)
+ it.Import("tailscale.com/types/ptr")
+ writef("\tdst.%s[i] = ptr.To(*src.%s[i])", fname, fname)
} else {
writef("\tdst.%s[i] = src.%s[i].Clone()", fname, fname)
}
@@ -145,41 +145,41 @@ func gen(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named) {
writef("dst.%s = src.%s.Clone()", fname, fname)
continue
}
- n := it.QualifiedName(ft.Elem())
+ it.Import("tailscale.com/types/ptr")
writef("if dst.%s != nil {", fname)
- writef("\tdst.%s = new(%s)", fname, n)
- writef("\t*dst.%s = *src.%s", fname, fname)
+ writef("\tdst.%s = ptr.To(*src.%s)", fname, fname)
if codegen.ContainsPointers(ft.Elem()) {
writef("\t" + `panic("TODO pointers in pointers")`)
}
writef("}")
case *types.Map:
elem := ft.Elem()
- writef("if dst.%s != nil {", fname)
- writef("\tdst.%s = map[%s]%s{}", fname, it.QualifiedName(ft.Key()), it.QualifiedName(elem))
if sliceType, isSlice := elem.(*types.Slice); isSlice {
n := it.QualifiedName(sliceType.Elem())
+ writef("if dst.%s != nil {", fname)
+ writef("\tdst.%s = map[%s]%s{}", fname, it.QualifiedName(ft.Key()), it.QualifiedName(elem))
writef("\tfor k := range src.%s {", fname)
// use zero-length slice instead of nil to ensure
// the key is always copied.
writef("\t\tdst.%s[k] = append([]%s{}, src.%s[k]...)", fname, n, fname)
writef("\t}")
+ writef("}")
} else if codegen.ContainsPointers(elem) {
+ writef("if dst.%s != nil {", fname)
+ writef("\tdst.%s = map[%s]%s{}", fname, it.QualifiedName(ft.Key()), it.QualifiedName(elem))
writef("\tfor k, v := range src.%s {", fname)
switch elem.(type) {
case *types.Pointer:
writef("\t\tdst.%s[k] = v.Clone()", fname)
default:
- writef("\t\tv2 := v.Clone()")
- writef("\t\tdst.%s[k] = *v2", fname)
+ writef("\t\tdst.%s[k] = *(v.Clone())", fname)
}
writef("\t}")
+ writef("}")
} else {
- writef("\tfor k, v := range src.%s {", fname)
- writef("\t\tdst.%s[k] = v", fname)
- writef("\t}")
+ it.Import("maps")
+ writef("\tdst.%s = maps.Clone(src.%s)", fname, fname)
}
- writef("}")
default:
writef(`panic("TODO: %s (%T)")`, fname, ft)
}
diff --git a/cmd/derper/depaware.txt b/cmd/derper/depaware.txt
index 394066519..e105d4a83 100644
--- a/cmd/derper/depaware.txt
+++ b/cmd/derper/depaware.txt
@@ -240,7 +240,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
io/ioutil from github.com/mitchellh/go-ps+
log from expvar+
log/internal from log
- maps from tailscale.com/types/views
+ maps from tailscale.com/types/views+
math from compress/flate+
math/big from crypto/dsa+
math/bits from compress/flate+
diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt
index ef29eeebe..7ce47f189 100644
--- a/cmd/tailscale/depaware.txt
+++ b/cmd/tailscale/depaware.txt
@@ -256,7 +256,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
io/ioutil from golang.org/x/sys/cpu+
log from expvar+
log/internal from log
- maps from tailscale.com/types/views
+ maps from tailscale.com/types/views+
math from compress/flate+
math/big from crypto/dsa+
math/bits from compress/flate+
diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt
index c151878b9..2e4075e64 100644
--- a/cmd/tailscaled/depaware.txt
+++ b/cmd/tailscaled/depaware.txt
@@ -466,7 +466,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
log from expvar+
log/internal from log
LD log/syslog from tailscale.com/ssh/tailssh
- maps from tailscale.com/types/views
+ maps from tailscale.com/types/views+
math from compress/flate+
math/big from crypto/dsa+
math/bits from compress/flate+
diff --git a/cmd/viewer/tests/tests_clone.go b/cmd/viewer/tests/tests_clone.go
index 3ff914126..4d82c03bd 100644
--- a/cmd/viewer/tests/tests_clone.go
+++ b/cmd/viewer/tests/tests_clone.go
@@ -6,7 +6,10 @@
package tests
import (
+ "maps"
"net/netip"
+
+ "tailscale.com/types/ptr"
)
// Clone makes a deep copy of StructWithPtrs.
@@ -18,12 +21,10 @@ func (src *StructWithPtrs) Clone() *StructWithPtrs {
dst := new(StructWithPtrs)
*dst = *src
if dst.Value != nil {
- dst.Value = new(StructWithoutPtrs)
- *dst.Value = *src.Value
+ dst.Value = ptr.To(*src.Value)
}
if dst.Int != nil {
- dst.Int = new(int)
- *dst.Int = *src.Int
+ dst.Int = ptr.To(*src.Int)
}
return dst
}
@@ -60,12 +61,7 @@ func (src *Map) Clone() *Map {
}
dst := new(Map)
*dst = *src
- if dst.Int != nil {
- dst.Int = map[string]int{}
- for k, v := range src.Int {
- dst.Int[k] = v
- }
- }
+ dst.Int = maps.Clone(src.Int)
if dst.SliceInt != nil {
dst.SliceInt = map[string][]int{}
for k := range src.SliceInt {
@@ -84,12 +80,7 @@ func (src *Map) Clone() *Map {
dst.StructPtrWithoutPtr[k] = v.Clone()
}
}
- if dst.StructWithoutPtr != nil {
- dst.StructWithoutPtr = map[string]StructWithoutPtrs{}
- for k, v := range src.StructWithoutPtr {
- dst.StructWithoutPtr[k] = v
- }
- }
+ dst.StructWithoutPtr = maps.Clone(src.StructWithoutPtr)
if dst.SlicesWithPtrs != nil {
dst.SlicesWithPtrs = map[string][]*StructWithPtrs{}
for k := range src.SlicesWithPtrs {
@@ -102,35 +93,19 @@ func (src *Map) Clone() *Map {
dst.SlicesWithoutPtrs[k] = append([]*StructWithoutPtrs{}, src.SlicesWithoutPtrs[k]...)
}
}
- if dst.StructWithoutPtrKey != nil {
- dst.StructWithoutPtrKey = map[StructWithoutPtrs]int{}
- for k, v := range src.StructWithoutPtrKey {
- dst.StructWithoutPtrKey[k] = v
- }
- }
+ dst.StructWithoutPtrKey = maps.Clone(src.StructWithoutPtrKey)
if dst.SliceIntPtr != nil {
dst.SliceIntPtr = map[string][]*int{}
for k := range src.SliceIntPtr {
dst.SliceIntPtr[k] = append([]*int{}, src.SliceIntPtr[k]...)
}
}
- if dst.PointerKey != nil {
- dst.PointerKey = map[*string]int{}
- for k, v := range src.PointerKey {
- dst.PointerKey[k] = v
- }
- }
- if dst.StructWithPtrKey != nil {
- dst.StructWithPtrKey = map[StructWithPtrs]int{}
- for k, v := range src.StructWithPtrKey {
- dst.StructWithPtrKey[k] = v
- }
- }
+ dst.PointerKey = maps.Clone(src.PointerKey)
+ dst.StructWithPtrKey = maps.Clone(src.StructWithPtrKey)
if dst.StructWithPtr != nil {
dst.StructWithPtr = map[string]StructWithPtrs{}
for k, v := range src.StructWithPtr {
- v2 := v.Clone()
- dst.StructWithPtr[k] = *v2
+ dst.StructWithPtr[k] = *(v.Clone())
}
}
return dst
@@ -175,8 +150,7 @@ func (src *StructWithSlices) Clone() *StructWithSlices {
}
dst.Ints = make([]*int, len(src.Ints))
for i := range dst.Ints {
- x := *src.Ints[i]
- dst.Ints[i] = &x
+ dst.Ints[i] = ptr.To(*src.Ints[i])
}
dst.Slice = append(src.Slice[:0:0], src.Slice...)
dst.Prefixes = append(src.Prefixes[:0:0], src.Prefixes...)