Skip to content

Commit fecc592

Browse files
committed
update ast to support generic
1 parent e57deea commit fecc592

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

Diff for: internal/wire/copyast.go

+7
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ func copyAST(original ast.Node) ast.Node {
263263
Index: exprFromMap(m, node.Index),
264264
Rbrack: node.Rbrack,
265265
}
266+
case *ast.IndexListExpr:
267+
m[node] = &ast.IndexListExpr{
268+
X: exprFromMap(m, node.X),
269+
Lbrack: node.Lbrack,
270+
Indices: copyExprList(m, node.Indices),
271+
Rbrack: node.Rbrack,
272+
}
266273
case *ast.InterfaceType:
267274
m[node] = &ast.InterfaceType{
268275
Interface: node.Interface,

Diff for: internal/wire/testdata/Generic/foo/foo.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2018 The Wire Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package main
15+
16+
type Foo string
17+
18+
type FooStorer interface {
19+
Set(key string, value Foo)
20+
Get(key string) (Foo, bool)
21+
}
22+
23+
type Store[TKey comparable, TValue any] struct {
24+
store map[TKey]TValue
25+
}
26+
27+
func NewStore[TKey comparable, TValue any]() Store[TKey, TValue] {
28+
return Store[TKey, TValue]{store: make(map[TKey]TValue)}
29+
}
30+
31+
func (s Store[TKey, TValue]) Set(key TKey, value TValue) {
32+
s.store[key] = value
33+
}
34+
35+
func (s Store[TKey, TValue]) Get(key TKey) (TValue, bool) {
36+
value, ok := s.store[key]
37+
return value, ok
38+
}
39+
40+
func NewFooStore(foo Foo) Store[string, Foo] {
41+
r := NewStore[string, Foo]()
42+
r.Set("foo", foo)
43+
return r
44+
}
45+
46+
func main() {
47+
fooStore := InitializeFooStore()
48+
v, ok := fooStore.Get("foo")
49+
if !ok {
50+
panic("foo not found")
51+
}
52+
print(v)
53+
54+
}

Diff for: internal/wire/testdata/Generic/foo/wire.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Wire Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//go:build wireinject
15+
// +build wireinject
16+
17+
// wire.go
18+
package main
19+
20+
import "github.com/google/wire"
21+
22+
func InitializeFooStore() FooStorer {
23+
wire.Build(
24+
NewFooStore,
25+
wire.Value(Foo("foo hello value")),
26+
wire.Bind(new(FooStorer), new(Store[string, Foo])),
27+
)
28+
return nil
29+
}

Diff for: internal/wire/testdata/Generic/pkg

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.com/foo

Diff for: internal/wire/testdata/Generic/want/program_out.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo hello value

Diff for: internal/wire/testdata/Generic/want/wire_gen.go

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)