From 0de66386d46c10ab5a7e57432dfa00e6558658f1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 17 Aug 2022 16:56:56 -0500 Subject: [PATCH] cmd/viewer: add flag to support Clone generation without Views Signed-off-by: Brad Fitzpatrick --- cmd/viewer/tests/tests.go | 6 +++++- cmd/viewer/tests/tests_clone.go | 16 ++++++++++++++++ cmd/viewer/tests/tests_view.go | 2 +- cmd/viewer/viewer.go | 10 ++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/viewer/tests/tests.go b/cmd/viewer/tests/tests.go index 847460709..1a2264c6c 100644 --- a/cmd/viewer/tests/tests.go +++ b/cmd/viewer/tests/tests.go @@ -10,7 +10,7 @@ import ( "net/netip" ) -//go:generate go run tailscale.com/cmd/viewer --type=StructWithPtrs,StructWithoutPtrs,Map,StructWithSlices +//go:generate go run tailscale.com/cmd/viewer --type=StructWithPtrs,StructWithoutPtrs,Map,StructWithSlices,OnlyGetClone --clone-only-type=OnlyGetClone type StructWithoutPtrs struct { Int int @@ -58,3 +58,7 @@ type StructWithSlices struct { Prefixes []netip.Prefix Data []byte } + +type OnlyGetClone struct { + SinViewerPorFavor bool +} diff --git a/cmd/viewer/tests/tests_clone.go b/cmd/viewer/tests/tests_clone.go index 396be03b9..8cad5f3f3 100644 --- a/cmd/viewer/tests/tests_clone.go +++ b/cmd/viewer/tests/tests_clone.go @@ -196,3 +196,19 @@ var _StructWithSlicesCloneNeedsRegeneration = StructWithSlices(struct { Prefixes []netip.Prefix Data []byte }{}) + +// Clone makes a deep copy of OnlyGetClone. +// The result aliases no memory with the original. +func (src *OnlyGetClone) Clone() *OnlyGetClone { + if src == nil { + return nil + } + dst := new(OnlyGetClone) + *dst = *src + return dst +} + +// A compilation failure here means this code must be regenerated, with the command at the top of this file. +var _OnlyGetCloneCloneNeedsRegeneration = OnlyGetClone(struct { + SinViewerPorFavor bool +}{}) diff --git a/cmd/viewer/tests/tests_view.go b/cmd/viewer/tests/tests_view.go index 03aa1a922..a01853553 100644 --- a/cmd/viewer/tests/tests_view.go +++ b/cmd/viewer/tests/tests_view.go @@ -15,7 +15,7 @@ import ( "tailscale.com/types/views" ) -//go:generate go run tailscale.com/cmd/cloner -clonefunc=false -type=StructWithPtrs,StructWithoutPtrs,Map,StructWithSlices +//go:generate go run tailscale.com/cmd/cloner -clonefunc=false -type=StructWithPtrs,StructWithoutPtrs,Map,StructWithSlices,OnlyGetClone // View returns a readonly view of StructWithPtrs. func (p *StructWithPtrs) View() StructWithPtrsView { diff --git a/cmd/viewer/viewer.go b/cmd/viewer/viewer.go index 5f58f2cd9..c24e28705 100644 --- a/cmd/viewer/viewer.go +++ b/cmd/viewer/viewer.go @@ -327,6 +327,8 @@ var ( flagTypes = flag.String("type", "", "comma-separated list of types; required") flagBuildTags = flag.String("tags", "", "compiler build tags to apply") flagCloneFunc = flag.Bool("clonefunc", false, "add a top-level Clone func") + + flagCloneOnlyTypes = flag.String("clone-only-type", "", "comma-separated list of types (a subset of --type) that should only generate a go:generate clone line and not actual views") ) func main() { @@ -353,10 +355,18 @@ func main() { } it := codegen.NewImportTracker(pkg.Types) + cloneOnlyType := map[string]bool{} + for _, t := range strings.Split(*flagCloneOnlyTypes, ",") { + cloneOnlyType[t] = true + } + buf := new(bytes.Buffer) fmt.Fprintf(buf, "//go:generate go run tailscale.com/cmd/cloner %s\n\n", strings.Join(flagArgs, " ")) runCloner := false for _, typeName := range typeNames { + if cloneOnlyType[typeName] { + continue + } typ, ok := namedTypes[typeName] if !ok { log.Fatalf("could not find type %s", typeName)