Skip to content

Commit 664b2fd

Browse files
fix: make parsedOptions appear in method JSON representation
1 parent ade223d commit 664b2fd

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/method.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var util = require("./util");
2121
* @param {Object.<string,*>} [options] Declared options
2222
* @param {string} [comment] The comment for this method
2323
*/
24-
function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment) {
24+
function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {
2525

2626
/* istanbul ignore next */
2727
if (util.isObject(requestStream)) {
@@ -93,6 +93,11 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
9393
* @type {string|null}
9494
*/
9595
this.comment = comment;
96+
97+
/**
98+
* Options properly parsed into an object
99+
*/
100+
this.parsedOptions = parsedOptions;
96101
}
97102

98103
/**
@@ -104,6 +109,8 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
104109
* @property {boolean} [requestStream=false] Whether requests are streamed
105110
* @property {boolean} [responseStream=false] Whether responses are streamed
106111
* @property {Object.<string,*>} [options] Method options
112+
* @property {string} comment Method comments
113+
* @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
107114
*/
108115

109116
/**
@@ -114,7 +121,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
114121
* @throws {TypeError} If arguments are invalid
115122
*/
116123
Method.fromJSON = function fromJSON(name, json) {
117-
return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment);
124+
return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment, json.parsedOptions);
118125
};
119126

120127
/**
@@ -131,7 +138,8 @@ Method.prototype.toJSON = function toJSON(toJSONOptions) {
131138
"responseType" , this.responseType,
132139
"responseStream" , this.responseStream,
133140
"options" , this.options,
134-
"comment" , keepComments ? this.comment : undefined
141+
"comment" , keepComments ? this.comment : undefined,
142+
"parsedOptions" , this.parsedOptions,
135143
]);
136144
};
137145

tests/comp_options-parse.js

+39
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,44 @@ tape.test("Options", function (test) {
117117
test.end();
118118
});
119119

120+
test.test(test.name + " - rpc options (Nested)", function (test) {
121+
var TestOptionsRpc = root.lookup("TestOptionsRpc");
122+
test.equal(TestOptionsRpc.options["(method_rep_msg).value"], 1, "should merge repeated options messages");
123+
test.equal(TestOptionsRpc.options["(method_rep_msg).rep_value"], 3, "should parse in any order");
124+
test.equal(TestOptionsRpc.options["(method_rep_msg).nested.nested.value"], "x", "should correctly parse nested field options");
125+
test.equal(TestOptionsRpc.options["(method_rep_msg).rep_nested.value"], "z", "should take second repeated nested options");
126+
test.equal(TestOptionsRpc.options["(method_rep_msg).nested.value"], "w", "should merge nested options");
127+
128+
test.equal(TestOptionsRpc.options["(method_single_msg).nested.value"], "x", "should correctly parse nested property name");
129+
test.equal(TestOptionsRpc.options["(method_single_msg).rep_nested.value"], "y", "should take second repeated nested options");
130+
test.equal(TestOptionsRpc.options["(method_single_msg).rep_nested.nested.nested.value"], "y", "should correctly parse several nesting levels");
131+
132+
var expectedParsedOptions = [
133+
{
134+
"(method_rep_msg)": {
135+
value: 1,
136+
nested: {nested: {value: "x"}},
137+
rep_nested: [{value: "y"}, {value: "z"}],
138+
rep_value: 3
139+
}
140+
},
141+
{"(method_rep_msg)": {nested: {value: "w"}}},
142+
{
143+
"(method_single_msg)": {
144+
nested: {value: "x"},
145+
rep_nested: [{value: "x", nested: {nested: {value: "y"}}}, {value: "y"}]
146+
}
147+
}
148+
];
149+
150+
test.same(TestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly parse all nested message options");
151+
var jsonTestOptionsRpc = TestOptionsRpc.toJSON();
152+
test.same(jsonTestOptionsRpc.parsedOptions, expectedParsedOptions, "should correctly store all nested method options in JSON");
153+
var rootFromJson = protobuf.Root.fromJSON(root.toJSON());
154+
var TestOptionsRpcFromJson = rootFromJson.lookup("TestOptionsRpc");
155+
test.same(TestOptionsRpcFromJson.parsedOptions, expectedParsedOptions, "should correctly read all nested method options from JSON");
156+
test.end();
157+
});
158+
120159
test.end();
121160
});

tests/data/options_test.proto

+33-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ extend google.protobuf.MessageOptions {
5353
Msg mo_single_msg = 50003;
5454
}
5555

56+
extend google.protobuf.MethodOptions {
57+
repeated Msg method_rep_msg = 50002;
58+
Msg method_single_msg = 50003;
59+
}
60+
5661
message TestFieldOptionsMsg {
5762
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}];
5863
string field2 = 2 [(fo_single_msg).value = 7, (fo_single_msg).rep_value = 8, (fo_single_msg).rep_value = 9];
@@ -80,7 +85,6 @@ message TestFieldOptionsNested {
8085
string field3 = 3 [(fo_single_msg).nested = {value: "x" nested {nested{value: "y"}}}];
8186
}
8287

83-
8488
message TestMessageOptionsNested {
8589
option (mo_rep_msg) = {
8690
value: 1
@@ -106,3 +110,31 @@ message TestMessageOptionsNested {
106110
option (mo_single_msg).rep_nested = {value : "x" nested {nested{value: "y"}}};
107111
option (mo_single_msg).rep_nested = {value : "y"};
108112
}
113+
114+
service TestOptionsService {
115+
rpc TestOptionsRpc(Msg) returns (Msg) {
116+
option (method_rep_msg) = {
117+
value: 1
118+
nested {
119+
nested {
120+
value: "x"
121+
}
122+
}
123+
rep_nested {
124+
value: "y"
125+
}
126+
rep_nested {
127+
value: "z"
128+
}
129+
rep_value: 3
130+
};
131+
option (method_rep_msg) = {
132+
nested {
133+
value: "w"
134+
}
135+
};
136+
option (method_single_msg).nested.value = "x";
137+
option (method_single_msg).rep_nested = {value : "x" nested {nested{value: "y"}}};
138+
option (method_single_msg).rep_nested = {value : "y"};
139+
}
140+
}

0 commit comments

Comments
 (0)