From 893af9b7228f25b605a6ac8f33f777540857f29b Mon Sep 17 00:00:00 2001 From: Vu Nguyen Date: Mon, 10 Jul 2023 21:10:00 +0700 Subject: [PATCH] feat: update ast for supporting generics --- go.mod | 11 ++-- go.sum | 35 +++--------- 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 +++++++ 8 files changed, 125 insertions(+), 32 deletions(-) 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/go.mod b/go.mod index 7335ae1a..6cf81488 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,15 @@ module github.com/google/wire -go 1.12 +go 1.18 require ( github.com/google/go-cmp v0.2.0 github.com/google/subcommands v1.2.0 github.com/pmezard/go-difflib v1.0.0 - golang.org/x/mod v0.4.1 // indirect - golang.org/x/sys v0.0.0-20210216224549-f992740a1bac // indirect - golang.org/x/tools v0.1.0 + golang.org/x/tools v0.11.0 +) + +require ( + golang.org/x/mod v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect ) diff --git a/go.sum b/go.sum index ff9cc31e..15844566 100644 --- a/go.sum +++ b/go.sum @@ -4,31 +4,10 @@ github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY= -golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210216224549-f992740a1bac h1:9glrpwtNjBYgRpb67AZJKHfzj1stG/8BL5H7In2oTC4= -golang.org/x/sys v0.0.0-20210216224549-f992740a1bac/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= +golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= 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..657413e9 --- /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")), + 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..578ad4cd --- /dev/null +++ b/internal/wire/testdata/Generic/want/program_out.txt @@ -0,0 +1 @@ +foo hello \ 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..be28958e --- /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") +)