From 0559d22dfe7e65807e1ddae5c44b49b26205f958 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 6 Aug 2018 23:49:11 +0200 Subject: [PATCH] all: simplify and clarify some expressions (google/go-cloud#260) --- internal/wire/parse.go | 6 ++---- internal/wire/wire.go | 13 +++++++------ internal/wire/wire_test.go | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/wire/parse.go b/internal/wire/parse.go index 1d014f4..c70f0d9 100644 --- a/internal/wire/parse.go +++ b/internal/wire/parse.go @@ -716,19 +716,17 @@ func processValue(fset *token.FileSet, info *types.Info, call *ast.CallExpr) (*V } ok := true ast.Inspect(call.Args[0], func(node ast.Node) bool { - switch node.(type) { + switch expr := node.(type) { case nil, *ast.ArrayType, *ast.BasicLit, *ast.BinaryExpr, *ast.ChanType, *ast.CompositeLit, *ast.FuncType, *ast.Ident, *ast.IndexExpr, *ast.InterfaceType, *ast.KeyValueExpr, *ast.MapType, *ast.ParenExpr, *ast.SelectorExpr, *ast.SliceExpr, *ast.StarExpr, *ast.StructType, *ast.TypeAssertExpr: // Good! case *ast.UnaryExpr: - expr := node.(*ast.UnaryExpr) if expr.Op == token.ARROW { ok = false return false } case *ast.CallExpr: // Only acceptable if it's a type conversion. - call := node.(*ast.CallExpr) - if _, isFunc := info.TypeOf(call.Fun).(*types.Signature); isFunc { + if _, isFunc := info.TypeOf(expr.Fun).(*types.Signature); isFunc { ok = false return false } diff --git a/internal/wire/wire.go b/internal/wire/wire.go index be08875..cc806ee 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -366,7 +366,7 @@ func (g *gen) rewritePkgRefs(info *types.Info, node ast.Node) ast.Node { if len(scopeStack) > 0 { // Avoid picking a name that conflicts with other names in the // current scope. - _, obj := scopeStack[len(scopeStack)-1].LookupParent(n, 0) + _, obj := scopeStack[len(scopeStack)-1].LookupParent(n, token.NoPos) if obj != nil { return true } @@ -436,7 +436,7 @@ func (g *gen) nameInFileScope(name string) bool { return true } } - _, obj := g.prog.Package(g.currPackage).Pkg.Scope().LookupParent(name, 0) + _, obj := g.prog.Package(g.currPackage).Pkg.Scope().LookupParent(name, token.NoPos) return obj != nil } @@ -481,13 +481,14 @@ func injectPass(name string, params *types.Tuple, injectSig outputSignature, cal ig.p("%s %s", ig.paramNames[i], types.TypeString(pi.Type(), ig.g.qualifyPkg)) } outTypeString := types.TypeString(injectSig.out, ig.g.qualifyPkg) - if injectSig.cleanup && injectSig.err { + switch { + case injectSig.cleanup && injectSig.err: ig.p(") (%s, func(), error) {\n", outTypeString) - } else if injectSig.cleanup { + case injectSig.cleanup: ig.p(") (%s, func()) {\n", outTypeString) - } else if injectSig.err { + case injectSig.err: ig.p(") (%s, error) {\n", outTypeString) - } else { + default: ig.p(") %s {\n", outTypeString) } for i := range calls { diff --git a/internal/wire/wire_test.go b/internal/wire/wire_test.go index c837665..62b8a9b 100644 --- a/internal/wire/wire_test.go +++ b/internal/wire/wire_test.go @@ -513,7 +513,7 @@ type dirInfo struct { func (d dirInfo) Name() string { return d.name } func (d dirInfo) Size() int64 { return 0 } -func (d dirInfo) Mode() os.FileMode { return os.ModeDir | 0777 } +func (d dirInfo) Mode() os.FileMode { return os.ModeDir | os.ModePerm } func (d dirInfo) ModTime() time.Time { return time.Unix(0, 0) } func (d dirInfo) IsDir() bool { return true } func (d dirInfo) Sys() interface{} { return nil }