Skip to content
Merged
Changes from 2 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
2 changes: 1 addition & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ converter.toObject = function toObject(mtype) {
("}else")
("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?

else gen
("d%s=%j", prop, field.typeDefault); // also messages (=null)
} gen
Expand Down