provider add format
All checks were successful
Go Action / goaction (pull_request) Successful in 57s
All checks were successful
Go Action / goaction (pull_request) Successful in 57s
This commit is contained in:
@@ -16,6 +16,12 @@ const (
|
|||||||
processorKey pkey = iota + 1
|
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 {
|
func Process(fn config.Processor) param.Option {
|
||||||
return func(p param.Params) param.Params {
|
return func(p param.Params) param.Params {
|
||||||
return param.With(p, processorKey, fn)
|
return param.With(p, processorKey, fn)
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ package handler_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/config"
|
"gitoa.ru/go-4devs/config"
|
||||||
"gitoa.ru/go-4devs/config/definition/group"
|
"gitoa.ru/go-4devs/config/definition/group"
|
||||||
"gitoa.ru/go-4devs/config/definition/option"
|
"gitoa.ru/go-4devs/config/definition/option"
|
||||||
"gitoa.ru/go-4devs/config/definition/proto"
|
"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/processor/csv"
|
||||||
"gitoa.ru/go-4devs/config/provider/handler"
|
"gitoa.ru/go-4devs/config/provider/handler"
|
||||||
"gitoa.ru/go-4devs/config/provider/memory"
|
"gitoa.ru/go-4devs/config/provider/memory"
|
||||||
@@ -16,9 +16,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testKey = []string{"test"}
|
testKey = []string{"test"}
|
||||||
testBool = []string{"group", "service", "bool"}
|
testBool = []string{"group", "service", "bool"}
|
||||||
testInt = []string{"group", "int"}
|
testInt = []string{"group", "int"}
|
||||||
|
testTime = []string{"group", "time"}
|
||||||
|
testUint64 = []string{"uint64"}
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcessor(t *testing.T) {
|
func TestProcessor(t *testing.T) {
|
||||||
@@ -35,7 +37,7 @@ func TestProcessor(t *testing.T) {
|
|||||||
var tdata []string
|
var tdata []string
|
||||||
|
|
||||||
require.NoError(t, tval.Unmarshal(&tdata))
|
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...)
|
bval, berr := prov.Value(ctx, testBool...)
|
||||||
require.NoError(t, berr)
|
require.NoError(t, berr)
|
||||||
@@ -51,7 +53,23 @@ func TestProcessor(t *testing.T) {
|
|||||||
var idata []int
|
var idata []int
|
||||||
|
|
||||||
require.NoError(t, ival.Unmarshal(&idata))
|
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 {
|
func testVariables(t *testing.T) config.Variables {
|
||||||
@@ -60,7 +78,7 @@ func testVariables(t *testing.T) config.Variables {
|
|||||||
vars := config.NewVars(
|
vars := config.NewVars(
|
||||||
option.String("test", "test",
|
option.String("test", "test",
|
||||||
option.Slice,
|
option.Slice,
|
||||||
option.Default("test1,test2"),
|
option.Default("test1,\"test2 data\",test3"),
|
||||||
handler.Process(config.ProcessFunc(csv.Csv)),
|
handler.Process(config.ProcessFunc(csv.Csv)),
|
||||||
),
|
),
|
||||||
group.New("group", "group",
|
group.New("group", "group",
|
||||||
@@ -68,18 +86,24 @@ func testVariables(t *testing.T) config.Variables {
|
|||||||
option.Bool("bool", "bool",
|
option.Bool("bool", "bool",
|
||||||
option.Slice,
|
option.Slice,
|
||||||
option.Default("true|false|true"),
|
option.Default("true|false|true"),
|
||||||
handler.Process(config.ProcessFunc(func(ctx context.Context, in config.Value, _ ...param.Option) (config.Value, error) {
|
handler.FormatFn(csv.Csv, csv.WithBool, csv.WithDelimiter('|')),
|
||||||
return csv.Csv(ctx, in, csv.WithBool, csv.WithDelimiter('|'))
|
|
||||||
})),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
option.Int("int", "int",
|
option.Int("int", "int",
|
||||||
option.Slice,
|
option.Slice,
|
||||||
option.Default("42,0,1"),
|
option.Default("-42,0,42"),
|
||||||
handler.Process(config.ProcessFunc(func(ctx context.Context, in config.Value, _ ...param.Option) (config.Value, error) {
|
handler.FormatFn(csv.Csv, csv.WithInt),
|
||||||
return csv.Csv(ctx, in, 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),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user