Made map IndexSet and IndexGet consistent. (#179)
This commit is contained in:
parent
8eabc23a45
commit
1a0c06b688
2 changed files with 23 additions and 2 deletions
|
@ -76,13 +76,13 @@ func (o *Map) Equals(x Object) bool {
|
||||||
|
|
||||||
// IndexGet returns the value for the given key.
|
// IndexGet returns the value for the given key.
|
||||||
func (o *Map) IndexGet(index Object) (res Object, err error) {
|
func (o *Map) IndexGet(index Object) (res Object, err error) {
|
||||||
strIdx, ok := index.(*String)
|
strIdx, ok := ToString(index)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = ErrInvalidIndexType
|
err = ErrInvalidIndexType
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val, ok := o.Value[strIdx.Value]
|
val, ok := o.Value[strIdx]
|
||||||
if !ok {
|
if !ok {
|
||||||
val = UndefinedValue
|
val = UndefinedValue
|
||||||
}
|
}
|
||||||
|
|
21
objects/map_test.go
Normal file
21
objects/map_test.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
package objects_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/d5/tengo/assert"
|
||||||
|
"github.com/d5/tengo/objects"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMap_Index(t *testing.T) {
|
||||||
|
m := &objects.Map{Value: make(map[string]objects.Object)}
|
||||||
|
k := &objects.Int{Value: 1}
|
||||||
|
v := &objects.String{Value: "abcdef"}
|
||||||
|
err := m.IndexSet(k, v)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
res, err := m.IndexGet(k)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, v, res)
|
||||||
|
}
|
Loading…
Reference in a new issue