cmd/viewer, types/views, util/codegen: add viewer support for custom 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>
This commit is contained in:
Nick Khyl
2024-07-14 11:45:55 -05:00
committed by Nick Khyl
parent e7bf6e716b
commit 20562a4fb9
7 changed files with 544 additions and 5 deletions

View File

@@ -111,6 +111,14 @@ func (it *ImportTracker) QualifiedName(t types.Type) string {
return types.TypeString(t, it.qualifier)
}
// PackagePrefix returns the prefix to be used when referencing named objects from pkg.
func (it *ImportTracker) PackagePrefix(pkg *types.Package) string {
if s := it.qualifier(pkg); s != "" {
return s + "."
}
return ""
}
// Write prints all the tracked imports in a single import block to w.
func (it *ImportTracker) Write(w io.Writer) {
fmt.Fprintf(w, "import (\n")