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

Changed attributes in objects are not Export()ed #252

Closed
calmh opened this issue May 17, 2017 · 0 comments · Fixed by #467
Closed

Changed attributes in objects are not Export()ed #252

calmh opened this issue May 17, 2017 · 0 comments · Fixed by #467

Comments

@calmh
Copy link

calmh commented May 17, 2017

I have the following use case, around templating: take an object which comes from JSON (i.e., map[string]interface{} etc.), pass it to some Javascript code, receive the maybe changed object, and JSON it back again.

The following test case illustrates what I do, and what I see as the failure when I do that:

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

	vm := otto.New()
	vm.Set("In", in)
	result, err := vm.Run(`(function fn() {
		var tmp = In;
		tmp.attr = ["changed"];
		return tmp;
	})()`)
	if err != nil {
		t.Fatal(err)
	}

	actual, err := result.Export()
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(actual, expected) {
		t.Errorf("Returned object does not match expected:\n\tactual: %#v\n\texpected: %#v", actual, expected)
	}

	expBs, _ := json.Marshal(expected)
	actBs, err := json.Marshal(actual)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(actBs, expBs) {
		t.Errorf("JSON serialization differs:\n\tactual: %s\n\texpected: %s", actBs, expBs)
	}
}

I would expect the test to pass, but the first check fails:

template_test.go:130: Returned object does not match expected:
  actual: map[string]interface {}{"attr":otto.Value{kind:5, value:(*otto._object)(0xc4201a0960)}}
  expected: map[string]interface {}{"attr":[]interface {}{"changed"}}

Here the changed attr is an otto.Value where I would have expected a []interface{} (or possibly slice of string or something). The next case, the JSON serialization:

template_test.go:139: JSON serialization differs:
  actual: {"attr":{}}
  expected: {"attr":["changed"]}

The slice here is gone and replaced by an empty object. Note that the serialization itself didn't fail, which is also an error in itself...

stevenh added a commit that referenced this issue Nov 28, 2022
When exporting a map check for values which are otto.Value and process
them. This is a one level export and no additional traversal is done.

Fixes #252
stevenh added a commit that referenced this issue Nov 28, 2022
When exporting a map check for values which are otto.Value and process
them. This is a one level export and no additional traversal is done.

Fixes #252
stevenh added a commit that referenced this issue Nov 28, 2022
When putting JavaScript objects into _go*object use exported values.
This reverts PR #467 in favour of this more complete work.

Fixes #118 #252

Co-authored-by: Dmitry Panov <dop@itoolabs.com>
stevenh added a commit that referenced this issue Nov 28, 2022
When putting JavaScript objects into _go*object use exported values.
This reverts PR #467 in favour of this more complete work.

Fixes #118 #252

Co-authored-by: Dmitry Panov <dop@itoolabs.com>
stevenh added a commit that referenced this issue Nov 28, 2022
When putting JavaScript objects into _go*object use exported values.
This reverts PR #467 in favour of this more complete work.

Fixes #118 #252

Co-authored-by: Dmitry Panov <dop@itoolabs.com>
sg3des pushed a commit to sg3des/otto that referenced this issue Jul 17, 2023
When exporting a map check for values which are otto.Value and process
them. This is a one level export and no additional traversal is done.

Fixes robertkrimen#252
sg3des pushed a commit to sg3des/otto that referenced this issue Jul 17, 2023
When putting JavaScript objects into _go*object use exported values.
This reverts PR robertkrimen#467 in favour of this more complete work.

Fixes robertkrimen#118 robertkrimen#252

Co-authored-by: Dmitry Panov <dop@itoolabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant