diff --git a/provider/handler/processor.go b/provider/handler/processor.go index 5c2ad12..6d9a2c4 100644 --- a/provider/handler/processor.go +++ b/provider/handler/processor.go @@ -16,6 +16,12 @@ const ( processorKey pkey = iota + 1 ) +func FormatFn(fn config.ProcessFunc, opts ...param.Option) param.Option { + return Process(config.ProcessFunc(func(ctx context.Context, in config.Value, _ ...param.Option) (config.Value, error) { + return fn(ctx, in, opts...) + })) +} + func Process(fn config.Processor) param.Option { return func(p param.Params) param.Params { return param.With(p, processorKey, fn) diff --git a/provider/handler/processor_test.go b/provider/handler/processor_test.go index 8644b81..0a4f28e 100644 --- a/provider/handler/processor_test.go +++ b/provider/handler/processor_test.go @@ -3,12 +3,12 @@ package handler_test import ( "context" "testing" + "time" "gitoa.ru/go-4devs/config" "gitoa.ru/go-4devs/config/definition/group" "gitoa.ru/go-4devs/config/definition/option" "gitoa.ru/go-4devs/config/definition/proto" - "gitoa.ru/go-4devs/config/param" "gitoa.ru/go-4devs/config/processor/csv" "gitoa.ru/go-4devs/config/provider/handler" "gitoa.ru/go-4devs/config/provider/memory" @@ -16,9 +16,11 @@ import ( ) var ( - testKey = []string{"test"} - testBool = []string{"group", "service", "bool"} - testInt = []string{"group", "int"} + testKey = []string{"test"} + testBool = []string{"group", "service", "bool"} + testInt = []string{"group", "int"} + testTime = []string{"group", "time"} + testUint64 = []string{"uint64"} ) func TestProcessor(t *testing.T) { @@ -35,7 +37,7 @@ func TestProcessor(t *testing.T) { var tdata []string require.NoError(t, tval.Unmarshal(&tdata)) - require.Equal(t, []string{"test1", "test2"}, tdata) + require.Equal(t, []string{"test1", "test2 data", "test3"}, tdata) bval, berr := prov.Value(ctx, testBool...) require.NoError(t, berr) @@ -51,7 +53,23 @@ func TestProcessor(t *testing.T) { var idata []int require.NoError(t, ival.Unmarshal(&idata)) - require.Equal(t, []int{42, 0, 1}, idata) + require.Equal(t, []int{-42, 0, 42}, idata) + + tival, tierr := prov.Value(ctx, testTime...) + require.NoError(t, tierr) + + var tidata []time.Time + + require.NoError(t, tival.Unmarshal(&tidata)) + require.Equal(t, []time.Time{time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC)}, tidata) + + uval, uerr := prov.Value(ctx, testUint64...) + require.NoError(t, uerr) + + var udata []uint64 + + require.NoError(t, uval.Unmarshal(&udata)) + require.Equal(t, []uint64{42}, udata) } func testVariables(t *testing.T) config.Variables { @@ -60,7 +78,7 @@ func testVariables(t *testing.T) config.Variables { vars := config.NewVars( option.String("test", "test", option.Slice, - option.Default("test1,test2"), + option.Default("test1,\"test2 data\",test3"), handler.Process(config.ProcessFunc(csv.Csv)), ), group.New("group", "group", @@ -68,18 +86,24 @@ func testVariables(t *testing.T) config.Variables { option.Bool("bool", "bool", option.Slice, option.Default("true|false|true"), - handler.Process(config.ProcessFunc(func(ctx context.Context, in config.Value, _ ...param.Option) (config.Value, error) { - return csv.Csv(ctx, in, csv.WithBool, csv.WithDelimiter('|')) - })), + handler.FormatFn(csv.Csv, csv.WithBool, csv.WithDelimiter('|')), ), ), option.Int("int", "int", option.Slice, - option.Default("42,0,1"), - handler.Process(config.ProcessFunc(func(ctx context.Context, in config.Value, _ ...param.Option) (config.Value, error) { - return csv.Csv(ctx, in, csv.WithInt) - })), + option.Default("-42,0,42"), + handler.FormatFn(csv.Csv, csv.WithInt), ), + option.Time("time", "time", + option.Slice, + option.Default("2006-01-02T15:04:05Z"), + handler.FormatFn(csv.Csv, csv.WithTime), + ), + ), + option.Uint64("uint64", "uint64", + option.Slice, + option.Default("42"), + handler.FormatFn(csv.Csv, csv.WithUint64), ), )