-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
262 lines (224 loc) · 7.56 KB
/
errors.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
// errors are a set of functions useful for type checks, given the absence
// of static type checking due to reflection.
package rel
import (
"fmt"
"reflect"
)
// I've tried to reproduce go's type error strings here, because these errors
// act as a (poor) replacement for static type checking.
// ContainerError represents an error that occurs when the wrong kind of
// container is given to a TupleChan, TupleSlice, or TupleMap method, as
// indicated by the name of the method.
type ContainerError struct {
Expected reflect.Kind
Found reflect.Kind
}
func (e *ContainerError) Error() string {
return "rel: expected tuple container '" + e.Expected.String() + "', found '" + e.Found.String() + "'"
}
// ElemError represents an error that occurs when the wrong kind of element
// is provided to a container given to TupleChan, TupleSlice, or TupleMap
// methods of relations.
type ElemError struct {
Expected reflect.Type
Found reflect.Type
}
func (e *ElemError) Error() string {
return "rel: expected tuple element '" + e.Expected.Name() + "', found '" + e.Found.Name() + "'"
}
// EnsureChan returns an error if the the input is not a channel with elements
// of the specified type.
func EnsureChan(ch reflect.Type, zero interface{}) error {
if t := ch.Kind(); t != reflect.Chan {
return &ContainerError{t, reflect.Chan}
}
// now check that the zero element can be sent to the channel
te := ch.Elem()
ze := reflect.TypeOf(zero)
if te != ze {
return &ElemError{ze, te}
}
return nil
}
// EnsureSlice returns an error if the the input is not a slice with elements
// of the specified type.
func EnsureSlice(sl reflect.Type, zero interface{}) error {
if t := sl.Kind(); t != reflect.Slice {
return &ContainerError{t, reflect.Slice}
}
// now check that the zero element can be sent to the channel
te := sl.Elem()
ze := reflect.TypeOf(zero)
if te != ze {
return &ElemError{ze, te}
}
return nil
}
// EnsureMap returns an error if the the input is not a map with key elements
// of the specified type, and value elements of type struct{}
func EnsureMap(m reflect.Type, zero interface{}) error {
if t := m.Kind(); t != reflect.Map {
return &ContainerError{t, reflect.Map}
}
// now check that the zero element can be sent to the channel
tk := m.Key()
ze := reflect.TypeOf(zero)
if tk != ze {
return &ElemError{ze, tk}
}
te := m.Elem()
empty := reflect.TypeOf(struct{}{})
if te != empty {
return fmt.Errorf("rel: Non-empty map value type, '%v'", te)
}
return nil
}
// funcArityError represents an error that occurs when the wrong number of
// inputs or outputs to a function are provided to groupby or map
type funcArityError struct {
Expected int
Found int
}
// NumInError represents an error that occurs when the wrong number of
// inputs to a function are provided to groupby or map
type NumInError funcArityError
func (e *NumInError) Error() string {
return fmt.Sprintf("rel: expected input arity %d, found %d", e.Expected, e.Found)
}
// NumOutError represents an error that occurs when the wrong number of
// outputs to a function are provided to groupby or map
type NumOutError funcArityError
func (e *NumOutError) Error() string {
return fmt.Sprintf("rel: expected output arity %d, found %d", e.Expected, e.Found)
}
// domainErorr represents an error that occurs when the input or output tuples
// of a function are not subdomains of the expected domains
type domainError struct {
Expected []Attribute
Found []Attribute
}
// InDomainError represents an error that occurs when the input tuples
// of a function are not subdomains of the expected domain
type InDomainError domainError
func (e *InDomainError) Error() string {
return fmt.Sprintf("rel: expected function input to be subdomain of %v, found %v", e.Expected, e.Found)
}
// OutDomainError represents an error that occurs when the output tuples
// of a function are not subdomains of the expected domain
type OutDomainError domainError
func (e *OutDomainError) Error() string {
return fmt.Sprintf("rel: expected function output to be subdomain of %v, found %v", e.Expected, e.Found)
}
// EnsureGroupFunc returns an error if the input is not a function with only
// one input and one output, where the input and output are subdomains of given
// tuples.
func EnsureGroupFunc(gfcn reflect.Type, inSuper, outSuper interface{}) (inTup, outTup reflect.Type, err error) {
if t := gfcn.Kind(); t != reflect.Func {
err = &ContainerError{t, reflect.Func}
return
}
if ni := gfcn.NumIn(); ni != 1 {
err = &NumInError{1, ni}
return
}
ch := gfcn.In(0)
if t := ch.Kind(); t != reflect.Chan {
err = &ContainerError{t, reflect.Chan}
return
}
inTup = ch.Elem()
if no := gfcn.NumOut(); no != 1 {
err = &NumOutError{1, no}
return
}
outTup = gfcn.Out(0)
// check that the fields are subdomains
inDomain := FieldNames(reflect.TypeOf(inSuper))
if fn := FieldNames(inTup); !IsSubDomain(fn, inDomain) {
err = &InDomainError{inDomain, fn}
return
}
outDomain := FieldNames(reflect.TypeOf(outSuper))
if fn := FieldNames(outTup); !IsSubDomain(fn, outDomain) {
err = &OutDomainError{outDomain, fn}
return
}
return
}
// EnsureMapFunc returns an error if the input is not a function with only
// one input and one output, where the input is a subdomain of given
// tuple.
func EnsureMapFunc(mfcn reflect.Type, inSuper interface{}) (inTup, outTup reflect.Type, err error) {
if t := mfcn.Kind(); t != reflect.Func {
err = &ContainerError{t, reflect.Func}
return
}
if ni := mfcn.NumIn(); ni != 1 {
err = &NumInError{1, ni}
return
}
inTup = mfcn.In(0)
if no := mfcn.NumOut(); no != 1 {
err = &NumOutError{1, no}
return
}
outTup = mfcn.Out(0)
// check that the fields are subdomains
inDomain := FieldNames(reflect.TypeOf(inSuper))
if fn := FieldNames(inTup); !IsSubDomain(fn, inDomain) {
err = &InDomainError{inDomain, fn}
return
}
return
}
// AttributeSubsetError represents an error that occurs when a method on a
// relation is called with a set of tuples that are not a subset of an expected
// type.
type AttributeSubsetError domainError
func (e *AttributeSubsetError) Error() string {
return fmt.Sprintf("rel: expected attributes to be a subset of %v, found %v", e.Expected, e.Found)
}
// EnsureSubDomain returns an error if the input sub is not a subdomain of
// input dom.
func EnsureSubDomain(sub, dom []Attribute) (err error) {
if IsSubDomain(sub, dom) {
return
}
// figure out the attributes that are in sub that are not in dom
invalidAttributes := make([]Attribute, 1)
SubLoop:
for _, n1 := range sub {
for _, n2 := range dom {
if n1 == n2 {
continue SubLoop
}
}
invalidAttributes = append(invalidAttributes, n1)
}
return &AttributeSubsetError{dom, invalidAttributes}
}
// DegreeError represents an error that occurs when the input tuples to a
// relational operation do not have the same degree as expected. This only
// occurs in rename operations.
type DegreeError struct {
Expected int
Found int
}
func (e *DegreeError) Error() string {
return fmt.Sprintf("rel: expected degree %d, found %d", e.Expected, e.Found)
}
// DomainMismatchError represents an error that occurs when two tuples have a
// different set of attributes.
type DomainMismatchError domainError
func (e *DomainMismatchError) Error() string {
return fmt.Sprintf("rel: mismatched domains found: %v, and %v", e.Expected, e.Found)
}
// EnsureSameDomain returns an error if the inputs do not have the same domain.
func EnsureSameDomain(sub, dom []Attribute) (err error) {
if len(sub) == len(dom) && IsSubDomain(sub, dom) {
return
}
// sub and dom do not have any particular order.
return &DomainMismatchError{sub, dom}
}