From 990e2f1ae9f5401ef82b5ad43c79497cc5813c5a Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Fri, 24 Jul 2020 17:59:49 +1000 Subject: [PATCH] tailcfg: generate some Clone methods Signed-off-by: David Crawshaw --- tailcfg/tailcfg.go | 48 +++---------------------------- tailcfg/tailcfg_clone.go | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 tailcfg/tailcfg_clone.go diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index 343470a15..0e98f2a72 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -93,18 +93,6 @@ type User struct { // Note: be sure to update Clone when adding new fields } -// Clone returns a copy of u that aliases no memory with the original. -func (u *User) Clone() *User { - if u == nil { - return nil - } - u2 := new(User) - *u2 = *u - u2.Logins = append([]LoginID(nil), u.Logins...) - u2.Roles = append([]RoleID(nil), u.Roles...) - return u2 -} - type Login struct { _ structs.Incomparable ID LoginID @@ -150,23 +138,6 @@ type Node struct { // require changes to Node.Clone. } -// Clone makes a deep copy of Node. -// The result aliases no memory with the original. -func (n *Node) Clone() (res *Node) { - res = new(Node) - *res = *n - - res.Addresses = append([]wgcfg.CIDR{}, res.Addresses...) - res.AllowedIPs = append([]wgcfg.CIDR{}, res.AllowedIPs...) - res.Endpoints = append([]string{}, res.Endpoints...) - if res.LastSeen != nil { - lastSeen := *res.LastSeen - res.LastSeen = &lastSeen - } - res.Hostinfo = *res.Hostinfo.Clone() - return res -} - type MachineStatus int const ( @@ -400,23 +371,10 @@ func (ni *NetInfo) BasicallyEqual(ni2 *NetInfo) bool { ni.LinkType == ni2.LinkType } -func (ni *NetInfo) Clone() (res *NetInfo) { - if ni == nil { - return nil - } - res = new(NetInfo) - *res = *ni - if ni.DERPLatency != nil { - res.DERPLatency = map[string]float64{} - for k, v := range ni.DERPLatency { - res.DERPLatency[k] = v - } - } - return res -} - // Clone makes a deep copy of Hostinfo. // The result aliases no memory with the original. +// +// TODO: use cmd/cloner, reconcile len(0) vs. nil. func (h *Hostinfo) Clone() (res *Hostinfo) { res = new(Hostinfo) *res = *h @@ -461,6 +419,8 @@ type RegisterRequest struct { // Clone makes a deep copy of RegisterRequest. // The result aliases no memory with the original. +// +// TODO: extend cmd/cloner to generate this method. func (req *RegisterRequest) Clone() *RegisterRequest { res := new(RegisterRequest) *res = *req diff --git a/tailcfg/tailcfg_clone.go b/tailcfg/tailcfg_clone.go new file mode 100644 index 000000000..cc936e51f --- /dev/null +++ b/tailcfg/tailcfg_clone.go @@ -0,0 +1,61 @@ +// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// AUTO-GENERATED by tailscale.com/cmd/cloner -type User,Node,NetInfo + +package tailcfg + +import ( + "github.com/tailscale/wireguard-go/wgcfg" + "time" +) + +// Clone makes a deep copy of User. +// The result aliases no memory with the original. +func (src *User) Clone() *User { + if src == nil { + return nil + } + dst := new(User) + *dst = *src + dst.Logins = append([]LoginID(nil), src.Logins...) + dst.Roles = append([]RoleID(nil), src.Roles...) + return dst +} + +// Clone makes a deep copy of Node. +// The result aliases no memory with the original. +func (src *Node) Clone() *Node { + if src == nil { + return nil + } + dst := new(Node) + *dst = *src + dst.Addresses = append([]wgcfg.CIDR(nil), src.Addresses...) + dst.AllowedIPs = append([]wgcfg.CIDR(nil), src.AllowedIPs...) + dst.Endpoints = append([]string(nil), src.Endpoints...) + dst.Hostinfo = *src.Hostinfo.Clone() + if dst.LastSeen != nil { + dst.LastSeen = new(time.Time) + *dst.LastSeen = *src.LastSeen + } + return dst +} + +// Clone makes a deep copy of NetInfo. +// The result aliases no memory with the original. +func (src *NetInfo) Clone() *NetInfo { + if src == nil { + return nil + } + dst := new(NetInfo) + *dst = *src + if dst.DERPLatency != nil { + dst.DERPLatency = map[string]float64{} + for k, v := range src.DERPLatency { + dst.DERPLatency[k] = v + } + } + return dst +}