-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
221 lines (180 loc) · 4.69 KB
/
operation.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
package calculator
import (
"errors"
)
// MonoCalculation performs an operation against two values
type MonoCalculation interface {
MonoCalculate(a interface{}) (interface{}, error)
}
// BiCalculation performs an operation against two values
type BiCalculation interface {
BiCalculate(a, b interface{}) (interface{}, error)
}
type biOpCommon struct {
intOp func(a, b int) (interface{}, error)
floatOp func(a, b float64) (interface{}, error)
stringOp func(a, b string) (interface{}, error)
boolOp func(a, b bool) (interface{}, error)
}
// BiOpDef implements an operation whose behaviour depends on the type
// of the left hand side
type BiOpDef struct {
biOpCommon
}
func (op *BiOpDef) doInt(a int, b interface{}) (interface{}, error) {
c, err := GetInt(b)
if err != nil {
return nil, err
}
return op.intOp(a, c)
}
func (op *BiOpDef) doFloat(a float64, b interface{}) (interface{}, error) {
c, err := GetFloat(b)
if err != nil {
return nil, err
}
return op.floatOp(a, c)
}
func (op *BiOpDef) doString(a string, b interface{}) (interface{}, error) {
c, err := GetString(b)
if err != nil {
return nil, err
}
return op.stringOp(a, c)
}
func (op *BiOpDef) doBool(a bool, b interface{}) (interface{}, error) {
c, err := GetBool(b)
if err != nil {
return nil, err
}
return op.boolOp(a, c)
}
func (op *BiOpDef) BiCalculate(a0, b0 interface{}) (interface{}, error) {
// Convert so a and b are the same type
a, b, err := Convert(a0, b0)
if err != nil {
return nil, err
}
if op.floatOp != nil {
if f, ok := GetFloatRaw(a); ok {
return op.doFloat(f, b)
}
}
if op.intOp != nil {
if i, ok := GetIntRaw(a); ok {
return op.doInt(i, b)
}
}
if op.stringOp != nil {
if s, ok := GetStringRaw(a); ok {
return op.doString(s, b)
}
}
if op.boolOp != nil {
if ab, ok := GetBoolRaw(a); ok {
return op.doBool(ab, b)
}
}
return nil, invalidOperation
}
var invalidOperation = errors.New("invalid operation")
// NewBiOpDef creates a new NewBiOp based on the provided
func NewBiOpDef() BiOpDefBuilder {
return &biOpBuilder{}
}
type BiOpDefBuilder interface {
Int(func(a, b int) (interface{}, error)) BiOpDefBuilder
Float(func(a, b float64) (interface{}, error)) BiOpDefBuilder
String(func(a, b string) (interface{}, error)) BiOpDefBuilder
Bool(func(a, b bool) (interface{}, error)) BiOpDefBuilder
Build() *BiOpDef
}
type biOpBuilder struct {
biOpCommon
}
func (b *biOpBuilder) Int(f func(a, b int) (interface{}, error)) BiOpDefBuilder {
b.intOp = f
return b
}
func (b *biOpBuilder) Float(f func(a, b float64) (interface{}, error)) BiOpDefBuilder {
b.floatOp = f
return b
}
func (b *biOpBuilder) String(f func(a, b string) (interface{}, error)) BiOpDefBuilder {
b.stringOp = f
return b
}
func (b *biOpBuilder) Bool(f func(a, b bool) (interface{}, error)) BiOpDefBuilder {
b.boolOp = f
return b
}
func (b *biOpBuilder) Build() *BiOpDef {
return &BiOpDef{biOpCommon: b.biOpCommon}
}
// NewMonoOpDef creates a new NewBiOp based on the provided
func NewMonoOpDef() MonoOpDefBuilder {
return &monoOpBuilder{}
}
type MonoOpDefBuilder interface {
Int(func(a int) (interface{}, error)) MonoOpDefBuilder
Float(func(a float64) (interface{}, error)) MonoOpDefBuilder
String(func(a string) (interface{}, error)) MonoOpDefBuilder
Bool(func(a bool) (interface{}, error)) MonoOpDefBuilder
Build() *MonoOpDef
}
type monoOpCommon struct {
intOp func(a int) (interface{}, error)
floatOp func(a float64) (interface{}, error)
stringOp func(a string) (interface{}, error)
boolOp func(a bool) (interface{}, error)
}
// MonoOpDef implements an operation whose behaviour depends on the type
// of the left hand side
type MonoOpDef struct {
monoOpCommon
}
func (op *MonoOpDef) MonoCalculate(a interface{}) (interface{}, error) {
if op.floatOp != nil {
if f, ok := GetFloatRaw(a); ok {
return op.floatOp(f)
}
}
if op.intOp != nil {
if i, ok := GetIntRaw(a); ok {
return op.intOp(i)
}
}
if op.stringOp != nil {
if s, ok := GetStringRaw(a); ok {
return op.stringOp(s)
}
}
if op.boolOp != nil {
if ab, ok := GetBoolRaw(a); ok {
return op.boolOp(ab)
}
}
return nil, invalidOperation
}
type monoOpBuilder struct {
monoOpCommon
}
func (b *monoOpBuilder) Int(f func(a int) (interface{}, error)) MonoOpDefBuilder {
b.intOp = f
return b
}
func (b *monoOpBuilder) Float(f func(a float64) (interface{}, error)) MonoOpDefBuilder {
b.floatOp = f
return b
}
func (b *monoOpBuilder) String(f func(a string) (interface{}, error)) MonoOpDefBuilder {
b.stringOp = f
return b
}
func (b *monoOpBuilder) Bool(f func(a bool) (interface{}, error)) MonoOpDefBuilder {
b.boolOp = f
return b
}
func (b *monoOpBuilder) Build() *MonoOpDef {
return &MonoOpDef{monoOpCommon: b.monoOpCommon}
}