Skip to content

Converter: Fix 'bytes' field decoding into an empty array, now decoded into an empty Buffer#1020

Merged
dcodeIO merged 5 commits intoprotobufjs:masterfrom
assaf-xm:master
Apr 30, 2018
Merged

Converter: Fix 'bytes' field decoding into an empty array, now decoded into an empty Buffer#1020
dcodeIO merged 5 commits intoprotobufjs:masterfrom
assaf-xm:master

Conversation

@assaf-xm
Copy link
Copy Markdown
Contributor

No description provided.

@assaf-xm
Copy link
Copy Markdown
Contributor Author

Should fix #1012

@assaf-xm
Copy link
Copy Markdown
Contributor Author

@dcodeIO can you take a look on the fix?

Comment thread src/converter.js Outdated
("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber());
else if (field.bytes) gen
("d%s=o.bytes===String?%j:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]");
("d%s=o.bytes===String?%j:o.bytes===Array?%s:%s", prop, String.fromCharCode.apply(String, field.typeDefault), "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]", "util.newBuffer([])");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the default for buffer doesn't use the default value. What do you think of making an array all the time, similar to how it was before, but wrapping it with util.newBuffer conditionally for the !== Array case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a fix

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this'd still generate the same array literal two times, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like:
object.buf = options.bytes === String ? "" : options.bytes === Array ? [] : $util.newBuffer([]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if the default value is empty. What's on my mind is something along the lines of:

if (options.bytes === String) object.buf = "...";
else {
  object.buf = [...];
  if (options.bytes !== Array) object.buf = $util.newBuffer(object.buf);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK possible as well, pushed now.
The generated code is:

    if (options.bytes === String)
        object.buf = "";
    else {
        object.buf = [];
        if (options.bytes !== Array)
            object.buf = $util.newBuffer(object.buf);
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcodeIO , is it better now?

@assaf-xm
Copy link
Copy Markdown
Contributor Author

@dcodeIO , the PR is ready for a second review.

@dcodeIO
Copy link
Copy Markdown
Member

dcodeIO commented Apr 25, 2018

Thanks for the heads up! Looking good to me now, except that it has some unnecessary whitespace in the gened parts.

Comment thread src/converter.js Outdated
else if (field.bytes) {
var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]";
gen
("if(o.bytes===String) d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, can remove whitespace at ) d

Comment thread src/converter.js Outdated
var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]";
gen
("if(o.bytes===String) d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
("else {")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at e {

Comment thread src/converter.js Outdated
("if(o.bytes===String) d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
("else {")
("d%s=%s", prop, arrayDefault)
("if(o.bytes!==Array) d%s=util.newBuffer(d%s)", prop, prop)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at ) d

@assaf-xm
Copy link
Copy Markdown
Contributor Author

100%, whitespaces were removed

@assaf-xm
Copy link
Copy Markdown
Contributor Author

@dcodeIO - I hope it's ready now

@dcodeIO dcodeIO merged commit a927a66 into protobufjs:master Apr 30, 2018
@dcodeIO
Copy link
Copy Markdown
Member

dcodeIO commented Apr 30, 2018

Thank you! :)

@assaf-xm
Copy link
Copy Markdown
Contributor Author

assaf-xm commented May 8, 2018

@dcodeIO , Hi, when the next release including this fix is expected?

@dcodeIO
Copy link
Copy Markdown
Member

dcodeIO commented May 8, 2018

There are some build issues with node v10 currently that I haven't yet been able to investigate.

@assaf-xm
Copy link
Copy Markdown
Contributor Author

@dcodeIO , when do you expect the next release to be issued?

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 this pull request may close these issues.

2 participants