expander-base-funcs.go -- Added two functions: __map, and __mapNC
This commit is contained in:
parent
b1fdc7d511
commit
ab722abe2d
@ -35,6 +35,9 @@ func TestNewDictSetContext(t *testing.T) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
yday := now.Add(-24 * time.Hour)
|
yday := now.Add(-24 * time.Hour)
|
||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
|
{ctx1, "${|__default two|__map : 'one:1' 'two:2' 'six:6'}", "2", nil},
|
||||||
|
{ctx1, "${|__default two|__map : 'one:1' 'TWO:2' 'six:6'}", "", nil},
|
||||||
|
{ctx1, "${|__default two|__mapNC : 'one:1' 'TWO:2' 'six:6'}", "2", nil},
|
||||||
{ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil},
|
{ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil},
|
||||||
{ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil},
|
{ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil},
|
||||||
{ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil},
|
{ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil},
|
||||||
|
@ -84,6 +84,35 @@ func ExFuncTrimSuffix(ctx ExpanderContext, varValue string, args []string) (resu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExFuncMap(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
|
||||||
|
sep := args[0]
|
||||||
|
for _, pairSpec := range args[1:] {
|
||||||
|
pair := strings.SplitN(pairSpec, sep, 2)
|
||||||
|
if pair[0] == varValue {
|
||||||
|
if len(pair) == 2 {
|
||||||
|
result = pair[1]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExFuncMapNC(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
|
||||||
|
sep := args[0]
|
||||||
|
varValue = strings.ToLower(varValue)
|
||||||
|
for _, pairSpec := range args[1:] {
|
||||||
|
pair := strings.SplitN(pairSpec, sep, 2)
|
||||||
|
if strings.ToLower(pair[0]) == varValue {
|
||||||
|
if len(pair) == 2 {
|
||||||
|
result = pair[1]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
||||||
ctx.AddFunc("__default", ExFuncDefault, 0, -1, "Returns the flow value if not empty, else all args joined; first arg is used as separator.")
|
ctx.AddFunc("__default", ExFuncDefault, 0, -1, "Returns the flow value if not empty, else all args joined; first arg is used as separator.")
|
||||||
ctx.AddFunc("__get", ExFuncGet, 0, 2, "Returns the value of the specified variable (arg1). With no arguments it returns the value of the variable having the flow value as name (same as __get ${.}). Arg2 is the default value. ")
|
ctx.AddFunc("__get", ExFuncGet, 0, 2, "Returns the value of the specified variable (arg1). With no arguments it returns the value of the variable having the flow value as name (same as __get ${.}). Arg2 is the default value. ")
|
||||||
@ -92,5 +121,7 @@ func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
|||||||
ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.")
|
ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.")
|
||||||
ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.")
|
ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.")
|
||||||
ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.")
|
ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.")
|
||||||
|
ctx.AddFunc("__map", ExFuncMap, 1, 1, "Returns the value mapped by the flow value as a key; e.g. two __map : 'one:1' 'two:2' 'six:6' returns 2")
|
||||||
|
ctx.AddFunc("__mapNC", ExFuncMapNC, 1, 1, "Same as __map but this comapares keys ignoring case differencies; e.g. two __map : 'ONE:1' 'Two:2' 'sIX:6' returns 2")
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user