cmd/cloner: simplify code

Change from a single-case type switch to a type assertion
with an early return.

That exposes that the name arg to gen is unneeded.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
josh/immutable-views
Josh Bleecher Snyder 3 years ago committed by Josh Bleecher Snyder
parent d5ab18b2e6
commit 081be3e96b

@ -85,7 +85,7 @@ func main() {
continue
}
pkg := typeNameObj.Pkg()
gen(buf, imports, typeName, typ, pkg)
gen(buf, imports, typ, pkg)
found = true
}
}
@ -150,7 +150,7 @@ package %s
`
func gen(buf *bytes.Buffer, imports map[string]struct{}, name string, typ *types.Named, thisPkg *types.Package) {
func gen(buf *bytes.Buffer, imports map[string]struct{}, typ *types.Named, thisPkg *types.Package) {
pkgQual := func(pkg *types.Package) string {
if thisPkg == pkg {
return ""
@ -162,8 +162,11 @@ func gen(buf *bytes.Buffer, imports map[string]struct{}, name string, typ *types
return types.TypeString(t, pkgQual)
}
switch t := typ.Underlying().(type) {
case *types.Struct:
t, ok := typ.Underlying().(*types.Struct)
if !ok {
return
}
name := typ.Obj().Name()
fmt.Fprintf(buf, "// Clone makes a deep copy of %s.\n", name)
fmt.Fprintf(buf, "// The result aliases no memory with the original.\n")
@ -244,7 +247,6 @@ func gen(buf *bytes.Buffer, imports map[string]struct{}, name string, typ *types
fmt.Fprintf(buf, "}\n\n")
buf.Write(codegen.AssertStructUnchanged(t, name, "Clone", thisPkg, imports))
}
}
func hasBasicUnderlying(typ types.Type) bool {

Loading…
Cancel
Save