wire: allow non-panic version of injector (google/go-cloud#91)
Fixes google/go-cloud#7
This commit is contained in:
@@ -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 {
|
||||
|
||||
26
internal/wire/testdata/InjectWithReturn/foo/foo.go
vendored
Normal file
26
internal/wire/testdata/InjectWithReturn/foo/foo.go
vendored
Normal 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!"
|
||||
}
|
||||
26
internal/wire/testdata/InjectWithReturn/foo/wire.go
vendored
Normal file
26
internal/wire/testdata/InjectWithReturn/foo/wire.go
vendored
Normal 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 ""
|
||||
}
|
||||
1
internal/wire/testdata/InjectWithReturn/out.txt
vendored
Normal file
1
internal/wire/testdata/InjectWithReturn/out.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Hello, World!
|
||||
1
internal/wire/testdata/InjectWithReturn/pkg
vendored
Normal file
1
internal/wire/testdata/InjectWithReturn/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
||||
foo
|
||||
Reference in New Issue
Block a user