summaryrefslogtreecommitdiffhomepage
path: root/cmd/viewer
AgeCommit message (Collapse)AuthorFilesLines
2025-08-27cmd/viewer: add field comments to generated view methodsMaisem Ali3-52/+132
Extract field comments from AST and include them in generated view methods. Comments are preserved from the original struct fields to provide documentation for the view accessors. Fixes #16958 Signed-off-by: Maisem Ali <3953239+maisem@users.noreply.github.com>
2025-08-14cmd/viewer, types/views: implement support for json/v2 (#16852)Joe Tsai3-37/+303
This adds support for having every viewer type implement jsonv2.MarshalerTo and jsonv2.UnmarshalerFrom. This provides a significant boost in performance as the json package no longer needs to validate the entirety of the JSON value outputted by MarshalJSON, nor does it need to identify the boundaries of a JSON value in order to call UnmarshalJSON. For deeply nested and recursive MarshalJSON or UnmarshalJSON calls, this can improve runtime from O(N²) to O(N). This still references "github.com/go-json-experiment/json" instead of the experimental "encoding/json/v2" package now available in Go 1.25 under goexperiment.jsonv2 so that code still builds without the experiment tag. Of note, the "github.com/go-json-experiment/json" package aliases the standard library under the right build conditions. Updates tailscale/corp#791 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2025-01-14cmd/viewer,all: consistently use "read-only" instead of "readonly"Brad Fitzpatrick3-27/+27
Updates #cleanup Change-Id: I8e4e3497d3d0ec5b16a73aedda500fe5cfa37a67 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2025-01-14cmd/viewer,types/views,various: avoid allocations in pointer field getters ↵Nick Khyl4-47/+83
whenever possible In this PR, we add a generic views.ValuePointer type that can be used as a view for pointers to basic types and struct types that do not require deep cloning and do not have corresponding view types. Its Get/GetOk methods return stack-allocated shallow copies of the underlying value. We then update the cmd/viewer codegen to produce getters that return either concrete views when available or ValuePointer views when not, for pointer fields in generated view types. This allows us to avoid unnecessary allocations compared to returning pointers to newly allocated shallow copies. Updates #14570 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-10-21cmd/viewer: import types/views when generating a getter for a map fieldNick Khyl2-0/+79
Fixes #13873 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-08-23cmd/cloner, cmd/viewer, util/codegen: add support for aliases of cloneable typesNick Khyl4-16/+143
We have several checked type assertions to *types.Named in both cmd/cloner and cmd/viewer. As Go 1.23 updates the go/types package to produce Alias type nodes for type aliases, these type assertions no longer work as expected unless the new behavior is disabled with gotypesalias=0. In this PR, we add codegen.NamedTypeOf(t types.Type), which functions like t.(*types.Named) but also unrolls type aliases. We then use it in place of type assertions in the cmd/cloner and cmd/viewer packages where appropriate. We also update type switches to include *types.Alias alongside *types.Named in relevant cases, remove *types.Struct cases when switching on types.Type.Underlying and update the tests with more cases where type aliases can be used. Updates #13224 Updates #12912 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-08-23util/codegen, cmd/cloner, cmd/viewer: update codegen.LookupMethod to support ↵Nick Khyl4-3/+170
alias type nodes Go 1.23 updates the go/types package to produce Alias type nodes for type aliases, unless disabled with gotypesalias=0. This new default behavior breaks codegen.LookupMethod, which uses checked type assertions to types.Named and types.Interface, as only named types and interfaces have methods. In this PR, we update codegen.LookupMethod to perform method lookup on the right-hand side of the alias declaration and clearly switch on the supported type nodes types. We also improve support for various edge cases, such as when an alias is used as a type parameter constraint, and add tests for the LookupMethod function. Additionally, we update cmd/viewer/tests to include types with aliases used in type fields and generic type constraints. Updates #13224 Updates #12912 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-08-14cmd/viewer: add support for map-like container typesNick Khyl4-16/+68
This PR modifies viewTypeForContainerType to use the last type parameter of a container type as the value type, enabling the implementation of map-like container types where the second-to-last (usually first) type parameter serves as the key type. It also adds a MapContainer type to test the code generation. Updates #12736 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-07-19cmd/viewer, types/views, util/codegen: add viewer support for custom ↵Nick Khyl4-5/+262
container types This adds support for container-like types such as Container[T] that don't explicitly specify a view type for T. Instead, a package implementing a container type should also implement and export a ContainerView[T, V] type and a ContainerViewOf(*Container[T]) ContainerView[T, V] function, which returns a view for the specified container, inferring the element view type V from the element type T. Updates #12736 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-07-12cmd/cloner, cmd/viewer: add _test prefix for files generated with the test ↵Nick Khyl1-1/+5
build tag Updates #12736 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-07-11cmd/cloner, cmd/viewer, util/codegen: add support for generic types and ↵Nick Khyl4-81/+635
interfaces This adds support for generic types and interfaces to our cloner and viewer codegens. It updates these packages to determine whether to make shallow or deep copies based on the type parameter constraints. Additionally, if a template parameter or an interface type has View() and Clone() methods, we'll use them for getters and the cloner of the owning structure. Updates #12736 Signed-off-by: Nick Khyl <nickk@tailscale.com>
2024-06-15cmd/viewer,type/views: add MapSlice for maps of slicesMaisem Ali2-8/+6
This abstraction provides a nicer way to work with maps of slices without having to write out three long type params. This also allows it to provide an AsMap implementation which copies the map and the slices at least. Updates tailscale/corp#20910 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-04-16all: use Go 1.22 range-over-intBrad Fitzpatrick1-2/+2
Updates #11058 Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-03-08cmd/viewer: import views when generating byteSliceFieldPercy Wegmann1-0/+1
Updates #cleanup Signed-off-by: Percy Wegmann <percy@tailscale.com>
2023-09-29cmd/cloner: add regression test for slice nil/empty semanticsJames Tucker1-1/+5
We had a misstep with the semantics when applying an optimization that showed up in the roll into corp. This test ensures that case and related cases must be retained. Updates #9410 Updates #9601 Signed-off-by: James Tucker <james@tailscale.com>
2023-09-29cmd/cloner,*: revert: optimize nillable slice clonerJames Tucker1-12/+20
This reverts commit ee90cd02fdd4e4125ec9d12eef1195ed36ef4b2e. The outcome is not identical for empty slices. Cloner really needs tests! Updates #9601 Signed-off-by: James Tucker <james@tailscale.com>
2023-09-29cmd/cloner,*: optimize nillable slice clonerJames Tucker1-20/+12
A wild @josharian appears with a good suggestion for a refactor, thanks Josh! Updates #9410 Signed-off-by: James Tucker <james@tailscale.com>
2023-09-18tailcfg: add NodeCapMapMaisem Ali1-2/+3
Like PeerCapMap, add a field to `tailcfg.Node` which provides a map of Capability to raw JSON messages which are deferred to be parsed later by the application code which cares about the specific capabilities. This effectively allows us to prototype new behavior without having to commit to a schema in tailcfg, and it also opens up the possibilities to develop custom behavior in tsnet applications w/o having to plumb through application specific data in the MapResponse. Updates #4217 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-09-14cmd/cloner: do not allocate slices when the source is nilJames Tucker1-12/+20
tailcfg.Node zero-value clone equality checks failed when I added a []*foo to the structure, as the zero value and it's clone contained a different slice header. Updates #9377 Updates #9408 Signed-off-by: James Tucker <james@tailscale.com>
2023-08-20types/views,cmd/viewer: add ByteSlice[T] to replace mem.ROMaisem Ali2-5/+4
Add a new views.ByteSlice[T ~[]byte] to provide a better API to use with views. Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-20cmd/cloner: use maps.Clone and ptr.ToMaisem Ali1-38/+12
Updates #cleanup Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-18cmd/viewer, types/views, all: un-special case slice of netip.PrefixBrad Fitzpatrick2-7/+2
Make it just a views.Slice[netip.Prefix] instead of its own named type. Having the special case led to circular dependencies in another WIP PR of mine. Updates #8948 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-05-09util/codegen: support embedded fieldsBrad Fitzpatrick3-2/+82
I noticed cmd/{cloner,viewer} didn't support structs with embedded fields while working on a change in another repo. This adds support. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-01-27all: update tools that manage copyright headersWill Norris1-1/+1
Update all code generation tools, and those that check for license headers to use the new standard header. Also update copyright statement in LICENSE file. Fixes #6865 Signed-off-by: Will Norris <will@tailscale.com>
2023-01-27all: update copyright and license headersWill Norris4-12/+8
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
2023-01-02util/codegen, all: use latest year, not time.Now, in generated filesBrad Fitzpatrick1-1/+1
Updates #6865 Change-Id: I6b86c646968ebbd4553cf37df5e5612fbf5c5f7d Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-09-29all: fix spelling mistakesJosh Soref1-1/+1
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-08-17cmd/viewer: add flag to support Clone generation without ViewsBrad Fitzpatrick4-2/+32
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-08-03cmd/{cloner,viewer}: add support for map values with pointersMaisem Ali4-4/+23
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-08-03cmd/viewer: add support for map of structs without pointersMaisem Ali4-17/+39
This adds support for fields like `map[string]netaddr.IPPrefix`. Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-08-02all: gofmt for Go 1.19Brad Fitzpatrick1-2/+1
Updates #5210 Change-Id: Ib02cd5e43d0a8db60c1f09755a8ac7b140b670be Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25all: convert more code to use net/netip directlyBrad Fitzpatrick2-5/+4
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.) perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. ) perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. ) goimports -w . Then delete some stuff from the net/netaddr shim package which is no longer neeed. Updates #5162 Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-25net/netaddr: start migrating to net/netip via new netaddr adapter packageBrad Fitzpatrick4-10/+10
Updates #5162 Change-Id: Id7bdec303b25471f69d542f8ce43805328d56c12 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-05-09cmd/viewer,types/views: add support for views of mapsMaisem Ali4-13/+197
Updates #4635 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2022-05-06cmd/viewer: add codegen tool for ViewsMaisem Ali4-0/+752
Signed-off-by: Maisem Ali <maisem@tailscale.com>