wire: allow wire.Value to use values with no parent (i.e., struct fields) (google/go-cloud#596)

This commit is contained in:
Robert van Gent
2018-11-02 13:58:15 -07:00
committed by Ross Light
parent b4218146b9
commit ab113bf8d1
13 changed files with 152 additions and 2 deletions

View File

@@ -0,0 +1,21 @@
// 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
var foo struct {
X int
}

View File

@@ -0,0 +1,21 @@
// 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.Printf("%v\n", injectedBar())
}

View File

@@ -0,0 +1,28 @@
// 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 injectedBar() string {
// Fails because bar.foo is unexported.
wire.Build(bar.foo.X)
return ""
}

View File

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

View File

@@ -0,0 +1 @@
/wire_gopath/src/example.com/foo/wire.go:x:y: foo not exported by package bar

View File

@@ -22,6 +22,7 @@ import (
)
func injectedMessage() string {
// Fails because bar.Value references unexported bar.privateMsg.
wire.Build(bar.Value)
return ""
}

View File

@@ -0,0 +1,28 @@
// 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() {
f := injectFoo()
fmt.Printf("%d\n", f.X)
}
type Foo struct {
X int
}

View File

@@ -0,0 +1,26 @@
// 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 (
"github.com/google/go-cloud/wire"
)
func injectFoo() Foo {
wire.Build(wire.Value(Foo{X: 42}))
return Foo{}
}

View File

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

View File

@@ -0,0 +1 @@
42

View File

@@ -0,0 +1,17 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
// Injectors from wire.go:
func injectFoo() Foo {
foo := _wireFooValue
return foo
}
var (
_wireFooValue = Foo{X: 42}
)

View File

@@ -845,7 +845,7 @@ func accessibleFrom(info *types.Info, node ast.Node, wantPkg string) error {
unexportError = fmt.Errorf("uses unexported identifier %s", obj.Name())
return false
}
if obj.Parent() != pkg.Scope() {
if obj.Parent() != nil && obj.Parent() != pkg.Scope() {
unexportError = fmt.Errorf("%s is not declared in package scope", obj.Name())
return false
}