The name of 'list' has been changed to 'array'; from now on, 'list' will refer to the linked list.

This commit is contained in:
2026-07-12 07:35:28 +02:00
parent b6da9bcad4
commit 77f36642b4
37 changed files with 180 additions and 196 deletions
+3 -3
View File
@@ -15,7 +15,7 @@ import (
)
type ListIterator struct {
a *array.ListType
a *array.ArrayType
count int64
index int64
start int64
@@ -23,7 +23,7 @@ type ListIterator struct {
step int64
}
func NewListIterator(list *array.ListType, args []any) (it *ListIterator) {
func NewListIterator(list *array.ArrayType, args []any) (it *ListIterator) {
var argc int = 0
listLen := int64(len(([]any)(*list)))
if args != nil {
@@ -63,7 +63,7 @@ func NewListIterator(list *array.ListType, args []any) (it *ListIterator) {
}
func NewArrayIterator(a []any) (it *ListIterator) {
it = &ListIterator{a: (*array.ListType)(&a), count: 0, index: -1, start: 0, stop: int64(len(a)) - 1, step: 1}
it = &ListIterator{a: (*array.ArrayType)(&a), count: 0, index: -1, start: 0, stop: int64(len(a)) - 1, step: 1}
return
}
+1 -1
View File
@@ -83,7 +83,7 @@ func boolFunc(ctx kern.ExprContext, name string, args map[string]any) (result an
result = v
case string:
result = len(v) > 0
case *array.ListType:
case *array.ArrayType:
result = len(*v) > 0
case *dict.DictType:
result = len(*v) > 0
+2 -2
View File
@@ -33,7 +33,7 @@ func doAdd(ctx kern.ExprContext, name string, it kern.Iterator, count, level int
level++
for v, err = it.Next(); err == nil; v, err = it.Next() {
if list, ok := v.(*array.ListType); ok {
if list, ok := v.(*array.ArrayType); ok {
v = NewListIterator(list, nil)
}
if subIter, ok := v.(kern.Iterator); ok {
@@ -107,7 +107,7 @@ func doMul(ctx kern.ExprContext, name string, it kern.Iterator, count, level int
level++
for v, err = it.Next(); err == nil; v, err = it.Next() {
if list, ok := v.(*array.ListType); ok {
if list, ok := v.(*array.ArrayType); ok {
v = NewListIterator(list, nil)
}
if subIter, ok := v.(kern.Iterator); ok {
+2 -2
View File
@@ -45,7 +45,7 @@ func joinStrFunc(ctx kern.ExprContext, name string, args map[string]any) (result
if v, exists := args[kern.ParamItem]; exists {
argv := v.([]any)
if len(argv) == 1 {
if a, ok := argv[0].(*array.ListType); ok {
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)
@@ -196,7 +196,7 @@ func splitStrFunc(ctx kern.ExprContext, name string, args map[string]any) (resul
} else {
parts = []string{}
}
a := make(array.ListType, len(parts))
a := make(array.ArrayType, len(parts))
for i, part := range parts {
a[i] = part
}
+2 -2
View File
@@ -260,7 +260,7 @@ func registerLocalFunctions(ctx kern.ExprContext) {
vars := ctx.EnumVars(func(name string) bool {
return len(name) > 0 && name[0] == '_'
})
result = array.ListFromStrings(vars)
result = array.ArrayFromStrings(vars)
return
}
@@ -312,7 +312,7 @@ func registerLocalFunctions(ctx kern.ExprContext) {
name, _, _ := strings.Cut(e, "=")
vars = append(vars, name)
}
result = array.ListFromStrings(vars)
result = array.ArrayFromStrings(vars)
return
}
+1 -1
View File
@@ -193,7 +193,7 @@ func (it *DictIterator) Current() (item any, err error) {
case dictIterModeItems:
a := *(it.a)
pair := []any{it.keys[it.index], a[it.keys[it.index]]}
item = array.NewList(pair)
item = array.NewArray(pair)
}
} else {
err = io.EOF
+1 -1
View File
@@ -29,7 +29,7 @@ func NewIterator(ctx kern.ExprContext, value any, ops []*scan.Term) (it kern.Ite
}
switch v := value.(type) {
case *array.ListType:
case *array.ArrayType:
it = NewListIterator(v, nil)
case *list.LinkedList:
it = NewLinkedListIterator(v, nil)
+1 -1
View File
@@ -135,7 +135,7 @@ func evalIterator(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
err = opTerm.Children[0].Errorf("the data-source must be a dictionary")
}
}
} else if a, ok := firstChildValue.(*array.ListType); ok {
} else if a, ok := firstChildValue.(*array.ArrayType); ok {
var args []any
if args, err = evalSiblings(ctx, opTerm.Children, nil); err == nil {
v = NewListIterator(a, args)
+1 -1
View File
@@ -29,7 +29,7 @@ func newListTerm(row, col int, args []*scan.Term) *scan.Term {
// -------- list func
func evalList(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
list, _ := opTerm.Value().([]*scan.Term)
items := make(array.ListType, len(list))
items := make(array.ArrayType, len(list))
for i, tree := range list {
var param any
if param, err = tree.Compute(ctx); err != nil {
+6 -6
View File
@@ -46,7 +46,7 @@ func evalDeepCopyAssign(ctx kern.ExprContext, opTerm *scan.Term) (v any, err err
func assignCollectionItem(ctx kern.ExprContext, collectionTerm, keyListTerm *scan.Term, value any) (err error) {
var collectionValue, keyListValue, keyValue any
var keyList *array.ListType
var keyList *array.ArrayType
var ok bool
if collectionValue, err = collectionTerm.Compute(ctx); err != nil {
@@ -55,7 +55,7 @@ func assignCollectionItem(ctx kern.ExprContext, collectionTerm, keyListTerm *sca
if keyListValue, err = keyListTerm.Compute(ctx); err != nil {
return
} else if keyList, ok = keyListValue.(*array.ListType); !ok || len(*keyList) != 1 {
} else if keyList, ok = keyListValue.(*array.ArrayType); !ok || len(*keyList) != 1 {
err = keyListTerm.Errorf("index/key specification expected, got %v [%s]", keyListValue, kern.TypeName(keyListValue))
return
}
@@ -65,7 +65,7 @@ func assignCollectionItem(ctx kern.ExprContext, collectionTerm, keyListTerm *sca
}
switch collection := collectionValue.(type) {
case *array.ListType:
case *array.ArrayType:
if index, ok := keyValue.(int64); ok {
err = collection.SetItem(index, value)
} else {
@@ -180,7 +180,7 @@ func newOpAssignTerm(tk *scan.Token) (inst *scan.Term) {
func getCollectionItemValue(ctx kern.ExprContext, collectionTerm, keyListTerm *scan.Term) (value any, err error) {
var collectionValue, keyListValue, keyValue any
var keyList *array.ListType
var keyList *array.ArrayType
var ok bool
if collectionValue, err = collectionTerm.Compute(ctx); err != nil {
@@ -189,7 +189,7 @@ func getCollectionItemValue(ctx kern.ExprContext, collectionTerm, keyListTerm *s
if keyListValue, err = keyListTerm.Compute(ctx); err != nil {
return
} else if keyList, ok = keyListValue.(*array.ListType); !ok || len(*keyList) != 1 {
} else if keyList, ok = keyListValue.(*array.ArrayType); !ok || len(*keyList) != 1 {
err = keyListTerm.Errorf("index/key specification expected, got %v [%s]", keyListValue, kern.TypeName(keyListValue))
return
}
@@ -199,7 +199,7 @@ func getCollectionItemValue(ctx kern.ExprContext, collectionTerm, keyListTerm *s
}
switch collection := collectionValue.(type) {
case *array.ListType:
case *array.ArrayType:
if index, ok := keyValue.(int64); ok {
value = (*collection)[index]
} else {
+3 -3
View File
@@ -89,12 +89,12 @@ func evalGroupBy(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
sItemKey = strconv.Itoa(int(it.Index()))
}
var ls *array.ListType
var ls *array.ArrayType
if lsAny, exists := values.GetItem(sItemKey); exists && lsAny != nil {
ls = lsAny.(*array.ListType)
ls = lsAny.(*array.ArrayType)
}
if ls == nil {
ls = array.NewListA()
ls = array.NewArrayA()
}
ls.AppendItem(item)
values.SetItem(sItemKey, ls)
+1 -1
View File
@@ -36,7 +36,7 @@ func evalIn(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
}
if array.IsList(rightValue) {
a, _ := rightValue.(*array.ListType)
a, _ := rightValue.(*array.ArrayType)
v = a.IndexDeepSameCmp(leftValue) >= 0
} else if dict.IsDict(rightValue) {
dict, _ := rightValue.(*dict.DictType)
+1 -1
View File
@@ -32,7 +32,7 @@ func evalInclude(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
count := 0
if array.IsList(childValue) {
a, _ := childValue.(*array.ListType)
a, _ := childValue.(*array.ArrayType)
for i, filePathSpec := range *a {
if filePath, ok := filePathSpec.(string); ok {
if v, err = EvalFile(ctx, filePath); err == nil {
+9 -9
View File
@@ -24,12 +24,12 @@ func newIndexTerm(tk *scan.Token) (inst *scan.Term) {
}
}
func verifyKey(indexList *array.ListType) (index any, err error) {
func verifyKey(indexList *array.ArrayType) (index any, err error) {
index = (*indexList)[0]
return
}
func verifyIndex(indexTerm *scan.Term, indexList *array.ListType, maxValue int) (index int, err error) {
func verifyIndex(indexTerm *scan.Term, indexList *array.ArrayType, maxValue int) (index int, err error) {
var v int
if v, err = types.ToGoInt((*indexList)[0], "index expression"); err == nil {
@@ -45,7 +45,7 @@ func verifyIndex(indexTerm *scan.Term, indexList *array.ListType, maxValue int)
return
}
func verifyRange(indexTerm *scan.Term, indexList *array.ListType, maxValue int) (startIndex, endIndex int, err error) {
func verifyRange(indexTerm *scan.Term, indexList *array.ArrayType, maxValue int) (startIndex, endIndex int, err error) {
v, _ := ((*indexList)[0]).(*intPair)
startIndex = v.a
endIndex = v.b
@@ -70,7 +70,7 @@ func verifyRange(indexTerm *scan.Term, indexList *array.ListType, maxValue int)
func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
var leftValue, rightValue any
var indexList *array.ListType
var indexList *array.ArrayType
var ok bool
if leftValue, rightValue, err = opTerm.EvalInfix(ctx); err != nil {
@@ -78,7 +78,7 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
}
indexTerm := opTerm.Children[1]
if indexList, ok = rightValue.(*array.ListType); !ok {
if indexList, ok = rightValue.(*array.ArrayType); !ok {
err = opTerm.Errorf("invalid index expression")
return
} else if len(*indexList) != 1 {
@@ -88,7 +88,7 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
if types.IsInteger((*indexList)[0]) {
switch unboxedValue := leftValue.(type) {
case *array.ListType:
case *array.ArrayType:
var index int
if index, err = verifyIndex(indexTerm, indexList, len(*unboxedValue)); err == nil {
v = (*unboxedValue)[index]
@@ -110,10 +110,10 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
}
} else if isIntPair((*indexList)[0]) {
switch unboxedValue := leftValue.(type) {
case *array.ListType:
case *array.ArrayType:
var start, end int
if start, end, err = verifyRange(indexTerm, indexList, len(*unboxedValue)); err == nil {
sublist := array.ListType((*unboxedValue)[start:end])
sublist := array.ArrayType((*unboxedValue)[start:end])
v = &sublist
}
case *list.LinkedList:
@@ -139,7 +139,7 @@ func evalIndex(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
return
}
func getDictItem(d *dict.DictType, indexTerm *scan.Term, indexList *array.ListType, rightValue any) (v any, err error) {
func getDictItem(d *dict.DictType, indexTerm *scan.Term, indexList *array.ArrayType, rightValue any) (v any, err error) {
var ok bool
var indexValue any
+3 -3
View File
@@ -34,7 +34,7 @@ func newAppendTerm(tk *scan.Token) (inst *scan.Term) {
}
func prependToList(opTerm *scan.Term, leftValue, rightValue any) (result any, err error) {
if a, ok := rightValue.(*array.ListType); ok {
if a, ok := rightValue.(*array.ArrayType); ok {
var it kern.Iterator
if it, ok = leftValue.(kern.Iterator); !ok {
it = NewFormalIterator(leftValue)
@@ -43,7 +43,7 @@ func prependToList(opTerm *scan.Term, leftValue, rightValue any) (result any, er
ls := list.NewLinkedList()
ls.SeqPushBack(it)
newArray := array.ListType(nil)
newArray := array.ArrayType(nil)
if newSize := len(*a) + int(ls.Len()); newSize > cap(*a) {
newArray = make([]any, 0, newSize)
for node := ls.FirstNode(); node != nil; node = node.Next() {
@@ -79,7 +79,7 @@ func evalPrepend(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
}
func appendToList(opTerm *scan.Term, leftValue, rightValue any) (result any, err error) {
if a, ok := leftValue.(*array.ListType); ok {
if a, ok := leftValue.(*array.ArrayType); ok {
var it kern.Iterator
if it, ok = rightValue.(kern.Iterator); !ok {
it = NewFormalIterator(rightValue)
+1 -1
View File
@@ -33,7 +33,7 @@ func evalLength(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
}
if array.IsList(childValue) {
ls, _ := childValue.(*array.ListType)
ls, _ := childValue.(*array.ArrayType)
v = int64(len(*ls))
} else if str.IsString(childValue) {
s, _ := childValue.(string)
+2 -2
View File
@@ -93,7 +93,7 @@ func divValues(opTerm *scan.Term, leftValue, rightValue any) (v any, err error)
} else if str.IsString(leftValue) && str.IsString(rightValue) {
source := leftValue.(string)
sep := rightValue.(string)
v = array.ListFromStrings(strings.Split(source, sep))
v = array.ArrayFromStrings(strings.Split(source, sep))
} else if str.IsString(leftValue) && types.IsInteger(rightValue) {
source := leftValue.(string)
partSize := int(rightValue.(int64))
@@ -113,7 +113,7 @@ func divValues(opTerm *scan.Term, leftValue, rightValue any) (v any, err error)
if remainder > 0 {
parts = append(parts, source[len(source)-remainder:])
}
v = array.NewList(parts)
v = array.NewArray(parts)
}
} else {
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
+2 -2
View File
@@ -120,8 +120,8 @@ func lessThan(self *scan.Term, a, b any) (isLess bool, err error) {
isLess = ls < rs
// Inclusion test
} else if array.IsList(a) && array.IsList(b) {
aList, _ := a.(*array.ListType)
bList, _ := b.(*array.ListType)
aList, _ := a.(*array.ArrayType)
bList, _ := b.(*array.ArrayType)
isLess = len(*aList) < len(*bList) && bList.Contains(aList)
} else {
err = self.ErrIncompatibleTypes(a, b)
+7 -7
View File
@@ -42,11 +42,11 @@ func sumValues(plusTerm *scan.Term, leftValue, rightValue any) (v any, err error
v = leftInt + rightInt
}
} else if array.IsList(leftValue) && array.IsList(rightValue) {
var leftList, rightList *array.ListType
leftList, _ = leftValue.(*array.ListType)
rightList, _ = rightValue.(*array.ListType)
var leftList, rightList *array.ArrayType
leftList, _ = leftValue.(*array.ArrayType)
rightList, _ = rightValue.(*array.ArrayType)
sumList := make(array.ListType, 0, len(*leftList)+len(*rightList))
sumList := make(array.ArrayType, 0, len(*leftList)+len(*rightList))
sumList = append(sumList, *leftList...)
sumList = append(sumList, *rightList...)
v = &sumList
@@ -104,9 +104,9 @@ func diffValues(minusTerm *scan.Term, leftValue, rightValue any) (v any, err err
v = leftInt - rightInt
}
} else if array.IsList(leftValue) && array.IsList(rightValue) {
leftList, _ := leftValue.(*array.ListType)
rightList, _ := rightValue.(*array.ListType)
diffList := make(array.ListType, 0, len(*leftList)-len(*rightList))
leftList, _ := leftValue.(*array.ArrayType)
rightList, _ := rightValue.(*array.ArrayType)
diffList := make(array.ArrayType, 0, len(*leftList)-len(*rightList))
for _, item := range *leftList {
if slices.Index(*rightList, item) < 0 {
diffList = append(diffList, item)
+1 -1
View File
@@ -52,7 +52,7 @@ func evalUnset(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
count := 0
if array.IsList(childValue) {
list, _ := childValue.(*array.ListType)
list, _ := childValue.(*array.ArrayType)
for _, item := range *list {
if deleted, err = deleteContextItem(ctx, opTerm, item); err != nil {
break
+1 -1
View File
@@ -63,7 +63,7 @@ func TestBoolNoShortcut(t *testing.T) {
current := kern.SetCtrl(ctx, kern.ControlBoolShortcut, false)
// runCtxTestSuiteSpec(t, ctx, section, inputs, 1)
runCtxTestSuite(t, ctx, section, inputs)
RunCtxTestSuite(t, ctx, section, inputs)
kern.SetCtrl(ctx, kern.ControlBoolShortcut, current)
}
+1 -1
View File
@@ -41,7 +41,7 @@ func TestFmt(t *testing.T) {
ctx := NewSimpleStore()
currentStdout := kern.SetCtrl(ctx, kern.ControlStdout, &b)
runCtxTestSuite(t, ctx, section, inputs)
RunCtxTestSuite(t, ctx, section, inputs)
kern.SetCtrl(ctx, kern.ControlStdout, currentStdout)
if b.String() != text+"\n" {
+1 -1
View File
@@ -30,7 +30,7 @@ func TestFuncString(t *testing.T) {
/* 14 */ {`builtin "string"; strEndsWith("0123456789", "xyz", "789")`, true, nil},
/* 15 */ {`builtin "string"; strEndsWith("0123456789", "xyz", "0125")`, false, nil},
/* 16 */ {`builtin "string"; strEndsWith("0123456789")`, nil, `strEndsWith(): too few params -- expected 2 or more, got 1`},
/* 17 */ {`builtin "string"; strSplit("one-two-three", "-")`, array.NewListA("one", "two", "three"), nil},
/* 17 */ {`builtin "string"; strSplit("one-two-three", "-")`, array.NewArrayA("one", "two", "three"), nil},
/* 18 */ {`builtin "string"; strJoin("-", [1, "two", "three"])`, nil, `strJoin(): expected string, got integer (1)`},
/* 19 */ {`builtin "string"; strJoin()`, nil, `strJoin(): too few params -- expected 1 or more, got 0`},
/* 20 */ {`builtin "string"; strUpper("StOp")`, "STOP", nil},
+5 -5
View File
@@ -19,7 +19,7 @@ type inputType struct {
wantErr any
}
func runCtxTestSuiteSpec(t *testing.T, ctx kern.ExprContext, section string, inputs []inputType, spec ...int) {
func RunCtxTestSuiteSpec(t *testing.T, ctx kern.ExprContext, section string, inputs []inputType, spec ...int) {
succeeded := 0
failed := 0
for _, count := range spec {
@@ -40,11 +40,11 @@ func runCtxTestSuiteSpec(t *testing.T, ctx kern.ExprContext, section string, inp
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(spec), succeeded, failed)
}
func runTestSuiteSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
runCtxTestSuiteSpec(t, nil, section, inputs, spec...)
func RunTestSuiteSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
RunCtxTestSuiteSpec(t, nil, section, inputs, spec...)
}
func runCtxTestSuite(t *testing.T, ctx kern.ExprContext, section string, inputs []inputType) {
func RunCtxTestSuite(t *testing.T, ctx kern.ExprContext, section string, inputs []inputType) {
succeeded := 0
failed := 0
@@ -62,7 +62,7 @@ func runCtxTestSuite(t *testing.T, ctx kern.ExprContext, section string, inputs
t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(inputs), succeeded, failed)
}
func runTestSuite(t *testing.T, section string, inputs []inputType) {
runCtxTestSuite(t, nil, section, inputs)
RunCtxTestSuite(t, nil, section, inputs)
}
func getWantedError(input *inputType) error {
+1 -1
View File
@@ -71,7 +71,7 @@ func TestAccessSubFields(t *testing.T) {
// /* 4 */ {`D.b[1] = 22; D["b"][1]`, int64(22), nil},
/* 4 */ {`D.b[1] = 22; D["b"][1]`, nil, `[1:3] collection expected`},
}
runCtxTestSuiteSpec(t, ctx, section, inputs, 4)
RunCtxTestSuiteSpec(t, ctx, section, inputs, 4)
// runCtxTestSuite(t, ctx, section, inputs)
}
+1 -1
View File
@@ -102,6 +102,6 @@ func TestGoFunction(t *testing.T) {
kern.NewFuncParamFlagDef("name", kern.PfOptional|kern.PfDefault, "Celestino Amoroso"),
})
runCtxTestSuite(t, ctx, section, inputs)
RunCtxTestSuite(t, ctx, section, inputs)
}
+1 -1
View File
@@ -20,7 +20,7 @@ func TestCollections(t *testing.T) {
/* 5 */ {`"abcdef"[1:2:3]`, nil, `[1:14] invalid range specification`},
/* 6 */ {`"abcdef"[((1>0)?{1}:{0}):3]`, "bc", nil},
/* 7 */ {`"abcdef"[[0,1][0]:1]`, "a", nil},
/* 8 */ {`[0,1,2,3,4][:]`, array.NewListA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 8 */ {`[0,1,2,3,4][:]`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
}
t.Setenv("EXPR_PATH", ".")
+9 -9
View File
@@ -12,7 +12,7 @@ import (
)
func TestNewListIterator(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
if item, err := it.Next(); err != nil {
t.Errorf("error: %v", err)
@@ -24,7 +24,7 @@ func TestNewListIterator(t *testing.T) {
}
func TestNewListIterator2(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{3, 1, -1})
if item, err := it.Next(); err != nil {
t.Errorf("error: %v", err)
@@ -36,7 +36,7 @@ func TestNewListIterator2(t *testing.T) {
}
func TestNewListIterator3(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, -1, 1})
if item, err := it.Next(); err != nil {
t.Errorf("error: %v", err)
@@ -93,7 +93,7 @@ func TestNewIterList5(t *testing.T) {
func TestNewIterList6(t *testing.T) {
ctx := NewSimpleStore()
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it1, _ := NewIterator(ctx, a, nil)
it, _ := NewIterator(ctx, it1, nil)
if item, err := it.Next(); err != nil {
@@ -115,7 +115,7 @@ func TestNewString(t *testing.T) {
func TestHasOperation(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
hasOp := it.HasOperation("reset")
if !hasOp {
@@ -125,7 +125,7 @@ func TestHasOperation(t *testing.T) {
func TestCallOperationReset(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
if v, err := it.CallOperation("reset", nil); err != nil {
t.Errorf("Error on CallOperation(reset): %v", err)
@@ -136,7 +136,7 @@ func TestCallOperationReset(t *testing.T) {
func TestCallOperationIndex(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
if v, err := it.CallOperation("index", nil); err != nil {
t.Errorf("Error on CallOperation(index): %v", err)
@@ -147,7 +147,7 @@ func TestCallOperationIndex(t *testing.T) {
func TestCallOperationCount(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
if v, err := it.CallOperation("count", nil); err != nil {
t.Errorf("Error on CallOperation(count): %v", err)
@@ -158,7 +158,7 @@ func TestCallOperationCount(t *testing.T) {
func TestCallOperationUnknown(t *testing.T) {
a := array.NewListA("a", "b", "c", "d")
a := array.NewArrayA("a", "b", "c", "d")
it := NewListIterator(a, []any{1, 3, 1})
if v, err := it.CallOperation("unknown", nil); err == nil {
t.Errorf("Expected error on CallOperation(unknown), got %v", v)
+1 -1
View File
@@ -38,7 +38,7 @@ func TestIteratorParser(t *testing.T) {
/* 19 */ {`it=$({1:"one",2:"two",3:"three"}); it++`, int64(1), nil},
/* 20 */ {`it=$({1:"one",2:"two",3:"three"}, "default", "value"); it++`, "one", nil},
/* 21 */ {`it=$({1:"one",2:"two",3:"three"}, "desc", "key"); it++`, int64(3), nil},
/* 22 */ {`it=$({1:"one",2:"two",3:"three"}, "asc", "item"); it++`, array.NewList([]any{int64(1), "one"}), nil},
/* 22 */ {`it=$({1:"one",2:"two",3:"three"}, "asc", "item"); it++`, array.NewArray([]any{int64(1), "one"}), nil},
/* 23 */ {`$$($(1,4,0))`, nil, `step cannot be zero`},
/* 24 */ {`$$($(1,4,-1))`, nil, `step cannot be negative when start < stop`},
/* 25 */ {`$$($(4,1,1))`, nil, `step cannot be positive when start > stop`},
+23 -23
View File
@@ -15,49 +15,49 @@ func TestListParser(t *testing.T) {
section := "List"
inputs := []inputType{
/* 1 */ {`[]`, array.NewListA(), nil},
/* 2 */ {`[1,2,3]`, array.NewListA(int64(1), int64(2), int64(3)), nil},
/* 3 */ {`[1,2,"hello"]`, array.NewListA(int64(1), int64(2), "hello"), nil},
/* 4 */ {`[1+2, not true, "hello"]`, array.NewListA(int64(3), false, "hello"), nil},
/* 5 */ {`[1,2]+[3]`, array.NewListA(int64(1), int64(2), int64(3)), nil},
/* 6 */ {`[1,4,3,2]-[3]`, array.NewListA(int64(1), int64(4), int64(2)), nil},
/* 1 */ {`[]`, array.NewArrayA(), nil},
/* 2 */ {`[1,2,3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
/* 3 */ {`[1,2,"hello"]`, array.NewArrayA(int64(1), int64(2), "hello"), nil},
/* 4 */ {`[1+2, not true, "hello"]`, array.NewArrayA(int64(3), false, "hello"), nil},
/* 5 */ {`[1,2]+[3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
/* 6 */ {`[1,4,3,2]-[3]`, array.NewArrayA(int64(1), int64(4), int64(2)), nil},
/* 7 */ {`builtin "math.arith"; add([1,4,3,2])`, int64(10), nil},
/* 8 */ {`builtin "math.arith"; add([1,[2,2],3,2])`, int64(10), nil},
/* 9 */ {`builtin "math.arith"; mul([1,4,3.0,2])`, float64(24.0), nil},
/* 10 */ {`builtin "math.arith"; add([1,"hello"])`, nil, `add(): param nr 2 (2 in 1) has wrong type string, number expected`},
/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil},
/* 12 */ {`[1,2,3] <+ 2+2`, array.NewListA(int64(1), int64(2), int64(3), int64(4)), nil},
/* 13 */ {`2-1 +> [2,3]`, array.NewListA(int64(1), int64(2), int64(3)), nil},
/* 12 */ {`[1,2,3] <+ 2+2`, array.NewArrayA(int64(1), int64(2), int64(3), int64(4)), nil},
/* 13 */ {`2-1 +> [2,3]`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
/* 14 */ {`[1,2,3][1]`, int64(2), nil},
/* 15 */ {`ls=[1,2,3] but ls[1]`, int64(2), nil},
/* 16 */ {`ls=[1,2,3] but ls[-1]`, int64(3), nil},
/* 17 */ {`list=["one","two","three"]; list[10]`, nil, `[1:34] index 10 out of bounds`},
/* 18 */ {`["a", "b", "c"]`, array.NewListA("a", "b", "c"), nil},
/* 19 */ {`["a", "b", "c"]`, array.NewList([]any{"a", "b", "c"}), nil},
/* 18 */ {`["a", "b", "c"]`, array.NewArrayA("a", "b", "c"), nil},
/* 19 */ {`["a", "b", "c"]`, array.NewArray([]any{"a", "b", "c"}), nil},
/* 20 */ {`#["a", "b", "c"]`, int64(3), nil},
/* 21 */ {`"b" in ["a", "b", "c"]`, true, nil},
/* 22 */ {`a=[1,2]; (a)<+3`, array.NewListA(int64(1), int64(2), int64(3)), nil},
/* 23 */ {`a=[1,2]; (a)<+3; a`, array.NewListA(int64(1), int64(2)), nil},
/* 22 */ {`a=[1,2]; (a)<+3`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
/* 23 */ {`a=[1,2]; (a)<+3; a`, array.NewArrayA(int64(1), int64(2)), nil},
/* 24 */ {`["a","b","c","d"][1]`, "b", nil},
/* 25 */ {`["a","b","c","d"][1,1]`, nil, `[1:19] one index only is allowed`},
/* 26 */ {`[0,1,2,3,4][:]`, array.NewListA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 26 */ {`[0,1,2,3,4][:]`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 27 */ {`["a", "b", "c"] <+ ;`, nil, `[1:18] infix operator "<+" requires two non-nil operands, got 1`},
/* 28 */ {`2 << 3;`, int64(16), nil},
/* 29 */ {`but +> ["a", "b", "c"]`, nil, `[1:6] infix operator "+>" requires two non-nil operands, got 0`},
/* 30 */ {`2 >> 3;`, int64(0), nil},
/* 31 */ {`a=[1,2]; a<+3`, array.NewListA(int64(1), int64(2), int64(3)), nil},
/* 32 */ {`a=[1,2]; 5+>a`, array.NewListA(int64(5), int64(1), int64(2)), nil},
/* 33 */ {`L=[1,2]; L[0]=9; L`, array.NewListA(int64(9), int64(2)), nil},
/* 31 */ {`a=[1,2]; a<+3`, array.NewArrayA(int64(1), int64(2), int64(3)), nil},
/* 32 */ {`a=[1,2]; 5+>a`, array.NewArrayA(int64(5), int64(1), int64(2)), nil},
/* 33 */ {`L=[1,2]; L[0]=9; L`, array.NewArrayA(int64(9), int64(2)), nil},
/* 34 */ {`L=[1,2]; L[5]=9; L`, nil, `index 5 out of bounds (0, 1)`},
/* 35 */ {`L=[1,2]; L[]=9; L`, nil, `[1:12] index/key specification expected, got [] [array]`},
/* 36 */ {`L=[1,2]; L[nil]=9;`, nil, `[1:12] index/key is nil`},
/* 37 */ {`[0,1,2,3,4][2:3]`, array.NewListA(int64(2)), nil},
/* 38 */ {`[0,1,2,3,4][3:-1]`, array.NewListA(int64(3)), nil},
/* 30 */ {`[0,1,2,3,4][-3:-1]`, array.NewListA(int64(2), int64(3)), nil},
/* 40 */ {`[0,1,2,3,4][0:]`, array.NewListA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 41 */ {`[0] << $([1,2,3,4])`, array.NewListA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 42 */ {`L=[]; [1] >> L; L`, array.NewListA(), nil},
/* 43 */ {`L=[]; L << [1]; L`, array.NewListA(), nil},
/* 37 */ {`[0,1,2,3,4][2:3]`, array.NewArrayA(int64(2)), nil},
/* 38 */ {`[0,1,2,3,4][3:-1]`, array.NewArrayA(int64(3)), nil},
/* 30 */ {`[0,1,2,3,4][-3:-1]`, array.NewArrayA(int64(2), int64(3)), nil},
/* 40 */ {`[0,1,2,3,4][0:]`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 41 */ {`[0] << $([1,2,3,4])`, array.NewArrayA(int64(0), int64(1), int64(2), int64(3), int64(4)), nil},
/* 42 */ {`L=[]; [1] >> L; L`, array.NewArrayA(), nil},
/* 43 */ {`L=[]; L << [1]; L`, array.NewArrayA(), nil},
// /* 44 */ {`[0,1,2,3,4][2:3]`, kern.NewListA(int64(20)), nil},
}
+22 -22
View File
@@ -54,23 +54,23 @@ func TestOperator(t *testing.T) {
func TestOperatorInsert(t *testing.T) {
section := "Operator-Insert"
inputs := []inputType{
/* 1 */ {`["a", "b"] << nil`, array.NewListA("a", "b"), nil},
/* 2 */ {`["a", "b"] << []`, array.NewListA("a", "b", array.NewListA()), nil},
/* 3 */ {`["a", "b"] << $([])`, array.NewListA("a", "b"), nil},
/* 4 */ {`["a", "b"] << 3`, array.NewListA("a", "b", int64(3)), nil},
/* 1 */ {`["a", "b"] << nil`, array.NewArrayA("a", "b"), nil},
/* 2 */ {`["a", "b"] << []`, array.NewArrayA("a", "b", array.NewArrayA()), nil},
/* 3 */ {`["a", "b"] << $([])`, array.NewArrayA("a", "b"), nil},
/* 4 */ {`["a", "b"] << 3`, array.NewArrayA("a", "b", int64(3)), nil},
/* 5 */ {`3 << ["a", "b"]`, nil, `[1:5] left operand '3' [integer] and right operand '["a", "b"]' [array] are not compatible with operator "<<"`},
/* 6 */ {`nil >> ["a", "b"]`, array.NewListA("a", "b"), nil},
/* 7 */ {`[] >> ["a", "b"]`, array.NewListA(array.NewListA(), "a", "b"), nil},
/* 8 */ {`$([]) >> ["a", "b"]`, array.NewListA("a", "b"), nil},
/* 9 */ {`["a", "b"] << $([1,2,3])`, array.NewListA("a", "b", int64(1), int64(2), int64(3)), nil},
/* 10 */ {`L=["a", "b"]; L << $([1,2,3])`, array.NewListA("a", "b", int64(1), int64(2), int64(3)), nil},
/* 11 */ {`L=["a", "b"]; L << $([1,2,3]); L`, array.NewListA("a", "b"), nil},
/* 6 */ {`nil >> ["a", "b"]`, array.NewArrayA("a", "b"), nil},
/* 7 */ {`[] >> ["a", "b"]`, array.NewArrayA(array.NewArrayA(), "a", "b"), nil},
/* 8 */ {`$([]) >> ["a", "b"]`, array.NewArrayA("a", "b"), nil},
/* 9 */ {`["a", "b"] << $([1,2,3])`, array.NewArrayA("a", "b", int64(1), int64(2), int64(3)), nil},
/* 10 */ {`L=["a", "b"]; L << $([1,2,3])`, array.NewArrayA("a", "b", int64(1), int64(2), int64(3)), nil},
/* 11 */ {`L=["a", "b"]; L << $([1,2,3]); L`, array.NewArrayA("a", "b"), nil},
/* 12 */ {`L << $([1,2,3])`, nil, `undefined variable or function "L"`},
/* 13 */ {`[<>] << 1`, list.NewLinkedListA(1), nil},
/* 14 */ {`[<>] << [1,2]`, list.NewLinkedListA(array.NewListA(int64(1), int64(2))), nil},
/* 14 */ {`[<>] << [1,2]`, list.NewLinkedListA(array.NewArrayA(int64(1), int64(2))), nil},
/* 15 */ {`[<>] << [<1,2>]`, list.NewLinkedListA(list.NewLinkedListA(1, 2)), nil},
/* 16 */ {`1 >> [<>]`, list.NewLinkedListA(1), nil},
/* 17 */ {`[1,2] >> [<>]`, list.NewLinkedListA(array.NewListA(int64(1), int64(2))), nil},
/* 17 */ {`[1,2] >> [<>]`, list.NewLinkedListA(array.NewArrayA(int64(1), int64(2))), nil},
/* 18 */ {`[<1,2>] >> [<>]`, list.NewLinkedListA(list.NewLinkedListA(1, 2)), nil},
/* 19 */ {`$([1,2]) >> [<>]`, list.NewLinkedListA(1, 2), nil},
/* 20 */ {`$([<1,2>]) >> [<>]`, list.NewLinkedListA(1, 2), nil},
@@ -85,8 +85,8 @@ func TestOperatorDeepAssign(t *testing.T) {
inputs := []inputType{
/* 1 */ {`DD={"uno": 1, "due": 2}; D=DD; D["uno"]=11; DD`, dict.NewDict(map[any]any{"uno": int64(11), "due": int64(2)}), nil},
/* 2 */ {`DD={"uno": 1, "due": 2}; D:=DD; D["uno"]=11; DD`, dict.NewDict(map[any]any{"uno": int64(1), "due": int64(2)}), nil},
/* 3 */ {`LL=["a", "b"]; L=LL; L[0]="x"; LL`, array.NewListA("x", "b"), nil},
/* 4 */ {`LL=["a", "b"]; L:=LL; L[0]="x"; LL`, array.NewListA("a", "b"), nil},
/* 3 */ {`LL=["a", "b"]; L=LL; L[0]="x"; LL`, array.NewArrayA("x", "b"), nil},
/* 4 */ {`LL=["a", "b"]; L:=LL; L[0]="x"; LL`, array.NewArrayA("a", "b"), nil},
}
// runTestSuiteSpec(t, section, inputs, 4)
@@ -108,24 +108,24 @@ func TestOperatorGroupBy(t *testing.T) {
inputs := []inputType{
/* 1 */ {`L=[{"num": 1, "alpha": "one"}, {"num": 2, "alpha": "two"}, {"num": 3, "alpha": "three"}]; L groupby "num"`,
dict.NewDict(map[any]any{
"1": array.NewListA(dict.NewDict(map[any]any{"num": int64(1), "alpha": "one"})),
"2": array.NewListA(dict.NewDict(map[any]any{"num": int64(2), "alpha": "two"})),
"3": array.NewListA(dict.NewDict(map[any]any{"num": int64(3), "alpha": "three"})),
"1": array.NewArrayA(dict.NewDict(map[any]any{"num": int64(1), "alpha": "one"})),
"2": array.NewArrayA(dict.NewDict(map[any]any{"num": int64(2), "alpha": "two"})),
"3": array.NewArrayA(dict.NewDict(map[any]any{"num": int64(3), "alpha": "three"})),
}),
nil},
/* 2 */ {`cars = [{"model": "compas", "vendor": "jeep"}, {"model": "limited", "vendor": "jeep"}, {"model": "600", "vendor":"fiat"}]; cars groupby "vendor"`,
dict.NewDict(map[any]any{
"jeep": array.NewListA(
"jeep": array.NewArrayA(
dict.NewDict(map[any]any{"model": "compas", "vendor": "jeep"}),
dict.NewDict(map[any]any{"model": "limited", "vendor": "jeep"})),
"fiat": array.NewListA(dict.NewDict(map[any]any{"model": "600", "vendor": "fiat"})),
"fiat": array.NewArrayA(dict.NewDict(map[any]any{"model": "600", "vendor": "fiat"})),
}),
nil},
/* 3 */ {`[3,4,5] groupby $__`,
dict.NewDict(map[any]any{
"0": array.NewListA(int64(3)),
"1": array.NewListA(int64(4)),
"2": array.NewListA(int64(5)),
"0": array.NewArrayA(int64(3)),
"1": array.NewArrayA(int64(4)),
"2": array.NewArrayA(int64(5)),
}),
nil},
}
+2 -2
View File
@@ -19,9 +19,9 @@ func TestStringsParser(t *testing.T) {
/* 4 */ {`"uno" * (2+1)`, `unounouno`, nil},
/* 5 */ {`"abc"[1]`, `b`, nil},
/* 6 */ {`#"abc"`, int64(3), nil},
/* 7 */ {`"192.168.0.240" / "."`, array.NewListA("192", "168", "0", "240"), nil},
/* 7 */ {`"192.168.0.240" / "."`, array.NewArrayA("192", "168", "0", "240"), nil},
/* 8 */ {`("192.168.0.240" / ".")[1]`, "168", nil},
/* 9 */ {`"AF3B0Dz" / 2`, array.NewListA("AF", "3B", "0D", "z"), nil},
/* 9 */ {`"AF3B0Dz" / 2`, array.NewArrayA("AF", "3B", "0D", "z"), nil},
/* 10 */ {`"AF3B0Dz" / 0`, nil, "[1:12] division by zero"},
}
+8 -28
View File
@@ -18,34 +18,24 @@ import (
func TestExpandPathRootOk(t *testing.T) {
source := "~root"
wantValue := "/root"
// wantErr := errors.New(`test expected string, got list ([])`)
wantErr := error(nil)
gotValue, gotErr := util.ExpandPath(source)
if gotErr != nil && gotErr.Error() != wantErr.Error() {
if gotErr != nil || gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
} else if gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
source, gotValue, gotErr, wantValue, nil)
}
}
func TestExpandPathRootSubDirOk(t *testing.T) {
source := "~root/test"
wantValue := "/root/test"
// wantErr := errors.New(`test expected string, got list ([])`)
wantErr := error(nil)
gotValue, gotErr := util.ExpandPath(source)
if gotErr != nil && gotErr.Error() != wantErr.Error() {
if gotErr != nil || gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
} else if gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
source, gotValue, gotErr, wantValue, nil)
}
}
@@ -53,17 +43,12 @@ func TestExpandPathUser(t *testing.T) {
u, _ := user.Current()
source := "~"
wantValue := path.Join("/home", u.Username)
// wantErr := errors.New(`test expected string, got list ([])`)
wantErr := error(nil)
gotValue, gotErr := util.ExpandPath(source)
if gotErr != nil {
if gotErr != nil || gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
} else if gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
source, gotValue, gotErr, wantValue, nil)
}
}
@@ -71,17 +56,12 @@ func TestExpandPathEnv(t *testing.T) {
u, _ := user.Current()
source := "$HOME"
wantValue := path.Join("/home", u.Username)
// wantErr := errors.New(`test expected string, got list ([])`)
wantErr := error(nil)
gotValue, gotErr := util.ExpandPath(source)
if gotErr != nil {
if gotErr != nil || gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
} else if gotValue != wantValue {
t.Errorf(`ExpandPath(%v) gotValue=%q, gotErr=%v -> wantValue=%q, wantErr=%v`,
source, gotValue, gotErr, wantValue, wantErr)
source, gotValue, gotErr, wantValue, nil)
}
}
+1 -1
View File
@@ -215,7 +215,7 @@ func TestToStringOk(t *testing.T) {
}
func TestToStringErr(t *testing.T) {
source := array.NewListA()
source := array.NewArrayA()
wantValue := ""
wantErr := errors.New(`test expected string, got array ([])`)
+50 -46
View File
@@ -11,53 +11,53 @@ import (
"git.portale-stac.it/go-pkg/expr/kern"
)
type ListType []any
type ArrayType []any
func IsList(v any) (ok bool) {
_, ok = v.(*ListType)
_, ok = v.(*ArrayType)
return ok
}
func NewListA(listAny ...any) (list *ListType) {
func NewArrayA(listAny ...any) (list *ArrayType) {
if listAny == nil {
listAny = []any{}
}
return NewList(listAny)
return NewArray(listAny)
}
func NewList(listAny []any) (list *ListType) {
if listAny != nil {
ls := make(ListType, len(listAny))
copy(ls, listAny)
list = &ls
func NewArray(arrayAny []any) (aRef *ArrayType) {
if arrayAny != nil {
a := make(ArrayType, len(arrayAny))
copy(a, arrayAny)
aRef = &a
}
return
}
func MakeList(length, capacity int) (list *ListType) {
func MakeArray(length, capacity int) (list *ArrayType) {
if capacity < length {
capacity = length
}
ls := make(ListType, length, capacity)
ls := make(ArrayType, length, capacity)
list = &ls
return
}
func ListFromStrings(stringList []string) (list *ListType) {
list = MakeList(len(stringList), 0)
for i, s := range stringList {
(*list)[i] = s
func ArrayFromStrings(stringSlice []string) (aRef *ArrayType) {
aRef = MakeArray(len(stringSlice), 0)
for i, s := range stringSlice {
(*aRef)[i] = s
}
return
}
func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
func (aRef *ArrayType) ToString(opt kern.FmtOpt) (s string) {
indent := kern.GetFormatIndent(opt)
flags := kern.GetFormatFlags(opt)
var sb strings.Builder
sb.WriteByte('[')
if len(*ls) > 0 {
if len(*aRef) > 0 {
innerOpt := kern.MakeFormatOptions(flags, indent+1)
nest := strings.Repeat(" ", indent+1)
@@ -65,7 +65,7 @@ func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
sb.WriteByte('\n')
sb.WriteString(nest)
}
for i, item := range []any(*ls) {
for i, item := range []any(*aRef) {
if i > 0 {
if flags&kern.MultiLine != 0 {
sb.WriteString(",\n")
@@ -89,19 +89,19 @@ func (ls *ListType) ToString(opt kern.FmtOpt) (s string) {
return
}
func (ls *ListType) String() string {
return ls.ToString(0)
func (aRef *ArrayType) String() string {
return aRef.ToString(0)
}
func (ls *ListType) TypeName() string {
func (aRef *ArrayType) TypeName() string {
return kern.TypeArray
}
func (ls *ListType) Contains(t *ListType) (answer bool) {
if len(*ls) >= len(*t) {
func (aRef *ArrayType) Contains(t *ArrayType) (answer bool) {
if len(*aRef) >= len(*t) {
answer = true
for _, item := range *t {
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
if answer = aRef.IndexDeepSameCmp(item) >= 0; !answer {
break
}
}
@@ -109,17 +109,17 @@ func (ls *ListType) Contains(t *ListType) (answer bool) {
return
}
func (ls *ListType) EqualTo(other kern.Equaler) (equal bool) {
if otherList, ok := other.(*ListType); ok {
equal = ls.Equals(*otherList)
func (aRef *ArrayType) EqualTo(other kern.Equaler) (equal bool) {
if otherList, ok := other.(*ArrayType); ok {
equal = aRef.Equals(*otherList)
}
return
}
func (ls *ListType) Equals(ls2 ListType) (answer bool) {
if ls2 != nil && len(*ls) == len(ls2) {
func (aRef *ArrayType) Equals(ls2 ArrayType) (answer bool) {
if ls2 != nil && len(*aRef) == len(ls2) {
answer = true
for index, i1 := range *ls {
for index, i1 := range *aRef {
// if !reflect.DeepEqual(i1, ls2[index]) {
// answer = false
// break
@@ -133,18 +133,18 @@ func (ls *ListType) Equals(ls2 ListType) (answer bool) {
return
}
func (ls1 *ListType) Clone() (ls2 *ListType) {
ls := make(ListType, len(*ls1))
for i, item := range *ls1 {
func (aRef *ArrayType) Clone() (ls2 *ArrayType) {
ls := make(ArrayType, len(*aRef))
for i, item := range *aRef {
ls[i] = kern.Clone(item)
}
ls2 = &ls
return
}
func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
func (aRef *ArrayType) IndexDeepSameCmp(target any) (index int) {
index = -1
for i, item := range *ls {
for i, item := range *aRef {
if kern.Equal(item, target) {
index = i
break
@@ -169,12 +169,16 @@ func (ls *ListType) IndexDeepSameCmp(target any) (index int) {
// }
func SameContent(a, b any) (same bool, err error) {
la, _ := a.(*ListType)
lb, _ := b.(*ListType)
if len(*la) == len(*lb) {
aRef, aValid := a.(*ArrayType)
bRef, bValid := b.(*ArrayType)
if !aValid || !bValid {
err = fmt.Errorf("invalid type for comparison")
return
}
if len(*aRef) == len(*bRef) {
same = true
for _, item := range *la {
if pos := lb.IndexDeepSameCmp(item); pos < 0 {
for _, item := range *aRef {
if pos := bRef.IndexDeepSameCmp(item); pos < 0 {
same = false
break
}
@@ -208,15 +212,15 @@ func SameContent(a, b any) (same bool, err error) {
// return
// }
func (ls *ListType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*ls)) {
(*ls)[index] = value
func (aRef *ArrayType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*aRef)) {
(*aRef)[index] = value
} else {
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*ls)-1)
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*aRef)-1)
}
return
}
func (ls *ListType) AppendItem(value any) {
*ls = append(*ls, value)
func (aRef *ArrayType) AppendItem(value any) {
*aRef = append(*aRef, value)
}
+2 -2
View File
@@ -24,8 +24,8 @@ func Equal(value1, value2 any) (equal bool) {
} else if boolean.IsBool(value1) && boolean.IsBool(value2) {
equal = value1.(bool) == value2.(bool)
} else if array.IsList(value1) && array.IsList(value2) {
ls1 := value1.(*array.ListType)
ls2 := value2.(*array.ListType)
ls1 := value1.(*array.ArrayType)
ls2 := value2.(*array.ArrayType)
equal = ls1.Equals(*ls2)
} else if dict.IsDict(value1) && dict.IsDict(value2) {
d1 := value1.(*dict.DictType)
+1 -1
View File
@@ -34,7 +34,7 @@ func FromGenericAny(v any) (exprAny any, ok bool) {
if exprAny, ok = v.(*dict.DictType); ok {
return
}
if exprAny, ok = v.(*array.ListType); ok {
if exprAny, ok = v.(*array.ArrayType); ok {
return
}
}