internal/wire: copy doc comment for generated injectors (#254)

Fixes #244
This commit is contained in:
ajjensen13
2020-06-11 10:06:02 -05:00
committed by GitHub
parent 978b7803d3
commit 4d5ab743af
7 changed files with 97 additions and 5 deletions

View File

@@ -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")
}

View File

@@ -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))))
}

1
internal/wire/testdata/DocComment/pkg vendored Normal file
View File

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

View File

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

View File

@@ -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
}

View File

@@ -7,6 +7,7 @@ package main
// Injectors from wire.go: // Injectors from wire.go:
// initApp returns a real app.
func initApp() *app { func initApp() *app {
mainTimer := _wireRealTimeValue mainTimer := _wireRealTimeValue
mainGreeter := greeter{ mainGreeter := greeter{
@@ -22,6 +23,9 @@ var (
_wireRealTimeValue = realTime{} _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 { func initMockedAppFromArgs(mt timer) *app {
mainGreeter := greeter{ mainGreeter := greeter{
T: mt, T: mt,
@@ -32,6 +36,8 @@ func initMockedAppFromArgs(mt timer) *app {
return mainApp return mainApp
} }
// initMockedApp returns an app with its mocked dependencies, created
// via providers (Approach B).
func initMockedApp() *appWithMocks { func initMockedApp() *appWithMocks {
mainMockTimer := newMockTimer() mainMockTimer := newMockTimer()
mainGreeter := greeter{ mainGreeter := greeter{

View File

@@ -179,7 +179,7 @@ func generateInjectors(g *gen, pkg *packages.Package) (injectorFiles []*ast.File
ec.add(notePositionAll(g.pkg.Fset.Position(fn.Pos()), errs)...) ec.add(notePositionAll(g.pkg.Fset.Position(fn.Pos()), errs)...)
continue 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...) ec.add(errs...)
continue continue
} }
@@ -304,7 +304,7 @@ func (g *gen) frame() []byte {
} }
// inject emits the code for an injector. // 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) injectSig, err := funcOutput(sig)
if err != nil { if err != nil {
return []error{notePosition(g.pkg.Fset.Position(pos), 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. // 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, g: g,
errVar: disambiguate("err", g.nameInFileScope), errVar: disambiguate("err", g.nameInFileScope),
discard: true, discard: true,
}) })
injectPass(name, sig, calls, set, &injectorGen{ injectPass(name, sig, calls, set, doc, &injectorGen{
g: g, g: g,
errVar: disambiguate("err", g.nameInFileScope), errVar: disambiguate("err", g.nameInFileScope),
discard: false, discard: false,
@@ -586,13 +586,18 @@ type injectorGen struct {
// injectPass generates an injector given the output from analysis. // injectPass generates an injector given the output from analysis.
// The sig passed in should be verified. // 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() params := sig.Params()
injectSig, err := funcOutput(sig) injectSig, err := funcOutput(sig)
if err != nil { if err != nil {
// This should be checked by the caller already. // This should be checked by the caller already.
panic(err) panic(err)
} }
if doc != nil {
for _, c := range doc.List {
ig.p("%s\n", c.Text)
}
}
ig.p("func %s(", name) ig.p("func %s(", name)
for i := 0; i < params.Len(); i++ { for i := 0; i < params.Len(); i++ {
if i > 0 { if i > 0 {