Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/wire/testdata/DocComment/foo/foo.go
Original file line number Diff line number Diff line change
@@ -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")
}
31 changes: 31 additions & 0 deletions internal/wire/testdata/DocComment/foo/wire.go
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions internal/wire/testdata/DocComment/pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.com/foo
1 change: 1 addition & 0 deletions internal/wire/testdata/DocComment/want/program_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World
20 changes: 20 additions & 0 deletions internal/wire/testdata/DocComment/want/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/wire/testdata/ExampleWithMocks/want/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down