-
Notifications
You must be signed in to change notification settings - Fork 0
/
badtype.go
58 lines (51 loc) · 1.88 KB
/
badtype.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
package errors
// UnexpectedType is returned when a value is received in a context where a
// value of another type was expected.
type UnexpectedType struct {
// Message contains the error message value.
Message
// Expected contains a value of the expected type. It's value may
// or may not mean anything depending on the message included with
// the error, but its type actually matters and is included in the
// error message.
//
// If Expected's value's type is AnyType, then error messages state
// that the expected type is any of the given slice of
Expected interface{}
// Actual contains a value of the expected type. It's value may
// or may not mean anything depending on the message included with
// the error, but its type actually matters and is included in the
// error message.
Actual interface{}
// slice is an array of the Expected and Actual values to avoid an
// allocation outside of the UnexpectedType value itself.
slice [3]interface{}
}
const errUnexpectedTypeDefaultString = "%[1]T: expected: %[2]v (type: %[2]T), but found: %[3]v (type: %[3]T)"
// NewUnexpectedType returns an UnexpectedType error from the given pair of
// values.
func NewUnexpectedType(expected, actual interface{}) *UnexpectedType {
err := &UnexpectedType{
Message: Message{},
Expected: expected,
Actual: actual,
slice: [...]interface{}{
nil, // the UnexpectdType error itself,
expected,
actual,
},
}
// set the first element of the slice so the error formatting works
// correctly.
err.slice[0] = err
err.Message = Message{
Fmt: errUnexpectedTypeDefaultString,
// slicing an array keeps the array itself instead of allocating
// a new slice:
Args: err.slice[:],
}
return err
}
// AnyType is used in TypeError to indicate that the expected type is any
// of the given types.
type AnyType []interface{}