Skip to content

Commit 740527b

Browse files
authored
Merge pull request #27 from bitflow-stream/anton-added-time-parameter
Time parsing in bitflow script
2 parents 4f64f7a + 2644dce commit 740527b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

script/reg/params.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"strconv"
66
"time"
7+
8+
"github.com/antongulenko/golib"
79
)
810

911
func ParameterError(name string, err error) error {
@@ -52,6 +54,10 @@ func Duration() ParameterParser {
5254
return primitiveParser{primitive: durationParser{}}
5355
}
5456

57+
func Time() ParameterParser {
58+
return primitiveParser{primitive: timeParser{}}
59+
}
60+
5561
func Bool() ParameterParser {
5662
return primitiveParser{primitive: boolParser{}}
5763
}
@@ -228,6 +234,51 @@ func (durationParser) CorrectMapType(val interface{}) (res bool) {
228234
return
229235
}
230236

237+
type timeParser struct {
238+
}
239+
240+
func (timeParser) String() string {
241+
return "time"
242+
}
243+
func (timeParser) ParsePrimitive(val string) (interface{}, error) {
244+
245+
return time.Parse(golib.SimpleTimeLayout+".999999", val)
246+
}
247+
func (p timeParser) ParseList(val []string) (interface{}, error) {
248+
result := make([]time.Time, len(val))
249+
for i, v := range val {
250+
parsed, err := p.ParsePrimitive(v)
251+
if err != nil {
252+
return nil, err
253+
}
254+
result[i] = parsed.(time.Time)
255+
}
256+
return result, nil
257+
}
258+
func (p timeParser) ParseMap(val map[string]string) (interface{}, error) {
259+
result := make(map[string]time.Time, len(val))
260+
for key, v := range val {
261+
parsed, err := p.ParsePrimitive(v)
262+
if err != nil {
263+
return nil, err
264+
}
265+
result[key] = parsed.(time.Time)
266+
}
267+
return result, nil
268+
}
269+
func (timeParser) CorrectPrimitiveType(val interface{}) (res bool) {
270+
_, res = val.(time.Time)
271+
return
272+
}
273+
func (timeParser) CorrectListType(val interface{}) (res bool) {
274+
_, res = val.([]time.Time)
275+
return
276+
}
277+
func (timeParser) CorrectMapType(val interface{}) (res bool) {
278+
_, res = val.(map[string]time.Time)
279+
return
280+
}
281+
231282
type boolParser struct {
232283
}
233284

script/script/parser_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func TestParseScript_listAndMapParams(t *testing.T) {
150150
optional2 string
151151
list1 []time.Duration
152152
list2 []int
153+
list3 []time.Time
153154
emptyList []float64
154155
map1 map[string]string
155156
map2 map[string]int
@@ -165,6 +166,7 @@ func TestParseScript_listAndMapParams(t *testing.T) {
165166
func(pipeline *bitflow.SamplePipeline, params map[string]interface{}) error {
166167
list1 = params["list1"].([]time.Duration)
167168
list2 = params["list2"].([]int)
169+
list3 = params["list3"].([]time.Time)
168170
map1 = params["map1"].(map[string]string)
169171
map2 = params["map2"].(map[string]int)
170172
emptyMap = params["emptyMap"].(map[string]float64)
@@ -181,6 +183,7 @@ func TestParseScript_listAndMapParams(t *testing.T) {
181183
}, "step with list and map parameters").
182184
Required("list1", reg.List(reg.Duration())).
183185
Required("list2", reg.List(reg.Int())).
186+
Required("list3", reg.List(reg.Time())).
184187
Required("map1", reg.Map(reg.String())).
185188
Required("map2", reg.Map(reg.Int())).
186189
Required("emptyMap", reg.Map(reg.Float())).
@@ -196,7 +199,7 @@ func TestParseScript_listAndMapParams(t *testing.T) {
196199

197200
testScript := "special_params(list2=[ 1,'2',3], map1 = { x=y, 1=2, ' '=v } , list1 = [1s ,2h, 3m] " +
198201
", map2 = { 4=5 }, emptyList=[], emptyMap={}, optionalList2= [50,60], optionalMap1={ g=40, h=60 }," +
199-
"normal1= 'true', 'normal2'= 33, optional1= 3.444)"
202+
"normal1= 'true', 'normal2'= 33, optional1= 3.444, list3=['2100-10-10 10:10:10.123456', '1990-05-06 07:15:06.1'])"
200203
_, errs := parser.ParseScript(testScript)
201204
assert.NoError(errs.NilOrError())
202205

@@ -214,6 +217,11 @@ func TestParseScript_listAndMapParams(t *testing.T) {
214217
assert.Equal(33, normal2)
215218
assert.Equal(3.444, optional1)
216219
assert.Equal("defaultVal2", optional2)
220+
221+
assert.Len(list3, 2)
222+
format := "2006-01-02 15:04:05.999999"
223+
assert.Equal(time.Date(2100, time.October, 10, 10, 10, 10, 123456*1000, time.Local).Format(format), list3[0].Format(format))
224+
assert.Equal(time.Date(1990, time.May, 6, 7, 15, 06, 100*1000*1000, time.Local).Format(format), list3[1].Format(format))
217225
}
218226

219227
func createTestParser() (BitflowScriptParser, *testOutputCatcher) {

0 commit comments

Comments
 (0)