mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
cmd/viewer: add flag to support Clone generation without Views
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
9ae1161e85
commit
0de66386d4
@ -10,7 +10,7 @@ import (
|
|||||||
"net/netip"
|
"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 {
|
type StructWithoutPtrs struct {
|
||||||
Int int
|
Int int
|
||||||
@ -58,3 +58,7 @@ type StructWithSlices struct {
|
|||||||
Prefixes []netip.Prefix
|
Prefixes []netip.Prefix
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OnlyGetClone struct {
|
||||||
|
SinViewerPorFavor bool
|
||||||
|
}
|
||||||
|
@ -196,3 +196,19 @@ var _StructWithSlicesCloneNeedsRegeneration = StructWithSlices(struct {
|
|||||||
Prefixes []netip.Prefix
|
Prefixes []netip.Prefix
|
||||||
Data []byte
|
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
|
||||||
|
}{})
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
"tailscale.com/types/views"
|
"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.
|
// View returns a readonly view of StructWithPtrs.
|
||||||
func (p *StructWithPtrs) View() StructWithPtrsView {
|
func (p *StructWithPtrs) View() StructWithPtrsView {
|
||||||
|
@ -327,6 +327,8 @@ var (
|
|||||||
flagTypes = flag.String("type", "", "comma-separated list of types; required")
|
flagTypes = flag.String("type", "", "comma-separated list of types; required")
|
||||||
flagBuildTags = flag.String("tags", "", "compiler build tags to apply")
|
flagBuildTags = flag.String("tags", "", "compiler build tags to apply")
|
||||||
flagCloneFunc = flag.Bool("clonefunc", false, "add a top-level Clone func")
|
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() {
|
func main() {
|
||||||
@ -353,10 +355,18 @@ func main() {
|
|||||||
}
|
}
|
||||||
it := codegen.NewImportTracker(pkg.Types)
|
it := codegen.NewImportTracker(pkg.Types)
|
||||||
|
|
||||||
|
cloneOnlyType := map[string]bool{}
|
||||||
|
for _, t := range strings.Split(*flagCloneOnlyTypes, ",") {
|
||||||
|
cloneOnlyType[t] = true
|
||||||
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
fmt.Fprintf(buf, "//go:generate go run tailscale.com/cmd/cloner %s\n\n", strings.Join(flagArgs, " "))
|
fmt.Fprintf(buf, "//go:generate go run tailscale.com/cmd/cloner %s\n\n", strings.Join(flagArgs, " "))
|
||||||
runCloner := false
|
runCloner := false
|
||||||
for _, typeName := range typeNames {
|
for _, typeName := range typeNames {
|
||||||
|
if cloneOnlyType[typeName] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
typ, ok := namedTypes[typeName]
|
typ, ok := namedTypes[typeName]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("could not find type %s", typeName)
|
log.Fatalf("could not find type %s", typeName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user