From 46248162d7367fbaa74961a1edd9af8544198286 Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Tue, 4 Sep 2018 09:10:14 -0700 Subject: [PATCH] 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 --- .../MultipleSimilarPackages/bar/bar.go | 32 ++++++++++ .../MultipleSimilarPackages/baz/baz.go | 32 ++++++++++ .../MultipleSimilarPackages/foo/foo.go | 27 ++++++++ .../MultipleSimilarPackages/main/wire.go | 62 +++++++++++++++++++ .../wire/testdata/MultipleSimilarPackages/pkg | 1 + .../want/program_out.txt | 1 + .../MultipleSimilarPackages/want/wire_gen.go | 55 ++++++++++++++++ 7 files changed, 210 insertions(+) create mode 100644 internal/wire/testdata/MultipleSimilarPackages/bar/bar.go create mode 100644 internal/wire/testdata/MultipleSimilarPackages/baz/baz.go create mode 100644 internal/wire/testdata/MultipleSimilarPackages/foo/foo.go create mode 100644 internal/wire/testdata/MultipleSimilarPackages/main/wire.go create mode 100644 internal/wire/testdata/MultipleSimilarPackages/pkg create mode 100644 internal/wire/testdata/MultipleSimilarPackages/want/program_out.txt create mode 100644 internal/wire/testdata/MultipleSimilarPackages/want/wire_gen.go diff --git a/internal/wire/testdata/MultipleSimilarPackages/bar/bar.go b/internal/wire/testdata/MultipleSimilarPackages/bar/bar.go new file mode 100644 index 0000000..bec3c6a --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/bar/bar.go @@ -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} +} diff --git a/internal/wire/testdata/MultipleSimilarPackages/baz/baz.go b/internal/wire/testdata/MultipleSimilarPackages/baz/baz.go new file mode 100644 index 0000000..475cd76 --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/baz/baz.go @@ -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} +} diff --git a/internal/wire/testdata/MultipleSimilarPackages/foo/foo.go b/internal/wire/testdata/MultipleSimilarPackages/foo/foo.go new file mode 100644 index 0000000..3fe0ea1 --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/foo/foo.go @@ -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} +} diff --git a/internal/wire/testdata/MultipleSimilarPackages/main/wire.go b/internal/wire/testdata/MultipleSimilarPackages/main/wire.go new file mode 100644 index 0000000..692c792 --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/main/wire.go @@ -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()) +} diff --git a/internal/wire/testdata/MultipleSimilarPackages/pkg b/internal/wire/testdata/MultipleSimilarPackages/pkg new file mode 100644 index 0000000..b7badd9 --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/pkg @@ -0,0 +1 @@ +example.com/main diff --git a/internal/wire/testdata/MultipleSimilarPackages/want/program_out.txt b/internal/wire/testdata/MultipleSimilarPackages/want/program_out.txt new file mode 100644 index 0000000..b85905e --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/want/program_out.txt @@ -0,0 +1 @@ +1 2 3 diff --git a/internal/wire/testdata/MultipleSimilarPackages/want/wire_gen.go b/internal/wire/testdata/MultipleSimilarPackages/want/wire_gen.go new file mode 100644 index 0000000..1e8cc60 --- /dev/null +++ b/internal/wire/testdata/MultipleSimilarPackages/want/wire_gen.go @@ -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()) +}