cmd/cloner, tailcfg: fix nil vs len 0 issues, add tests, use for Hostinfo

Also use go:generate and https://golang.org/s/generatedcode header style.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2020-07-27 10:40:34 -07:00
committed by Brad Fitzpatrick
parent 41d0c81859
commit ec4feaf31c
4 changed files with 66 additions and 24 deletions

View File

@ -123,7 +123,7 @@ const header = `// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserve
// 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 %s
// Code generated by tailscale.com/cmd/cloner -type %s; DO NOT EDIT.
package %s
@ -168,8 +168,8 @@ func gen(buf *bytes.Buffer, imports map[string]struct{}, name string, typ *types
}
switch ft := ft.Underlying().(type) {
case *types.Slice:
n := importedName(ft.Elem())
if containsPointers(ft.Elem()) {
n := importedName(ft.Elem())
writef("dst.%s = make([]%s, len(src.%s))", fname, n, fname)
writef("for i := range dst.%s {", fname)
if _, isPtr := ft.Elem().(*types.Pointer); isPtr {
@ -179,7 +179,7 @@ func gen(buf *bytes.Buffer, imports map[string]struct{}, name string, typ *types
}
writef("}")
} else {
writef("dst.%s = append([]%s(nil), src.%s...)", fname, n, fname)
writef("dst.%s = append(src.%s[:0:0], src.%s...)", fname, fname, fname)
}
case *types.Pointer:
if named, _ := ft.Elem().(*types.Named); named != nil && containsPointers(ft.Elem()) {