-
Notifications
You must be signed in to change notification settings - Fork 39
/
operation_chainable_interface.go
202 lines (185 loc) · 6.23 KB
/
operation_chainable_interface.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
package gubrak
// Operation represent the type of chainable operation
type Operation string
const (
OperationNone = ""
OperationChunk = "Chunk()"
OperationCompact = "Compact()"
OperationConcatMany = "ConcatMany()"
OperationConcat = "Concat()"
OperationContains = "Contains()"
OperationCountBy = "CountBy()"
OperationCount = "Count()"
OperationDifferenceMany = "DifferenceMany()"
OperationDifference = "Difference()"
OperationDrop = "Drop()"
OperationDropRight = "DropRight()"
OperationEach = "Each()"
OperationEachRight = "EachRight()"
OperationExclude = "Exclude()"
OperationExcludeMany = "ExcludeMany()"
OperationExcludeAt = "ExcludeAt()"
OperationExcludeAtMany = "ExcludeAtMany()"
OperationForEach = "ForEach()"
OperationForEachRight = "ForEachRight()"
OperationFill = "Fill()"
OperationFilter = "Filter()"
OperationFind = "Find()"
OperationFindIndex = "FindIndex()"
OperationFindLast = "FindLast()"
OperationFindLastIndex = "FindLastIndex()"
OperationFirst = "First()"
OperationHead = "Head()"
OperationFromPairs = "FromPairs()"
OperationGroupBy = "GroupBy()"
OperationIndexOf = "IndexOf()"
OperationInitial = "Initial()"
OperationIntersection = "Intersection()"
OperationIntersectionMany = "IntersectionMany()"
OperationJoin = "Join()"
OperationKeyBy = "KeyBy()"
OperationLast = "Last()"
OperationLastIndexOf = "LastIndexOf()"
OperationMap = "Map()"
OperationNth = "Nth()"
OperationOrderBy = "OrderBy()"
OperationPartition = "Partition()"
OperationReduce = "Reduce()"
OperationReject = "Reject()"
OperationReverse = "Reverse()"
OperationSample = "Sample()"
OperationSampleSize = "SampleSize()"
OperationShuffle = "Shuffle()"
OperationSize = "Size()"
OperationTail = "Tail()"
OperationTake = "Take()"
OperationTakeRight = "TakeRight()"
OperationUniq = "Uniq()"
OperationUnionMany = "UnionMany()"
)
// IChainable is the base interface for chainable functions
// It is contain the `IChainableOperation` interface (embedded), and result-related methods
type IChainable interface {
IChainableOperation
ResultAndError() (interface{}, error)
Result() interface{}
Error() error
IsError() bool
LastSuccessOperation() Operation
LastErrorOperation() Operation
LastOperation() Operation
}
// IChainableOperation is interface for chainable functions declaration
type IChainableOperation interface {
Chunk(int) IChainable
Compact() IChainable
ConcatMany(...interface{}) IChainable
Concat(interface{}) IChainable
CountBy(interface{}) IChainableNumberResult
Count() IChainableNumberResult
DifferenceMany(...interface{}) IChainable
Difference(interface{}) IChainable
Drop(int) IChainable
DropRight(int) IChainable
Each(interface{}) IChainableNoReturnValueResult
EachRight(interface{}) IChainableNoReturnValueResult
Exclude(interface{}) IChainable
ExcludeMany(...interface{}) IChainable
ExcludeAt(int) IChainable
ExcludeAtMany(...int) IChainable
Fill(interface{}, ...int) IChainable
Filter(interface{}) IChainable
Find(interface{}, ...int) IChainable
FindIndex(interface{}, ...int) IChainable
FindLast(interface{}, ...int) IChainable
FindLastIndex(interface{}, ...int) IChainable
First() IChainable
FromPairs() IChainable
GroupBy(interface{}) IChainable
Contains(interface{}, ...int) IChainableBoolResult
IndexOf(interface{}, ...int) IChainableNumberResult
Initial() IChainable
Intersection(interface{}) IChainable
IntersectionMany(data ...interface{}) IChainable
Join(string) IChainableStringResult
KeyBy(interface{}) IChainable
Last() IChainable
LastIndexOf(interface{}, ...int) IChainableNumberResult
Map(interface{}) IChainable
Nth(int) IChainable
OrderBy(interface{}, ...bool) IChainable
Partition(interface{}) IChainableTwoReturnValueResult
Reduce(interface{}, interface{}) IChainable
Reject(interface{}) IChainable
Reverse() IChainable
Sample() IChainable
SampleSize(int) IChainable
Shuffle() IChainable
Size() IChainable
Tail() IChainable
Take(int) IChainable
TakeRight(int) IChainable
Uniq() IChainable
UnionMany(...interface{}) IChainable
}
// Chainable is base type of gubrak chainable operations
type Chainable struct {
data interface{}
lastOperation Operation
lastSuccessOperation Operation
lastErrorOperation Operation
lastErrorCaught error
}
// From is the initial function to use gubrak chainable operation.
// This function requires one argument, the data that are going to be used in operations
func From(data interface{}) IChainable {
g := new(Chainable)
g.data = data
g.lastSuccessOperation = OperationNone
g.lastErrorOperation = OperationNone
g.lastOperation = OperationNone
g.lastErrorCaught = nil
return g
}
func (g *Chainable) markError(data interface{}, err error) *Chainable {
g.data = data
g.lastErrorCaught = err
g.lastErrorOperation = g.lastOperation
return g
}
func (g *Chainable) markResult(data interface{}) *Chainable {
g.data = data
g.lastSuccessOperation = g.lastOperation
return g
}
func (g *Chainable) shouldReturn() bool {
return false
}
// ResultAndError returns the result after operation, and error object
func (g *Chainable) ResultAndError() (interface{}, error) {
return g.Result(), g.Error()
}
// Result returns the result after operation
func (g *Chainable) Result() interface{} {
return g.data
}
// Error returns the error object
func (g *Chainable) Error() error {
return g.lastErrorCaught
}
// IsError `true` on error, otherwise `false`
func (g *Chainable) IsError() bool {
return g.Error() != nil
}
// LastSuccessOperation return last success operation
func (g *Chainable) LastSuccessOperation() Operation {
return g.lastSuccessOperation
}
// LastErrorOperation return last error operation
func (g *Chainable) LastErrorOperation() Operation {
return g.lastErrorOperation
}
// LastOperation return last operation
func (g *Chainable) LastOperation() Operation {
return g.lastOperation
}