-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
263 lines (227 loc) · 6.97 KB
/
generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (C) 2018 Google Inc.
//
// 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
//
// http://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 generic provides methods to test whether a type conforms to a generic
// interface.
package generic
import (
"bytes"
"fmt"
"reflect"
)
type (
// T1 is a placeholder type that can be used for generic subsitutions.
T1 struct{}
// T2 is a placeholder type that can be used for generic subsitutions.
T2 struct{}
// T3 is a placeholder type that can be used for generic subsitutions.
T3 struct{}
// T4 is a placeholder type that can be used for generic subsitutions.
T4 struct{}
// TO is a placeholder type that represents the type that is being checked
// for interface compliance (ty parameter to Implements).
TO struct{}
// TP is a placeholder type that represents a pointer to the type that is
// being checked for interface compliance.
TP struct{}
// Any is the type used for open types.
Any struct{}
)
var (
// T1Ty is the type of T1.
T1Ty = reflect.TypeOf(T1{})
// T2Ty is the type of T2.
T2Ty = reflect.TypeOf(T2{})
// T3Ty is the type of T3.
T3Ty = reflect.TypeOf(T3{})
// T4Ty is the type of T4.
T4Ty = reflect.TypeOf(T4{})
// TOTy is the type of TO.
TOTy = reflect.TypeOf(TO{})
// TPTy is the type of TP.
TPTy = reflect.TypeOf(TP{})
anyTy = reflect.TypeOf(Any{})
)
// Match is the result of Implements and CheckSigs.
type Match struct {
// Errors found matching the interface to the implementation.
Errors []error
// Bindings of generic type to matched type.
Bindings map[reflect.Type]reflect.Type
}
// Ok returns true if the type implemented the generic interface.
func (m Match) Ok() bool { return len(m.Errors) == 0 }
func newSubs(generics ...reflect.Type) map[reflect.Type]reflect.Type {
subs := map[reflect.Type]reflect.Type{}
subs[T1Ty] = nil
subs[T2Ty] = nil
subs[T3Ty] = nil
subs[T4Ty] = nil
for _, g := range generics {
subs[g] = nil
}
return subs
}
// Implements checks that ty implements the generic interface iface.
// Implements returns a Match which lists any generic type inconsistencies, and
// lists the mappings of generic types to implementation types.
// generics is a list of open types used by the generic interface iface.
func Implements(ty, iface reflect.Type, generics ...reflect.Type) Match {
var errs []error
subs := newSubs(generics...)
subs[TOTy] = ty
subs[TPTy] = reflect.PtrTo(ty)
for i, c := 0, iface.NumMethod(); i < c; i++ {
iM := iface.Method(i)
oM, ok := ty.MethodByName(iM.Name)
if !ok {
errs = append(errs, fmt.Errorf("'%v' does not implement method '%v'", ty, iM.Name))
continue
}
if err := checkMethod(oM, iM, subs, true); err != nil {
errs = append(errs, err)
}
}
return Match{errs, subs}
}
// Sig is a pair of functions used for validation by CheckSigs.
type Sig struct {
// Name of the function.
Name string
// The signature the function must match.
Interface interface{}
// The provided function being checked.
Function interface{}
}
// CheckSigs checks that each function signature implements the generic
// interface signature.
// CheckSigs returns a Match which lists any generic type inconsistencies, and
// lists the mappings of generic types to implementation types.
func CheckSigs(sigs ...Sig) Match {
var errs []error
subs := newSubs()
for i, s := range sigs {
fun := reflect.TypeOf(s.Function)
if fun.Kind() != reflect.Func {
panic(fmt.Errorf("Signature for '%v' requires a function for Function", s.Name))
}
iface := reflect.TypeOf(s.Interface)
if iface.Kind() != reflect.Func {
panic(fmt.Errorf("Signature for '%v' requires a function for Interface", s.Name))
}
oM := reflect.Method{Name: s.Name, Type: fun, Index: i}
iM := reflect.Method{Name: s.Name, Type: iface, Index: i}
if err := checkMethod(oM, iM, subs, false); err != nil {
errs = append(errs, err)
}
}
return Match{errs, subs}
}
func checkMethod(oM, iM reflect.Method, subs map[reflect.Type]reflect.Type, hasReceiver bool) error {
fail := func(err error) error {
return fmt.Errorf("%v\nInterface: %v\nImplementor: %v",
err, printFunc(iM.Type, 0), printFunc(oM.Type, 1))
}
numReceivers := 0
if hasReceiver {
numReceivers = 1
}
if oC, iC := oM.Type.NumIn()-numReceivers, iM.Type.NumIn(); oC == iC {
for i, c := 0, iM.Type.NumIn(); i < c; i++ {
iTy, oTy := iM.Type.In(i), oM.Type.In(i+numReceivers)
if err := checkType(oTy, iTy, subs); err != nil {
return fail(err)
}
}
} else {
if oC < iC {
return fail(fmt.Errorf("method '%v' has too few parameters", iM.Name))
}
return fail(fmt.Errorf("method '%v' has too many parameters", iM.Name))
}
if oC, iC := oM.Type.NumOut(), iM.Type.NumOut(); oC >= iC {
// Too many return values is okay so long as the ones that are returned match.
for i, c := 0, iM.Type.NumOut(); i < c; i++ {
iTy, oTy := iM.Type.Out(i), oM.Type.Out(i)
if err := checkType(oTy, iTy, subs); err != nil {
return fail(err)
}
}
} else {
return fail(fmt.Errorf("method '%v' has too few return values", iM.Name))
}
return nil
}
func checkType(oTy, iTy reflect.Type, subs map[reflect.Type]reflect.Type) error {
if iTy.Kind() == oTy.Kind() {
switch iTy.Kind() {
case reflect.Slice, reflect.Array:
return checkType(oTy.Elem(), iTy.Elem(), subs)
case reflect.Map:
if err := checkType(oTy.Key(), iTy.Key(), subs); err != nil {
return err
}
return checkType(oTy.Elem(), iTy.Elem(), subs)
}
}
switch iTy {
case oTy, anyTy: // type match
return nil
}
sub, ok := subs[iTy]
if !ok {
return fmt.Errorf("type mismatch '%v' and '%v'", iTy, oTy)
}
switch sub {
case nil: // First time this generic was seen. Map it to oTy.
subs[iTy] = oTy
case oTy, anyTy: // Consistent generic usage.
default:
// We're erroring about inconsistent usage of this generic.
// Mark it as any to prevent repeated errors of this generic.
subs[iTy] = anyTy
return fmt.Errorf("mixed use of generic type '%v'. First used as '%v', now used as '%v'",
iTy, sub, oTy)
}
return nil
}
func printFunc(f reflect.Type, fisrtParm int) string {
if f.Kind() != reflect.Func {
return "Not a function"
}
b := bytes.Buffer{}
b.WriteString("func(")
for i := fisrtParm; i < f.NumIn(); i++ {
if i > fisrtParm {
b.WriteString(", ")
}
b.WriteString(f.In(i).String())
}
b.WriteString(")")
switch f.NumOut() {
case 0:
case 1:
b.WriteString(" ")
b.WriteString(f.Out(0).String())
default:
b.WriteString(" (")
for i := 0; i < f.NumOut(); i++ {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(f.Out(i).String())
}
b.WriteString(")")
}
return b.String()
}