new operator 'in' added. It check if an item is member of a list, or if a key is contained in a dictionary

This commit is contained in:
Celestino Amoroso 2024-05-18 07:47:41 +02:00
parent 14bb9e942b
commit c39970fa7e
5 changed files with 61 additions and 0 deletions

View File

@ -27,6 +27,7 @@ func TestDictParser(t *testing.T) {
/* 4 */ {`{1:"one",2:"two",3:"three"}.2`, "three", nil}, /* 4 */ {`{1:"one",2:"two",3:"three"}.2`, "three", nil},
/* 5 */ {`#{1:"one",2:"two",3:"three"}`, int64(3), nil}, /* 5 */ {`#{1:"one",2:"two",3:"three"}`, int64(3), nil},
/* 6 */ {`{1:"one"} + {2:"two"}`, map[any]any{1: "one", 2: "two"}, nil}, /* 6 */ {`{1:"one"} + {2:"two"}`, map[any]any{1: "one", 2: "two"}, nil},
/* 7 */ {`2 in {1:"one", 2:"two"}`, true, nil},
} }
succeeded := 0 succeeded := 0

View File

@ -40,6 +40,7 @@ func TestListParser(t *testing.T) {
/* 18 */ {`["a", "b", "c"]`, newListA("a", "b", "c"), nil}, /* 18 */ {`["a", "b", "c"]`, newListA("a", "b", "c"), nil},
/* 19 */ {`["a", "b", "c"]`, newList([]any{"a", "b", "c"}), nil}, /* 19 */ {`["a", "b", "c"]`, newList([]any{"a", "b", "c"}), nil},
/* 20 */ {`#["a", "b", "c"]`, int64(3), nil}, /* 20 */ {`#["a", "b", "c"]`, int64(3), nil},
/* 21 */ {`"b" in ["a", "b", "c"]`, true, nil},
// /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil}, // /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil},
// /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil}, // /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil},

View File

@ -6,6 +6,7 @@ package expr
import ( import (
"fmt" "fmt"
"reflect"
"strings" "strings"
) )
@ -61,6 +62,16 @@ func newList(listAny []any) (list *ListType) {
return return
} }
func (list *ListType) indexDeepCmp(target any) (index int) {
index = -1
for i, item := range *list {
if reflect.DeepEqual(item, target) {
index = i
break
}
}
return
}
// -------- list term // -------- list term
func newListTermA(args ...*term) *term { func newListTermA(args ...*term) *term {

46
operator-in.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-in.go
package expr
//-------- in term
func newInTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 2),
position: posInfix,
priority: priRelational,
evalFunc: evalIn,
}
}
func hasKey(d map[any]any, target any) (ok bool) {
_, ok = d[target]
return
}
func evalIn(ctx ExprContext, self *term) (v any, err error) {
var leftValue, rightValue any
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
return
}
if IsList(rightValue) {
list, _ := rightValue.(*ListType)
v = list.indexDeepCmp(leftValue) >= 0
} else if IsDict(rightValue) {
d, _ := rightValue.(map[any]any)
v = hasKey(d, leftValue)
} else {
err = self.errIncompatibleTypes(leftValue, rightValue)
}
return
}
// init
func init() {
registerTermConstructor(SymKwIn, newInTerm)
}

View File

@ -99,6 +99,7 @@ const (
SymKwBut SymKwBut
SymKwFunc SymKwFunc
SymKwBuiltin SymKwBuiltin
SymKwIn
SymKwInclude SymKwInclude
SymKwNil SymKwNil
) )
@ -112,6 +113,7 @@ func init() {
"BUILTIN": SymKwBuiltin, "BUILTIN": SymKwBuiltin,
"BUT": SymKwBut, "BUT": SymKwBut,
"FUNC": SymKwFunc, "FUNC": SymKwFunc,
"IN": SymKwIn,
"INCLUDE": SymKwInclude, "INCLUDE": SymKwInclude,
"NOT": SymKwNot, "NOT": SymKwNot,
"OR": SymKwOr, "OR": SymKwOr,