wire: allow wire.Value to use values with no parent (i.e., struct fields) (google/go-cloud#596)
This commit is contained in:
committed by
Ross Light
parent
b4218146b9
commit
ab113bf8d1
@@ -327,9 +327,13 @@ The generated injector would look like this:
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func injectFoo() Foo {
|
func injectFoo() Foo {
|
||||||
foo := Foo{X: 42}
|
foo := _wireFooValue
|
||||||
return foo
|
return foo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_wireFooValue = Foo{X: 42}
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
It's important to note that the expression will be copied to the injector's
|
It's important to note that the expression will be copied to the injector's
|
||||||
|
|||||||
21
internal/wire/testdata/UnexportedStruct/bar/bar.go
vendored
Normal file
21
internal/wire/testdata/UnexportedStruct/bar/bar.go
vendored
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
21
internal/wire/testdata/UnexportedStruct/foo/foo.go
vendored
Normal file
21
internal/wire/testdata/UnexportedStruct/foo/foo.go
vendored
Normal 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())
|
||||||
|
}
|
||||||
28
internal/wire/testdata/UnexportedStruct/foo/wire.go
vendored
Normal file
28
internal/wire/testdata/UnexportedStruct/foo/wire.go
vendored
Normal 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 ""
|
||||||
|
}
|
||||||
1
internal/wire/testdata/UnexportedStruct/pkg
vendored
Normal file
1
internal/wire/testdata/UnexportedStruct/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
example.com/foo
|
||||||
1
internal/wire/testdata/UnexportedStruct/want/wire_errs.txt
vendored
Normal file
1
internal/wire/testdata/UnexportedStruct/want/wire_errs.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/wire_gopath/src/example.com/foo/wire.go:x:y: foo not exported by package bar
|
||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func injectedMessage() string {
|
func injectedMessage() string {
|
||||||
|
// Fails because bar.Value references unexported bar.privateMsg.
|
||||||
wire.Build(bar.Value)
|
wire.Build(bar.Value)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
28
internal/wire/testdata/ValueIsStruct/foo/foo.go
vendored
Normal file
28
internal/wire/testdata/ValueIsStruct/foo/foo.go
vendored
Normal 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
|
||||||
|
}
|
||||||
26
internal/wire/testdata/ValueIsStruct/foo/wire.go
vendored
Normal file
26
internal/wire/testdata/ValueIsStruct/foo/wire.go
vendored
Normal 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{}
|
||||||
|
}
|
||||||
1
internal/wire/testdata/ValueIsStruct/pkg
vendored
Normal file
1
internal/wire/testdata/ValueIsStruct/pkg
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
example.com/foo
|
||||||
1
internal/wire/testdata/ValueIsStruct/want/program_out.txt
vendored
Normal file
1
internal/wire/testdata/ValueIsStruct/want/program_out.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
42
|
||||||
17
internal/wire/testdata/ValueIsStruct/want/wire_gen.go
vendored
Normal file
17
internal/wire/testdata/ValueIsStruct/want/wire_gen.go
vendored
Normal 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}
|
||||||
|
)
|
||||||
@@ -845,7 +845,7 @@ func accessibleFrom(info *types.Info, node ast.Node, wantPkg string) error {
|
|||||||
unexportError = fmt.Errorf("uses unexported identifier %s", obj.Name())
|
unexportError = fmt.Errorf("uses unexported identifier %s", obj.Name())
|
||||||
return false
|
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())
|
unexportError = fmt.Errorf("%s is not declared in package scope", obj.Name())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user