operator-dot: added support to access dict value by exprssion; example: D.key, D."key", D.expr
This commit is contained in:
parent
32c0b45255
commit
610e2df5f5
@ -44,6 +44,17 @@ func evalDot(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
||||
} else {
|
||||
err = indexTerm.tk.ErrorExpectedGot("identifier")
|
||||
}
|
||||
case *kern.DictType:
|
||||
s := opTerm.children[1].symbol()
|
||||
if s == SymVariable || s == SymString {
|
||||
src := opTerm.children[1].Source()
|
||||
if len(src) > 1 && src[0] == '"' && src[len(src)-1] == '"' {
|
||||
src = src[1 : len(src)-1]
|
||||
}
|
||||
v, err = unboxedValue.GetItem(src)
|
||||
} else if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
|
||||
v, err = unboxedValue.GetItem(rightValue)
|
||||
}
|
||||
default:
|
||||
if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
|
||||
@ -39,6 +39,9 @@ func TestDictParser(t *testing.T) {
|
||||
//"b":2,
|
||||
"c":3
|
||||
}`, map[any]any{"a": 1, "c": 3}, nil},
|
||||
/* 13 */ {`D={"a":1, "b":2}; D."a"`, int64(1), nil},
|
||||
/* 14 */ {`D={"a":1, "b":2}; D.a`, int64(1), nil},
|
||||
/* 15 */ {`D={1:"a", 2:"b", 3:"c"}; D.(1+2)`, "c", nil},
|
||||
}
|
||||
|
||||
succeeded := 0
|
||||
|
||||
Loading…
Reference in New Issue
Block a user