The plus and minus operators now support lists for join and difference respectively
This commit is contained in:
@@ -6,6 +6,7 @@ package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
//-------- plus term
|
||||
@@ -39,6 +40,23 @@ func evalPlus(ctx exprContext, self *term) (v any, err error) {
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt + rightInt
|
||||
}
|
||||
} else if isList(leftValue) || isList(rightValue) {
|
||||
var leftList, rightList []any
|
||||
var ok bool
|
||||
if leftList, ok = leftValue.([]any); !ok {
|
||||
leftList = []any{leftValue}
|
||||
}
|
||||
if rightList, ok = rightValue.([]any); !ok {
|
||||
rightList = []any{rightValue}
|
||||
}
|
||||
sumList := make([]any, 0, len(leftList)+len(rightList))
|
||||
for _, item := range leftList {
|
||||
sumList = append(sumList, item)
|
||||
}
|
||||
for _, item := range rightList {
|
||||
sumList = append(sumList, item)
|
||||
}
|
||||
v = sumList
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
@@ -74,6 +92,16 @@ func evalMinus(ctx exprContext, self *term) (v any, err error) {
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt - rightInt
|
||||
}
|
||||
} else if isList(leftValue) && isList(rightValue) {
|
||||
leftList, _ := leftValue.([]any)
|
||||
rightList, _ := rightValue.([]any)
|
||||
diffList := make([]any, 0, len(leftList)-len(rightList))
|
||||
for _, item := range leftList {
|
||||
if slices.Index(rightList, item) < 0 {
|
||||
diffList = append(diffList, item)
|
||||
}
|
||||
}
|
||||
v = diffList
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user