operator at for dict
This commit is contained in:
+13
-5
@@ -8,6 +8,7 @@ import (
|
|||||||
"git.portale-stac.it/go-pkg/expr/kern"
|
"git.portale-stac.it/go-pkg/expr/kern"
|
||||||
"git.portale-stac.it/go-pkg/expr/scan"
|
"git.portale-stac.it/go-pkg/expr/scan"
|
||||||
"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"
|
"git.portale-stac.it/go-pkg/expr/types/list"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,13 +38,20 @@ func evalAt(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
|||||||
|
|
||||||
if array.IsArray(rightValue) {
|
if array.IsArray(rightValue) {
|
||||||
a, _ := rightValue.(*array.ArrayType)
|
a, _ := rightValue.(*array.ArrayType)
|
||||||
v = a.IndexDeepSameCmp(leftValue)
|
if index := a.IndexDeepSameCmp(leftValue); index >= 0 {
|
||||||
// } else if dict.IsDict(rightValue) {
|
v = index
|
||||||
// dict, _ := rightValue.(*dict.DictType)
|
}
|
||||||
// v = dict.HasKey(leftValue)
|
} else if dict.IsDict(rightValue) {
|
||||||
|
dict, _ := rightValue.(*dict.DictType)
|
||||||
|
if k, exists := dict.FindKey(leftValue); exists {
|
||||||
|
v = k
|
||||||
|
}
|
||||||
|
|
||||||
} else if list.IsLinkedList(rightValue) {
|
} else if list.IsLinkedList(rightValue) {
|
||||||
ls, _ := rightValue.(*list.LinkedList)
|
ls, _ := rightValue.(*list.LinkedList)
|
||||||
v = ls.Index(leftValue)
|
if index := ls.Index(leftValue); index >= 0 {
|
||||||
|
v = index
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ func TestDictParser(t *testing.T) {
|
|||||||
/* 22 */ {`f=func(n){"x"+n}; d={f(5):10}`, dict.NewDict(map[any]any{"x5": int64(10)}), nil},
|
/* 22 */ {`f=func(n){"x"+n}; d={f(5):10}`, dict.NewDict(map[any]any{"x5": int64(10)}), nil},
|
||||||
/* 23 */ {`f=func(n){"x"+n}; d={f(5); "z":10}`, nil, "[1:27] expected one of `:`, `}`, got `;`"},
|
/* 23 */ {`f=func(n){"x"+n}; d={f(5); "z":10}`, nil, "[1:27] expected one of `:`, `}`, got `;`"},
|
||||||
/* 24 */ {`f=func(n){"x"+n}; d={f(5) but "z":10}`, dict.NewDict(map[any]any{"z": int64(10)}), nil},
|
/* 24 */ {`f=func(n){"x"+n}; d={f(5) but "z":10}`, dict.NewDict(map[any]any{"z": int64(10)}), nil},
|
||||||
|
/* 25 */ {`3 in {1:"one", 2:"two"}`, false, nil},
|
||||||
|
/* 26 */ {`1 at {"a":2, "b":3, "c":1}`, "c", nil},
|
||||||
|
/* 27 */ {`10 at {"a":2, "b":3, "c":1}`, nil, nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunTestSuiteSpec(t, section, inputs, 1)
|
// RunTestSuiteSpec(t, section, inputs, 1)
|
||||||
@@ -88,6 +91,17 @@ func TestAccessSubFields(t *testing.T) {
|
|||||||
// runCtxTestSuite(t, ctx, section, inputs)
|
// runCtxTestSuite(t, ctx, section, inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDictDeepAssign(t *testing.T) {
|
||||||
|
section := "Dict-DeepAssign"
|
||||||
|
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},
|
||||||
|
}
|
||||||
|
|
||||||
|
// runTestSuiteSpec(t, section, inputs, 4)
|
||||||
|
RunTestSuite(t, section, inputs)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDictToStringMultiLine(t *testing.T) {
|
func TestDictToStringMultiLine(t *testing.T) {
|
||||||
var good bool
|
var good bool
|
||||||
section := "dict-ToString-ML"
|
section := "dict-ToString-ML"
|
||||||
|
|||||||
@@ -160,6 +160,17 @@ func (dict *DictType) GetItem(key any) (value any, exists bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dict *DictType) FindKey(item any) (key any, exists bool) {
|
||||||
|
for k, v := range *dict {
|
||||||
|
if kern.Equal(v, item) {
|
||||||
|
key = k
|
||||||
|
exists = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (dict *DictType) Clone() kern.Cloner {
|
func (dict *DictType) Clone() kern.Cloner {
|
||||||
c := newDict(nil)
|
c := newDict(nil)
|
||||||
for k, v := range *dict {
|
for k, v := range *dict {
|
||||||
|
|||||||
Reference in New Issue
Block a user