strJoin() now supports linked list and added strJoin(sep, linked-list) test
This commit is contained in:
+40
-4
@@ -12,6 +12,8 @@ import (
|
|||||||
"git.portale-stac.it/go-pkg/expr/kern"
|
"git.portale-stac.it/go-pkg/expr/kern"
|
||||||
"git.portale-stac.it/go-pkg/expr/types"
|
"git.portale-stac.it/go-pkg/expr/types"
|
||||||
"git.portale-stac.it/go-pkg/expr/types/array"
|
"git.portale-stac.it/go-pkg/expr/types/array"
|
||||||
|
"git.portale-stac.it/go-pkg/expr/types/dict"
|
||||||
|
"git.portale-stac.it/go-pkg/expr/types/list"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -45,14 +47,24 @@ func joinStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result
|
|||||||
if v, exists := args[kern.ParamItem]; exists {
|
if v, exists := args[kern.ParamItem]; exists {
|
||||||
argv := v.([]any)
|
argv := v.([]any)
|
||||||
if len(argv) == 1 {
|
if len(argv) == 1 {
|
||||||
if a, ok := argv[0].(*array.ArrayType); ok {
|
var it kern.Iterator
|
||||||
result, err = doJoinStr(name, sep, NewListIterator(a, nil))
|
if it, ok = argv[0].(kern.Iterator); !ok {
|
||||||
} else if it, ok := argv[0].(kern.Iterator); ok {
|
if a, ok := argv[0].(*array.ArrayType); ok {
|
||||||
|
it = NewArrayIteratorFromArray(a, nil)
|
||||||
|
} else if l, ok := argv[0].(*list.LinkedList); ok {
|
||||||
|
it = NewLinkedListIterator(l, nil)
|
||||||
|
} else if d, ok := argv[0].(*dict.DictType); ok {
|
||||||
|
if it, err = NewDictIteratorA(d, "none", "values"); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if it != nil {
|
||||||
result, err = doJoinStr(name, sep, it)
|
result, err = doJoinStr(name, sep, it)
|
||||||
} else if s, ok := argv[0].(string); ok {
|
} else if s, ok := argv[0].(string); ok {
|
||||||
result = s
|
result = s
|
||||||
} else {
|
} else {
|
||||||
err = kern.ErrInvalidParameterValue(name, kern.ParamItem, v)
|
err = kern.ErrInvalidParameterValue(name, kern.ParamItem, argv[0])
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result, err = doJoinStr(name, sep, NewArrayIterator(argv))
|
result, err = doJoinStr(name, sep, NewArrayIterator(argv))
|
||||||
@@ -64,6 +76,30 @@ func joinStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func joinStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||||
|
// if sep, ok := args[kern.ParamSeparator].(string); ok {
|
||||||
|
// if v, exists := args[kern.ParamItem]; exists {
|
||||||
|
// argv := v.([]any)
|
||||||
|
// if len(argv) == 1 {
|
||||||
|
// if a, ok := argv[0].(*array.ArrayType); ok {
|
||||||
|
// result, err = doJoinStr(name, sep, NewListIterator(a, nil))
|
||||||
|
// } else if it, ok := argv[0].(kern.Iterator); ok {
|
||||||
|
// result, err = doJoinStr(name, sep, it)
|
||||||
|
// } else if s, ok := argv[0].(string); ok {
|
||||||
|
// result = s
|
||||||
|
// } else {
|
||||||
|
// err = kern.ErrInvalidParameterValue(name, kern.ParamItem, argv[0])
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// result, err = doJoinStr(name, sep, NewArrayIterator(argv))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// err = kern.ErrWrongParamType(name, kern.ParamSeparator, kern.TypeString, args[kern.ParamSeparator])
|
||||||
|
// }
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
func subStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
func subStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||||
var start = 0
|
var start = 0
|
||||||
var count = -1
|
var count = -1
|
||||||
|
|||||||
+4
-2
@@ -35,7 +35,9 @@ func TestExpr(t *testing.T) {
|
|||||||
/* 19 */ {`a=2; ${a}`, int64(2), nil},
|
/* 19 */ {`a=2; ${a}`, int64(2), nil},
|
||||||
/* 20 */ {`$_=2; $_`, int64(2), nil},
|
/* 20 */ {`$_=2; $_`, int64(2), nil},
|
||||||
/* 21 */ {`$$`, dict.NewDict(map[any]any{"vars": dict.NewDict(nil), "funcs": dict.NewDict(nil)}), nil},
|
/* 21 */ {`$$`, dict.NewDict(map[any]any{"vars": dict.NewDict(nil), "funcs": dict.NewDict(nil)}), nil},
|
||||||
/* 22 */ {`
|
/* 22 */ {`builtin "string"; strJoin("-", [<"a", "b", "c">])`, nil, `strJoin(): invalid value [<"a", "b", "c">] (linked-list) for parameter "item"`},
|
||||||
|
/* 22 */ {`builtin ""`, nil, `strJoin(): invalid value [<"a", "b", "c">] (linked-list) for parameter "item"`},
|
||||||
|
/* 23 */ {`
|
||||||
ds={
|
ds={
|
||||||
"init":func(@end){@current=0 but true},
|
"init":func(@end){@current=0 but true},
|
||||||
//"current":func(){current},
|
//"current":func(){current},
|
||||||
@@ -50,6 +52,6 @@ func TestExpr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
// t.Setenv("EXPR_PATH", ".")
|
// t.Setenv("EXPR_PATH", ".")
|
||||||
|
|
||||||
// RunTestSuiteSpec(t, section, inputs, 19)
|
// RunTestSuiteSpec(t, section, inputs, 22)
|
||||||
RunTestSuite(t, section, inputs)
|
RunTestSuite(t, section, inputs)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user