From fecc592d48e4f54b9c52e1e3b53aedd2d16ccc51 Mon Sep 17 00:00:00 2001 From: "nguyen.duy.vu (ikarus)" Date: Mon, 11 Mar 2024 09:09:10 +0900 Subject: [PATCH] update ast to support generic --- internal/wire/copyast.go | 7 +++ internal/wire/testdata/Generic/foo/foo.go | 54 +++++++++++++++++++ internal/wire/testdata/Generic/foo/wire.go | 29 ++++++++++ internal/wire/testdata/Generic/pkg | 1 + .../testdata/Generic/want/program_out.txt | 1 + .../wire/testdata/Generic/want/wire_gen.go | 19 +++++++ 6 files changed, 111 insertions(+) create mode 100644 internal/wire/testdata/Generic/foo/foo.go create mode 100644 internal/wire/testdata/Generic/foo/wire.go create mode 100644 internal/wire/testdata/Generic/pkg create mode 100644 internal/wire/testdata/Generic/want/program_out.txt create mode 100644 internal/wire/testdata/Generic/want/wire_gen.go diff --git a/internal/wire/copyast.go b/internal/wire/copyast.go index 179d1c64..9ec63d70 100644 --- a/internal/wire/copyast.go +++ b/internal/wire/copyast.go @@ -263,6 +263,13 @@ func copyAST(original ast.Node) ast.Node { Index: exprFromMap(m, node.Index), Rbrack: node.Rbrack, } + case *ast.IndexListExpr: + m[node] = &ast.IndexListExpr{ + X: exprFromMap(m, node.X), + Lbrack: node.Lbrack, + Indices: copyExprList(m, node.Indices), + Rbrack: node.Rbrack, + } case *ast.InterfaceType: m[node] = &ast.InterfaceType{ Interface: node.Interface, diff --git a/internal/wire/testdata/Generic/foo/foo.go b/internal/wire/testdata/Generic/foo/foo.go new file mode 100644 index 00000000..3d93e859 --- /dev/null +++ b/internal/wire/testdata/Generic/foo/foo.go @@ -0,0 +1,54 @@ +// Copyright 2018 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 + +type Foo string + +type FooStorer interface { + Set(key string, value Foo) + Get(key string) (Foo, bool) +} + +type Store[TKey comparable, TValue any] struct { + store map[TKey]TValue +} + +func NewStore[TKey comparable, TValue any]() Store[TKey, TValue] { + return Store[TKey, TValue]{store: make(map[TKey]TValue)} +} + +func (s Store[TKey, TValue]) Set(key TKey, value TValue) { + s.store[key] = value +} + +func (s Store[TKey, TValue]) Get(key TKey) (TValue, bool) { + value, ok := s.store[key] + return value, ok +} + +func NewFooStore(foo Foo) Store[string, Foo] { + r := NewStore[string, Foo]() + r.Set("foo", foo) + return r +} + +func main() { + fooStore := InitializeFooStore() + v, ok := fooStore.Get("foo") + if !ok { + panic("foo not found") + } + print(v) + +} diff --git a/internal/wire/testdata/Generic/foo/wire.go b/internal/wire/testdata/Generic/foo/wire.go new file mode 100644 index 00000000..bfe12c6f --- /dev/null +++ b/internal/wire/testdata/Generic/foo/wire.go @@ -0,0 +1,29 @@ +// Copyright 2018 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. +//go:build wireinject +// +build wireinject + +// wire.go +package main + +import "github.com/google/wire" + +func InitializeFooStore() FooStorer { + wire.Build( + NewFooStore, + wire.Value(Foo("foo hello value")), + wire.Bind(new(FooStorer), new(Store[string, Foo])), + ) + return nil +} diff --git a/internal/wire/testdata/Generic/pkg b/internal/wire/testdata/Generic/pkg new file mode 100644 index 00000000..056e4d30 --- /dev/null +++ b/internal/wire/testdata/Generic/pkg @@ -0,0 +1 @@ +example.com/foo \ No newline at end of file diff --git a/internal/wire/testdata/Generic/want/program_out.txt b/internal/wire/testdata/Generic/want/program_out.txt new file mode 100644 index 00000000..a2d1dc84 --- /dev/null +++ b/internal/wire/testdata/Generic/want/program_out.txt @@ -0,0 +1 @@ +foo hello value \ No newline at end of file diff --git a/internal/wire/testdata/Generic/want/wire_gen.go b/internal/wire/testdata/Generic/want/wire_gen.go new file mode 100644 index 00000000..f4ee5a87 --- /dev/null +++ b/internal/wire/testdata/Generic/want/wire_gen.go @@ -0,0 +1,19 @@ +// Code generated by Wire. DO NOT EDIT. + +//go:generate go run -mod=mod github.com/google/wire/cmd/wire +//go:build !wireinject +// +build !wireinject + +package main + +// Injectors from wire.go: + +func InitializeFooStore() FooStorer { + foo := _wireFooValue + store := NewFooStore(foo) + return store +} + +var ( + _wireFooValue = Foo("foo hello value") +)