cmd/{cloner,viewer}: handle maps of views

Instead of trying to call View() on something that's already a View
type (or trying to Clone the view unnecessarily), we can re-use the
existing View values in a map[T]ViewType.

Fixes #17866

Signed-off-by: Andrew Dunham <andrew@tailscale.com>
This commit is contained in:
Andrew Dunham
2025-11-12 17:53:39 -05:00
committed by Andrew Dunham
parent f4f9dd7f8c
commit 6ac80b7334
5 changed files with 120 additions and 10 deletions
+11 -4
View File
@@ -367,14 +367,21 @@ func genView(buf *bytes.Buffer, it *codegen.ImportTracker, typ *types.Named, fie
case *types.Struct, *types.Named, *types.Alias:
strucT := u
args.FieldType = it.QualifiedName(fieldType)
if codegen.ContainsPointers(strucT) {
// We need to call View() unless the type is
// either a View itself or does not contain
// pointers (and can thus be shallow-copied).
//
// Otherwise, we need to create a View of the
// map value.
if codegen.IsViewType(strucT) || !codegen.ContainsPointers(strucT) {
template = "mapField"
args.MapValueType = it.QualifiedName(mElem)
} else {
args.MapFn = "t.View()"
template = "mapFnField"
args.MapValueType = it.QualifiedName(mElem)
args.MapValueView = appendNameSuffix(args.MapValueType, "View")
} else {
template = "mapField"
args.MapValueType = it.QualifiedName(mElem)
}
case *types.Basic:
template = "mapField"