wire: add a testcase with multiple similar packages (google/go-cloud#387)

This is based on the description in google/go-cloud#372. It will be useful if we add wire.FieldsOf, and also for the disambiguation-using-package.

Updates google/go-cloud#372
Updates google/go-cloud#386
This commit is contained in:
Robert van Gent
2018-09-04 09:10:14 -07:00
committed by Ross Light
parent e93f33129e
commit 46248162d7
7 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
// 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
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,32 @@
// 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 baz
import (
"example.com/bar"
)
type Config struct {
V int
}
type Service struct {
Cfg *Config
B *bar.Service
}
func New(cfg *Config, b *bar.Service) *Service {
return &Service{Cfg: cfg, B: b}
}

View File

@@ -0,0 +1,27 @@
// 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 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,62 @@
// 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 (
"fmt"
"example.com/bar"
"example.com/baz"
"example.com/foo"
"github.com/google/go-cloud/wire"
)
type MainConfig struct {
Foo *foo.Config
Bar *bar.Config
Baz *baz.Config
}
type MainService struct {
Foo *foo.Service
Bar *bar.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)
}
func newMainService(*foo.Config, *bar.Config, *baz.Config) *MainService {
wire.Build(
MainService{},
foo.New,
bar.New,
baz.New,
)
return nil
}
func main() {
cfg := &MainConfig{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
Baz: &baz.Config{3},
}
svc := newMainService(cfg.Foo, cfg.Bar, cfg.Baz)
fmt.Println(svc.String())
}

View File

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

View File

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

View File

@@ -0,0 +1,55 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
import (
bar "example.com/bar"
baz "example.com/baz"
foo "example.com/foo"
fmt "fmt"
)
// Injectors from wire.go:
func newMainService(config *foo.Config, config2 *bar.Config, config3 *baz.Config) *MainService {
service := foo.New(config)
service2 := bar.New(config2, service)
service3 := baz.New(config3, service2)
mainService := &MainService{
Foo: service,
Bar: service2,
Baz: service3,
}
return mainService
}
// wire.go:
type MainConfig struct {
Foo *foo.Config
Bar *bar.Config
Baz *baz.Config
}
type MainService struct {
Foo *foo.Service
Bar *bar.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)
}
func main() {
cfg := &MainConfig{
Foo: &foo.Config{1},
Bar: &bar.Config{2},
Baz: &baz.Config{3},
}
svc := newMainService(cfg.Foo, cfg.Bar, cfg.Baz)
fmt.Println(svc.String())
}