wire: use go/packages for analysis (google/go-cloud#623)

Unfortunately, this does come with a ~4x slowdown to Wire, as it is
now pulling source for all transitively depended packages, but not
trimming comments or function bodies. This is due to limitations with
the ParseFile callback in go/packages.

This comes with a single semantic change: when performing analysis, Wire
will now evaluate everything with the wireinject build tag. I updated
the build tags tests accordingly. Prior to this PR, only the packages
directly named by the package patterns would be evaluated with the
wireinject build tag. Dependencies would not have the wireinject build
tag applied. There isn't a way to selectively apply build tags in go/packages,
and there isn't a clear benefit to applying it selectively. Being consistent with
other Go tooling provides greater benefit.

I deleted the vendoring test, as non-top-level vendoring
becomes obsolete with modules.

go/packages now parses comments by default, so now the generated code
includes comments for non-injector declarations.

Fixes google/go-cloud#78
This commit is contained in:
Ross Light
2018-11-07 14:44:15 -08:00
parent ce30a430c6
commit 282105c273
26 changed files with 198 additions and 391 deletions

View File

@@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//+build wireinject
//+build !wireinject
// Package bar includes both wireinject and non-wireinject variants.
package bar
import "github.com/google/go-cloud/wire"

View File

@@ -12,9 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//+build !wireinject
//+build wireinject
// Package bar includes both wireinject and non-wireinject variants.
package bar
import "github.com/google/go-cloud/wire"

View File

@@ -1,23 +0,0 @@
// Copyright 2018 The Go Cloud Authors
//
// 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 bar includes both wireinject and non-wireinject variants.
package bar
import "github.com/google/go-cloud/wire"
// Set provides a friendly user greeting.
var Set = wire.NewSet(wire.Value("Hello, World!"))

View File

@@ -1,22 +0,0 @@
// Copyright 2018 The Go Cloud Authors
//
// 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 bar
import "github.com/google/go-cloud/wire"
// Set provides an unfriendly user greeting.
var Set = wire.NewSet(wire.Value("Bah humbug! This is the wrong variant!"))

View File

@@ -1,17 +0,0 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
// Injectors from wire.go:
func injectedMessage() string {
string2 := _wireStringValue
return string2
}
var (
_wireStringValue = "Hello, World!"
)

View File

@@ -22,6 +22,7 @@ func main() {
fmt.Println(injectedMessage())
}
// provideMessage provides a friendly user greeting.
func provideMessage() string {
return "Hello, World!"
}

View File

@@ -19,3 +19,8 @@ import "fmt"
func main() {
fmt.Println(injectedMessage())
}
// provideMessage provides a friendly user greeting.
func provideMessage() string {
return "Hello, World!"
}

View File

@@ -17,11 +17,10 @@
package main
import (
"example.com/bar"
"github.com/google/go-cloud/wire"
)
func injectedMessage() string {
wire.Build(bar.Set)
wire.Build(provideMessage)
return ""
}

View File

@@ -5,13 +5,9 @@
package main
import (
"example.com/bar"
)
// Injectors from wire.go:
func injectedMessage() string {
string2 := bar.ProvideMessage()
string2 := provideMessage()
return string2
}

View File

@@ -15,4 +15,7 @@ func injectInterface() Interface {
// wire.go:
// Wire tries to disambiguate the variable "select" by prepending
// the package name; this package-scoped variable conflicts with that
// and forces a different name.
var mainSelect = 0

View File

@@ -1,21 +0,0 @@
// Copyright 2018 The Go Cloud Authors
//
// 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())
}

View File

@@ -1,27 +0,0 @@
// Copyright 2018 The Go Cloud Authors
//
// 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 (
"example.com/bar"
"github.com/google/go-cloud/wire"
)
func injectedMessage() string {
wire.Build(bar.ProvideMessage)
return ""
}

View File

@@ -1 +0,0 @@
example.com/foo

View File

@@ -1,21 +0,0 @@
// Copyright 2018 The Go Cloud Authors
//
// 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 bar is the vendored copy of bar which contains the real provider.
package bar
// ProvideMessage provides a friendly user greeting.
func ProvideMessage() string {
return "Hello, World!"
}

View File

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