add FieldsOf to inject fields of a struct directly (#138)

This commit is contained in:
shantuo
2019-03-01 13:52:07 -08:00
committed by GitHub
parent 58e5de342a
commit 327f42724c
37 changed files with 869 additions and 56 deletions

View File

@@ -0,0 +1,38 @@
// Copyright 2019 The Wire 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(injectedBaz())
}
type Foo int
type Baz int
type Bar struct {
Bz Baz
}
func provideFoo(_ Baz) Foo {
return 0
}
func provideBar(_ Foo) Bar {
return Bar{}
}

View File

@@ -0,0 +1,26 @@
// Copyright 2019 The Wire 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/wire"
)
func injectedBaz() Baz {
wire.Build(provideFoo, provideBar, wire.FieldsOf(new(Bar), "Bz"))
return 0
}

View File

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

View File

@@ -0,0 +1,5 @@
example.com/foo/wire.go:x:y: cycle for example.com/foo.Bar:
example.com/foo.Bar (example.com/foo.provideBar) ->
example.com/foo.Foo (example.com/foo.provideFoo) ->
example.com/foo.Baz (example.com/foo.Bar.Bz) ->
example.com/foo.Bar

View File

@@ -0,0 +1,32 @@
// Copyright 2019 The Wire 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
import (
"example.com/foo"
)
type Config struct {
V int
}
type Service struct {
Cfg *Config
F *foo.Service
}
func New(cfg *Config, f *foo.Service) *Service {
return &Service{Cfg: cfg, F: f}
}

View File

@@ -0,0 +1,36 @@
// Copyright 2019 The Wire 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 baz
import (
"fmt"
"example.com/bar"
"example.com/foo"
)
type Config struct {
Foo *foo.Config
Bar *bar.Config
}
type Service struct {
Foo *foo.Service
Bar *bar.Service
}
func (m *Service) String() string {
return fmt.Sprintf("%d %d", m.Foo.Cfg.V, m.Bar.Cfg.V)
}

View File

@@ -0,0 +1,27 @@
// Copyright 2019 The Wire 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 foo
type Config struct {
V int
}
type Service struct {
Cfg *Config
}
func New(cfg *Config) *Service {
return &Service{Cfg: cfg}
}

View File

@@ -0,0 +1,49 @@
// Copyright 2019 The Wire 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 (
"fmt"
"example.com/bar"
"example.com/baz"
"example.com/foo"
"github.com/google/wire"
)
func newBazService(*baz.Config) *baz.Service {
wire.Build(
baz.Service{},
wire.FieldsOf(
new(*baz.Config),
"Foo",
"Bar",
),
foo.New,
bar.New,
)
return nil
}
func main() {
cfg := &baz.Config{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
}
svc := newBazService(cfg)
fmt.Println(svc.String())
}

View File

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

View File

@@ -0,0 +1 @@
1 2

View File

@@ -0,0 +1,38 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
import (
"example.com/bar"
"example.com/baz"
"example.com/foo"
"fmt"
)
// Injectors from wire.go:
func newBazService(config *baz.Config) *baz.Service {
fooConfig := config.Foo
service := foo.New(fooConfig)
barConfig := config.Bar
barService := bar.New(barConfig, service)
bazService := &baz.Service{
Foo: service,
Bar: barService,
}
return bazService
}
// wire.go:
func main() {
cfg := &baz.Config{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
}
svc := newBazService(cfg)
fmt.Println(svc.String())
}

View File

@@ -0,0 +1,29 @@
// Copyright 2018 The Wire 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"
type S struct {
Foo string
}
func provideS() S {
return S{Foo: "Hello, World!"}
}
func main() {
fmt.Println(injectedMessage())
}

View File

@@ -0,0 +1,28 @@
// Copyright 2018 The Wire 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/wire"
)
func injectedMessage() string {
wire.Build(
provideS,
wire.FieldsOf(new(S), "Foo"))
return ""
}

View File

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

View File

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

View File

@@ -0,0 +1,14 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
// Injectors from wire.go:
func injectedMessage() string {
s := provideS()
string2 := s.Foo
return string2
}

View File

@@ -0,0 +1,29 @@
// Copyright 2018 The Wire 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"
type S struct {
Foo string
}
func provideS() *S {
return &S{Foo: "Hello, World!"}
}
func main() {
fmt.Println(injectedMessage())
}

View File

@@ -0,0 +1,28 @@
// Copyright 2018 The Wire 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/wire"
)
func injectedMessage() string {
wire.Build(
provideS,
wire.FieldsOf(new(*S), "Foo"))
return ""
}

View File

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

View File

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

View File

@@ -0,0 +1,14 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
// Injectors from wire.go:
func injectedMessage() string {
s := provideS()
string2 := s.Foo
return string2
}

View File

@@ -0,0 +1,32 @@
// Copyright 2019 The Wire 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
import (
"example.com/foo"
)
type Config struct {
V int
}
type Service struct {
Cfg *Config
F *foo.Service
}
func New(cfg *Config, f *foo.Service) *Service {
return &Service{Cfg: cfg, F: f}
}

View File

@@ -0,0 +1,36 @@
// Copyright 2019 The Wire 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 baz
import (
"fmt"
"example.com/bar"
"example.com/foo"
)
type Config struct {
Foo *foo.Config
Bar *bar.Config
}
type Service struct {
Foo *foo.Service
Bar *bar.Service
}
func (m *Service) String() string {
return fmt.Sprintf("%d %d", m.Foo.Cfg.V, m.Bar.Cfg.V)
}

View File

