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

feat: honor json_name option #1825

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"prof": "node bench/prof",
"test": "npm run test:sources && npm run test:types",
"test:sources": "tape -r ./lib/tape-adapter tests/*.js tests/node/*.js",
"test:types": "tsc tests/comp_typescript.ts --lib es2015 --esModuleInterop --strictNullChecks --experimentalDecorators --emitDecoratorMetadata && tsc tests/data/test.js.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks && tsc tests/data/*.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks",
"test:types": "tsc tests/comp_typescript.ts --lib es2015 --esModuleInterop --strictNullChecks --experimentalDecorators --emitDecoratorMetadata && tsc tests/data/test.js.ts --lib es2015 --esModuleInterop --noEmit --strictNullChecks && tsc --project tests/data/all_files.tsconfig.json --lib es2015 --esModuleInterop --noEmit --strictNullChecks",
"make": "npm run lint:sources && npm run build && npm run lint:types && node ./scripts/gentests.js && npm test"
},
"dependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
return ReflectionObject.prototype.setOption.call(this, name, value, ifNotSet);
};

Field.prototype.applyParsedOptions = function applyParsedOptions() {
if(this.parsedOptions !== null) {
var opt = this.parsedOptions.find(function (opt) {
return Object.prototype.hasOwnProperty.call(opt, "json_name");
});
if(opt)
this.name = opt["json_name"];
}
};

/**
* Field descriptor.
* @interface IField
Expand Down
2 changes: 2 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ function parse(source, root, options) {
parseInlineOptions(field);
});

field.applyParsedOptions();

if (rule === "proto3_optional") {
// for proto3 optional fields, we create a single-member Oneof to mimic "optional" behavior
var oneof = new OneOf("_" + name);
Expand Down
2 changes: 2 additions & 0 deletions tests/comp_options-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ tape.test("Options", function (test) {
test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).value"], 7, "should correctly parse single msg option");
test.equal(TestFieldOptionsMsg.fields.field2.options["(fo_single_msg).rep_value"], 9, "should take second repeated int in single msg option");
test.same(TestFieldOptionsMsg.fields.field2.parsedOptions, [{"(fo_single_msg)": {value: 7, rep_value: [8,9]}}], "should take all repeated message option");
test.equal(TestFieldOptionsMsg.fields.Field_Three.options["json_name"], "Field_Three", "should correctly parse json_name option");
test.equal(TestFieldOptionsMsg.fields.field3, undefined, "json_name option should change field name");
test.end();
});

Expand Down
4 changes: 4 additions & 0 deletions tests/data/all_files.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["*.ts"]
}
1 change: 1 addition & 0 deletions tests/data/options_test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extend google.protobuf.MethodOptions {
message TestFieldOptionsMsg {
string field1 = 1 [(fo_rep_msg) = {value: 1 rep_value: 2 rep_value: 3}, (fo_rep_msg) = {value: 4 rep_value: 5 rep_value: 6}];
string field2 = 2 [(fo_single_msg).value = 7, (fo_single_msg).rep_value = 8, (fo_single_msg).rep_value = 9];
string field3 = 3 [json_name = "Field_Three"];
}

message TestMessageOptionsMsg {
Expand Down