Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: map export #467

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otto
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -1074,3 +1075,33 @@ func Test_issue317(t *testing.T) {
})
}
}

func Test_issue252(t *testing.T) {
in := map[string]interface{}{
"attr": []interface{}{"string"},
}
expected := map[string]interface{}{
"attr": []string{"changed"},
}

vm := New()
err := vm.Set("In", in)
require.NoError(t, err)

result, err := vm.Run(`(function fn() {
var tmp = In;
tmp.attr = ["changed"];
return tmp;
})()`)
require.NoError(t, err)

actual, err := result.Export()
require.NoError(t, err)
require.Equal(t, expected, actual)

expBs, err := json.Marshal(expected)
require.NoError(t, err)
actBs, err := json.Marshal(actual)
require.NoError(t, err)
require.Equal(t, expBs, actBs)
}
7 changes: 7 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,13 @@ func (self Value) export() interface{} {
case *_goStructObject:
return value.value.Interface()
case *_goMapObject:
iter := value.value.MapRange()
for iter.Next() {
v := iter.Value()
if ov, ok := v.Interface().(Value); ok {
value.value.SetMapIndex(iter.Key(), reflect.ValueOf(ov.export()))
}
}
return value.value.Interface()
case *_goArrayObject:
return value.value.Interface()
Expand Down