/
stream.go
188 lines (163 loc) · 4.26 KB
/
stream.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
package stream
import (
"github.com/wesovilabs/koazee/errors"
"reflect"
)
// Output structure for returning single values
type Output struct {
value reflect.Value
error *errors.Error
}
// Val reurn the Output of the Stream
func (o Output) Val() interface{} {
v := (o.value)
return v.Interface()
}
// Err reurn the error in the Stream
func (o Output) Err() *errors.Error { return o.error }
// Bool parses the Output of the Stream as a bool type
func (o Output) Bool() bool {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Bool {
return o.value.Interface().(bool)
}
return false
}
// String parses the Output of the Stream as a string type
func (o Output) String() string {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.String {
return o.value.Interface().(string)
}
return ""
}
// Int parses the Output of the Stream as a int type
func (o Output) Int() int {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Int {
return o.value.Interface().(int)
}
return 0
}
// Int8 parses the Output of the Stream as a int8 type
func (o Output) Int8() int8 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Int8 {
return o.value.Interface().(int8)
}
return 0
}
// Int16 parses the Output of the Stream as a int16 type
func (o Output) Int16() int16 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Int16 {
return o.value.Interface().(int16)
}
return 0
}
// Int32 parses the Output of the Stream as a int32 type
func (o Output) Int32() int32 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Int32 {
return o.value.Interface().(int32)
}
return 0
}
// Int64 parses the Output of the Stream as a int64 type
func (o Output) Int64() int64 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Int64 {
return o.value.Interface().(int64)
}
return 0
}
// Uint parses the Output of the Stream as a Uint type
func (o Output) Uint() uint {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Uint {
return o.value.Interface().(uint)
}
return 0
}
// Uint8 parses the Output of the Stream as a Uint type8
func (o Output) Uint8() uint8 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Uint8 {
return o.value.Interface().(uint8)
}
return 0
}
// Uint16 parses the Output of the Stream as a Uint type16
func (o Output) Uint16() uint16 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Uint16 {
return o.value.Interface().(uint16)
}
return 0
}
// Uint32 parses the Output of the Stream as a Uint type32
func (o Output) Uint32() uint32 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Uint32 {
return o.value.Interface().(uint32)
}
return 0
}
// Uint64 parses the Output of the Stream as a Uint type64
func (o Output) Uint64() uint64 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Uint64 {
return o.value.Interface().(uint64)
}
return 0
}
// Float32 parses the Output of the Stream as a Uint float32
func (o Output) Float32() float32 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Float32 {
return o.value.Interface().(float32)
}
return 0.00
}
// Float64 parses the Output of the Stream as a Uint float64
func (o Output) Float64() float64 {
if reflect.TypeOf(o.value.Interface()).Kind() == reflect.Float64 {
return o.value.Interface().(float64)
}
return 0.00
}
type lazyOp interface {
run(Stream) Stream
}
// Stream stream structure
type Stream struct {
items interface{}
itemsValue reflect.Value
itemsType reflect.Type
itemsLen int
err *errors.Error
operations []lazyOp
}
func (s Stream) run() Stream {
if len(s.operations) == 0 {
return s
}
s = s.operations[0].run(s)
if s.err != nil {
return s
}
s.operations = s.operations[1:]
return s.run()
}
// New creates a Stream with the provided array of elements
func New(items interface{}) Stream {
s := Stream{}
if items != nil {
itemsValue := reflect.ValueOf(items)
itemsType := reflect.TypeOf(items).Elem()
s.items = items
s.itemsType = itemsType
s.itemsValue = itemsValue
s.itemsLen = itemsValue.Len()
}
return s
}
func (s Stream) withItemsValue(items reflect.Value) Stream {
s.itemsValue = items
s.items = items.Interface()
s.itemsLen = items.Len()
return s
}
// Error initialize the Stream with error
func Error(err *errors.Error) Stream {
return Stream{
err: err,
}
}