wire: allow non-panic version of injector (google/go-cloud#91)

Fixes google/go-cloud#7
This commit is contained in:
Ross Light
2018-06-15 15:47:43 -07:00
parent 6345348d86
commit b2d47f8fcc
6 changed files with 107 additions and 29 deletions

View File

@@ -578,6 +578,11 @@ func isInjector(info *types.Info, fn *ast.FuncDecl) *ast.CallExpr {
only = stmt
case *ast.EmptyStmt:
// Do nothing.
case *ast.ReturnStmt:
// Allow the function to end in a return.
if only == nil {
return nil
}
default:
return nil
}
@@ -585,29 +590,24 @@ func isInjector(info *types.Info, fn *ast.FuncDecl) *ast.CallExpr {
if only == nil {
return nil
}
panicCall, ok := only.X.(*ast.CallExpr)
call, ok := only.X.(*ast.CallExpr)
if !ok {
return nil
}
panicIdent, ok := panicCall.Fun.(*ast.Ident)
if !ok {
return nil
if qualifiedIdentObject(info, call.Fun) == types.Universe.Lookup("panic") {
if len(call.Args) != 1 {
return nil
}
call, ok = call.Args[0].(*ast.CallExpr)
if !ok {
return nil
}
}
if info.ObjectOf(panicIdent) != types.Universe.Lookup("panic") {
return nil
}
if len(panicCall.Args) != 1 {
return nil
}
buildCall, ok := panicCall.Args[0].(*ast.CallExpr)
if !ok {
return nil
}
buildObj := qualifiedIdentObject(info, buildCall.Fun)
buildObj := qualifiedIdentObject(info, call.Fun)
if !isWireImport(buildObj.Pkg().Path()) || buildObj.Name() != "Build" {
return nil
}
return buildCall
return call
}
func isWireImport(path string) bool {

View File

@@ -0,0 +1,26 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import "fmt"
func main() {
fmt.Println(injectedMessage())
}
// provideMessage provides a friendly user greeting.
func provideMessage() string {
return "Hello, World!"
}

View File

@@ -0,0 +1,26 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//+build wireinject
package main
import (
"github.com/google/go-cloud/wire"
)
func injectedMessage() string {
wire.Build(provideMessage)
return ""
}

View File

@@ -0,0 +1 @@
Hello, World!

View File

@@ -0,0 +1 @@
foo