@@ -0,0 +1,27 @@
// Copyright 2019 The Wire 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 foo
type Config struct {
V int
}
type Service struct {
Cfg *Config
}
func New(cfg *Config) *Service {
return &Service{Cfg: cfg}
}

View File

@@ -0,0 +1,49 @@
// Copyright 2019 The Wire 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 (
"fmt"
"example.com/bar"
"example.com/baz"
"example.com/foo"
"github.com/google/wire"
)
func newBazService() *baz.Service {
wire.Build(
baz.Service{},
wire.Value(&baz.Config{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
}),
wire.FieldsOf(
new(*baz.Config),
"Foo",
"Bar",
),
foo.New,
bar.New,
)
return nil
}
func main() {
svc := newBazService()
fmt.Println(svc.String())
}

View File

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

View File

@@ -0,0 +1 @@
1 2

View File

@@ -0,0 +1,42 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
import (
"example.com/bar"
"example.com/baz"
"example.com/foo"
"fmt"
)
// Injectors from wire.go:
func newBazService() *baz.Service {
config := _wireConfigValue
fooConfig := config.Foo
service := foo.New(fooConfig)
barConfig := config.Bar
barService := bar.New(barConfig, service)
bazService := &baz.Service{
Foo: service,
Bar: barService,
}
return bazService
}
var (
_wireConfigValue = &baz.Config{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
}
)
// wire.go:
func main() {
svc := newBazService()
fmt.Println(svc.String())
}

View File

@@ -28,22 +28,28 @@ import (
type MainConfig struct {
Foo *foo.Config
Bar *bar.Config
Baz *baz.Config
baz *baz.Config
}
type MainService struct {
Foo *foo.Service
Bar *bar.Service
Baz *baz.Service
baz *baz.Service
}
func (m *MainService) String() string {
return fmt.Sprintf("%d %d %d", m.Foo.Cfg.V, m.Bar.Cfg.V, m.Baz.Cfg.V)
return fmt.Sprintf("%d %d %d", m.Foo.Cfg.V, m.Bar.Cfg.V, m.baz.Cfg.V)
}
func newMainService(*foo.Config, *bar.Config, *baz.Config) *MainService {
func newMainService(MainConfig) *MainService {
wire.Build(
MainService{},
wire.FieldsOf(
new(MainConfig),
"Foo",
"Bar",
"baz",
),
foo.New,
bar.New,
baz.New,
@@ -52,11 +58,11 @@ func newMainService(*foo.Config, *bar.Config, *baz.Config) *MainService {
}
func main() {
cfg := &MainConfig{
cfg := MainConfig{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
Baz: &baz.Config{3},
baz: &baz.Config{3},
}
svc := newMainService(cfg.Foo, cfg.Bar, cfg.Baz)
svc := newMainService(cfg)
fmt.Println(svc.String())
}

View File

@@ -14,14 +14,17 @@ import (
// Injectors from wire.go:
func newMainService(config *foo.Config, barConfig *bar.Config, bazConfig *baz.Config) *MainService {
func newMainService(mainConfig MainConfig) *MainService {
config := mainConfig.Foo
service := foo.New(config)
barConfig := mainConfig.Bar
barService := bar.New(barConfig, service)
bazConfig := mainConfig.baz
bazService := baz.New(bazConfig, barService)
mainService := &MainService{
Foo: service,
Bar: barService,
Baz: bazService,
baz: bazService,
}
return mainService
}
@@ -31,25 +34,25 @@ func newMainService(config *foo.Config, barConfig *bar.Config, bazConfig *baz.Co
type MainConfig struct {
Foo *foo.Config
Bar *bar.Config
Baz *baz.Config
baz *baz.Config
}
type MainService struct {
Foo *foo.Service
Bar *bar.Service
Baz *baz.Service
baz *baz.Service
}
func (m *MainService) String() string {
return fmt.Sprintf("%d %d %d", m.Foo.Cfg.V, m.Bar.Cfg.V, m.Baz.Cfg.V)
return fmt.Sprintf("%d %d %d", m.Foo.Cfg.V, m.Bar.Cfg.V, m.baz.Cfg.V)
}
func main() {
cfg := &MainConfig{
cfg := MainConfig{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
Baz: &baz.Config{3},
baz: &baz.Config{3},
}
svc := newMainService(cfg.Foo, cfg.Bar, cfg.Baz)
svc := newMainService(cfg)
fmt.Println(svc.String())
}

View File

@@ -69,3 +69,9 @@ func provideOneOfTwo() OneOfTwo {
func provideTwoOfTwo() TwoOfTwo {
return 1
}
type S struct {
Cfg Config
}
type Config int

View File

@@ -29,6 +29,7 @@ func injectBar() Bar {
wire.Value("unused"), // not needed -> error
unusedSet, // nothing in set is needed -> error
wire.Bind((*Fooer)(nil), (*Foo)(nil)), // binding to Fooer is not needed -> error
wire.FieldsOf(new(S), "Cfg"), // S.Cfg not needed -> error
)
return 0
}

View File

@@ -4,4 +4,6 @@ example.com/foo/wire.go:x:y: inject injectBar: unused provider "provideUnused"
example.com/foo/wire.go:x:y: inject injectBar: unused value of type string
example.com/foo/wire.go:x:y: inject injectBar: unused interface binding to type example.com/foo.Fooer
example.com/foo/wire.go:x:y: inject injectBar: unused interface binding to type example.com/foo.Fooer
example.com/foo/wire.go:x:y: inject injectBar: unused field "example.com/foo.S".Cfg