-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathany_type.go
123 lines (108 loc) · 3.68 KB
/
any_type.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
package assert
import "reflect"
type AnyType struct {
logFacade *logFacade
actual interface{}
}
func (a *AnyType) IsEqualTo(expected interface{}) *AnyType {
return a.isTrue(reflect.DeepEqual(a.actual, expected),
"Expected <%v>, but was <%v>.", expected, a.actual)
}
func (a *AnyType) IsNotEqualTo(expected interface{}) *AnyType {
return a.isTrue(!reflect.DeepEqual(a.actual, expected),
"Expected value not equal to <%v>, but was equal.", expected)
}
func (a *AnyType) IsNil() *AnyType {
return a.isTrue(a.actual == nil,
"Expected value to be nil, but was <%v>.", a.actual)
}
func (a *AnyType) IsNotNil() *AnyType {
return a.isTrue(a.actual != nil,
"Expected value not to be nil, but was.")
}
func (a *AnyType) AsBool() *Bool {
val, kind := valueWithKind(a.actual)
if kind == reflect.Bool {
return &Bool{a.logFacade, val.Bool()}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <bool>.", a.actual, a.actual)
return &Bool{}
}
func (a *AnyType) AsInt() *Int {
val, kind := valueWithKind(a.actual)
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
return &Int{a.logFacade, int(val.Int())}
case reflect.Uint8, reflect.Uint16:
return &Int{a.logFacade, int(val.Uint())}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <int>.", a.actual, a.actual)
return &Int{}
}
func (a *AnyType) AsInt64() *Int64 {
val, kind := valueWithKind(a.actual)
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return &Int64{a.logFacade, val.Int()}
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return &Int64{a.logFacade, int64(val.Uint())}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <int64>.", a.actual, a.actual)
return &Int64{}
}
func (a *AnyType) AsUint64() *Uint64 {
val, kind := valueWithKind(a.actual)
switch kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return &Uint64{a.logFacade, val.Uint()}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <uint64>.", a.actual, a.actual)
return &Uint64{}
}
func (a *AnyType) AsFloat() *Float {
val, kind := valueWithKind(a.actual)
switch kind {
case reflect.Float32, reflect.Float64:
return &Float{a.logFacade, val.Float()}
case reflect.Int8, reflect.Int16, reflect.Int32:
return &Float{a.logFacade, float64(val.Int())}
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return &Float{a.logFacade, float64(val.Uint())}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <float64>.", a.actual, a.actual)
return &Float{}
}
func (a *AnyType) AsComplex() *Complex {
val, kind := valueWithKind(a.actual)
switch kind {
case reflect.Complex64, reflect.Complex128:
return &Complex{a.logFacade, val.Complex()}
case reflect.Float32, reflect.Float64:
return &Complex{a.logFacade, complex(val.Float(), 0)}
case reflect.Int8, reflect.Int16, reflect.Int32:
return &Complex{a.logFacade, complex(float64(val.Int()), 0)}
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return &Complex{a.logFacade, complex(float64(val.Uint()), 0)}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <complex128>.", a.actual, a.actual)
return &Complex{}
}
func (a *AnyType) AsString() *String {
val, kind := valueWithKind(a.actual)
if kind == reflect.String {
return &String{a.logFacade, val.String()}
}
a.isTrue(false, "Cannot convert <%v> of type <%T> to <string>.", a.actual, a.actual)
return &String{}
}
func (a *AnyType) isTrue(condition bool, format string, args ...interface{}) *AnyType {
logIfFalse(a.logFacade, condition, format, args...)
return a
}
func valueWithKind(data interface{}) (val reflect.Value, kind reflect.Kind) {
if data != nil {
val = reflect.ValueOf(data)
kind = val.Type().Kind()
}
return
}