From 4d5ab743af5c9fcce91fe18cb0f21be77bba111a Mon Sep 17 00:00:00 2001 From: ajjensen13 Date: Thu, 11 Jun 2020 10:06:02 -0500 Subject: [PATCH] internal/wire: copy doc comment for generated injectors (#254) Fixes #244 --- internal/wire/testdata/DocComment/foo/foo.go | 28 +++++++++++++++++ internal/wire/testdata/DocComment/foo/wire.go | 31 +++++++++++++++++++ internal/wire/testdata/DocComment/pkg | 1 + .../testdata/DocComment/want/program_out.txt | 1 + .../wire/testdata/DocComment/want/wire_gen.go | 20 ++++++++++++ .../ExampleWithMocks/want/wire_gen.go | 6 ++++ internal/wire/wire.go | 15 ++++++--- 7 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 internal/wire/testdata/DocComment/foo/foo.go create mode 100644 internal/wire/testdata/DocComment/foo/wire.go create mode 100644 internal/wire/testdata/DocComment/pkg create mode 100644 internal/wire/testdata/DocComment/want/program_out.txt create mode 100644 internal/wire/testdata/DocComment/want/wire_gen.go diff --git a/internal/wire/testdata/DocComment/foo/foo.go b/internal/wire/testdata/DocComment/foo/foo.go new file mode 100644 index 0000000..c545d3f --- /dev/null +++ b/internal/wire/testdata/DocComment/foo/foo.go @@ -0,0 +1,28 @@ +// Copyright 2020 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 ( + Bar struct{} + Foo struct{} +) + +func main() { + fmt.Println("Hello, World") +} diff --git a/internal/wire/testdata/DocComment/foo/wire.go b/internal/wire/testdata/DocComment/foo/wire.go new file mode 100644 index 0000000..87ea7d5 --- /dev/null +++ b/internal/wire/testdata/DocComment/foo/wire.go @@ -0,0 +1,31 @@ +// Copyright 2020 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" +) + +/* blockComment returns Foo and has a /*- style doc comment */ +func blockComment() *Foo { + panic(wire.Build(wire.Struct(new(Foo)))) +} + +// lineComment returns Bar and has a //- style doc comment +func lineComment() *Bar { + panic(wire.Build(wire.Struct(new(Bar)))) +} diff --git a/internal/wire/testdata/DocComment/pkg b/internal/wire/testdata/DocComment/pkg new file mode 100644 index 0000000..f7a5c8c --- /dev/null +++ b/internal/wire/testdata/DocComment/pkg @@ -0,0 +1 @@ +example.com/foo diff --git a/internal/wire/testdata/DocComment/want/program_out.txt b/internal/wire/testdata/DocComment/want/program_out.txt new file mode 100644 index 0000000..3fa0d4b --- /dev/null +++ b/internal/wire/testdata/DocComment/want/program_out.txt @@ -0,0 +1 @@ +Hello, World diff --git a/internal/wire/testdata/DocComment/want/wire_gen.go b/internal/wire/testdata/DocComment/want/wire_gen.go new file mode 100644 index 0000000..bc8efa9 --- /dev/null +++ b/internal/wire/testdata/DocComment/want/wire_gen.go @@ -0,0 +1,20 @@ +// Code generated by Wire. DO NOT EDIT. + +//go:generate wire +//+build !wireinject + +package main + +// Injectors from wire.go: + +/* blockComment returns Foo and has a /*- style doc comment */ +func blockComment() *Foo { + foo := &Foo{} + return foo +} + +// lineComment returns Bar and has a //- style doc comment +func lineComment() *Bar { + bar := &Bar{} + return bar +} diff --git a/internal/wire/testdata/ExampleWithMocks/want/wire_gen.go b/internal/wire/testdata/ExampleWithMocks/want/wire_gen.go index 86521f3..8f57c06 100644 --- a/internal/wire/testdata/ExampleWithMocks/want/wire_gen.go +++ b/internal/wire/testdata/ExampleWithMocks/want/wire_gen.go @@ -7,6 +7,7 @@ package main // Injectors from wire.go: +// initApp returns a real app. func initApp() *app { mainTimer := _wireRealTimeValue mainGreeter := greeter{ @@ -22,6 +23,9 @@ var ( _wireRealTimeValue = realTime{} ) +// initMockedAppFromArgs returns an app with mocked dependencies provided via +// arguments (Approach A). Note that the argument's type is the interface +// type (timer), but the concrete mock type should be passed. func initMockedAppFromArgs(mt timer) *app { mainGreeter := greeter{ T: mt, @@ -32,6 +36,8 @@ func initMockedAppFromArgs(mt timer) *app { return mainApp } +// initMockedApp returns an app with its mocked dependencies, created +// via providers (Approach B). func initMockedApp() *appWithMocks { mainMockTimer := newMockTimer() mainGreeter := greeter{ diff --git a/internal/wire/wire.go b/internal/wire/wire.go index ff62c76..a75144f 100644 --- a/internal/wire/wire.go +++ b/internal/wire/wire.go @@ -179,7 +179,7 @@ func generateInjectors(g *gen, pkg *packages.Package) (injectorFiles []*ast.File ec.add(notePositionAll(g.pkg.Fset.Position(fn.Pos()), errs)...) continue } - if errs := g.inject(fn.Pos(), fn.Name.Name, sig, set); len(errs) > 0 { + if errs := g.inject(fn.Pos(), fn.Name.Name, sig, set, fn.Doc); len(errs) > 0 { ec.add(errs...) continue } @@ -304,7 +304,7 @@ func (g *gen) frame() []byte { } // inject emits the code for an injector. -func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *ProviderSet) []error { +func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *ProviderSet, doc *ast.CommentGroup) []error { injectSig, err := funcOutput(sig) if err != nil { return []error{notePosition(g.pkg.Fset.Position(pos), @@ -367,12 +367,12 @@ func (g *gen) inject(pos token.Pos, name string, sig *types.Signature, set *Prov } // Perform one pass to collect all imports, followed by the real pass. - injectPass(name, sig, calls, set, &injectorGen{ + injectPass(name, sig, calls, set, doc, &injectorGen{ g: g, errVar: disambiguate("err", g.nameInFileScope), discard: true, }) - injectPass(name, sig, calls, set, &injectorGen{ + injectPass(name, sig, calls, set, doc, &injectorGen{ g: g, errVar: disambiguate("err", g.nameInFileScope), discard: false, @@ -586,13 +586,18 @@ type injectorGen struct { // injectPass generates an injector given the output from analysis. // The sig passed in should be verified. -func injectPass(name string, sig *types.Signature, calls []call, set *ProviderSet, ig *injectorGen) { +func injectPass(name string, sig *types.Signature, calls []call, set *ProviderSet, doc *ast.CommentGroup, ig *injectorGen) { params := sig.Params() injectSig, err := funcOutput(sig) if err != nil { // This should be checked by the caller already. panic(err) } + if doc != nil { + for _, c := range doc.List { + ig.p("%s\n", c.Text) + } + } ig.p("func %s(", name) for i := 0; i < params.Len(); i++ { if i > 0 {