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: generate valid js code for aliased enum values #1801

Merged
merged 1 commit into from
Sep 9, 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
4 changes: 3 additions & 1 deletion src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ var Enum = require("./enum"),
* @ignore
*/
function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
var defaultAlreadyEmitted = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would usually use let, but it looks like var is internally consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's the remains of Node 4 support. We probably want a global refactor to make the code more modern.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @bcoe and @alexander-fenster,

Firstly, Thanks for developing and maintaining this amazing library.

Second, I use protobuf.js massively in IoT embedded devices without experiencing any issues.
Due to small memory footprint I use embedded Javascript engine - Duktape (ES5/5.1 complaint + some more features from ES6/7).

If it is planned to use modern syntax for feature/bug fixes, I am concerned about Duktape compatibility issues with the new code. In addition, I don't believe there are better protobuf alternatives in Javascript.

In the future, I see 2 options:


  1. I maintain a forked ES5/5.1 version equivalent to protobuf.js releases (which could be a nightmare)
  2. You’d also be maintaining another branch for ES5/5.1 syntax.

Requesting your thoughts and guidance on the same.

Thanks!

/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
if (field.resolvedType) {
if (field.resolvedType instanceof Enum) { gen
("switch(d%s){", prop);
for (var values = field.resolvedType.values, keys = Object.keys(values), i = 0; i < keys.length; ++i) {
// enum unknown values passthrough
if (values[keys[i]] === field.typeDefault) { gen
if (values[keys[i]] === field.typeDefault && !defaultAlreadyEmitted) { gen
("default:")
("if(typeof(d%s)===\"number\"){m%s=d%s;break}", prop, prop, prop);
if (!field.repeated) gen // fallback to default value only for
// arrays, to avoid leaving holes.
("break"); // for non-repeated fields, just ignore
defaultAlreadyEmitted = true;
}
gen
("case%j:", keys[i])
Expand Down
2 changes: 2 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ tape.test("pbjs generates static code", function(test) {
value: 42,
},
regularField: "abc",
enumField: 0,
};
var obj1 = OneofContainer.toObject(OneofContainer.fromObject(obj));
test.deepEqual(obj, obj1, "fromObject and toObject work for plain object");
Expand All @@ -76,6 +77,7 @@ tape.test("pbjs generates static code", function(test) {
instance.messageInOneof = new Message();
instance.messageInOneof.value = 42;
instance.regularField = "abc";
instance.enumField = 0;
var instance1 = OneofContainerDynamic.toObject(OneofContainerDynamic.fromObject(instance));
test.deepEqual(instance, instance1, "fromObject and toObject work for instance of the static type");

Expand Down
9 changes: 9 additions & 0 deletions tests/data/cli/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ message Message {
int32 value = 1;
}

enum Enum {
option allow_alias = true;

UNSPECIFIED = 0;
DEFAULT = 0; // checking that an alias does not break things
SOMETHING = 1;
}

message OneofContainer {
oneof some_oneof {
string string_in_oneof = 1;
Message message_in_oneof = 2;
}
string regular_field = 3;
Enum enum_field = 4;
}