Skip to content

Commit 3946e0f

Browse files
committed
Breaking: Initial upgrade of converters to real generated functions, see #620
1 parent f230d98 commit 3946e0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+7902
-3960
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ While only useful for the adventurous cherishing an aversion to [generated stati
175175
```js
176176
var writer = protobuf.Writer.create();
177177
var buffer = writer
178-
.int32(/* id */ 1 << 3 | /* wireType */ 2)
178+
.uint32(/* id */ 1 << 3 | /* wireType */ 2)
179179
.string("hello world!")
180180
.finish();
181181

182182
var reader = protobuf.Reader.create(buffer);
183183
while (reader.pos < reader.len) {
184-
var tag = reader.int32();
184+
var tag = reader.uint32();
185185
switch (/* id */ tag >>> 3) {
186186
case 1:
187187
console.log(reader.string());

bench/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ setTimeout(function() {
9494
.run();
9595

9696
var dataMessage = Test.from(data);
97-
var dataJson = dataMessage.asJSON();
97+
var dataObject = dataMessage.toObject();
9898

99-
newSuite("converting from JSON")
100-
.add("Message.from", function() {
101-
Test.from(dataJson);
99+
newSuite("message from object")
100+
.add("Type.fromObject", function() {
101+
Test.fromObject(dataObject);
102102
})
103103
.run();
104104

105-
newSuite("converting to JSON")
106-
.add("Message#asJSON", function() {
107-
dataMessage.asJSON();
105+
newSuite("message to object")
106+
.add("Type.toObject", function() {
107+
Test.toObject(dataMessage);
108108
})
109109
.run();
110110

cli/pbjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ exports.main = function(args, callback) {
9191
"",
9292
chalk.bold.gray(" Static targets only:"),
9393
"",
94-
" --no-create Does not generate create functions used for runtime compatibility.",
94+
" --no-create Does not generate create functions used for reflection compatibility.",
9595
" --no-encode Does not generate encode functions.",
9696
" --no-decode Does not generate decode functions.",
9797
" --no-verify Does not generate verify functions.",

cli/targets/static.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,12 @@ function beautify(code) {
154154
.replace(/\bc2\b/g, "end2")
155155
.replace(/\bk\b/g, "key")
156156
.replace(/\bks\b/g, "keys")
157+
.replace(/\bks2\b/g, "keys2")
157158
.replace(/\be\b/g, "err")
158159
.replace(/\bf\b/g, "impl")
159160
.replace(/\bo\b/g, "options")
160-
.replace(/\bs\b/g, "src")
161-
.replace(/\bd\b/g, "dst"),
161+
.replace(/\bd\b/g, "object")
162+
.replace(/\bn\b/g, "long"),
162163
{
163164
fromString: true,
164165
compress: false,
@@ -443,40 +444,63 @@ function buildType(ref, type) {
443444
if (config.convert) {
444445
push("");
445446
pushComment([
446-
"Converts " + aOrAn(type.name) + " message.",
447-
"@function",
448-
"@param {" + fullName + "|Object} source " + type.name + " message or plain object to convert",
449-
"@param {*} impl Converter implementation to use",
450-
"@param {Object.<string,*>} [options] Conversion options",
451-
"@returns {" + fullName + "|Object} Converted message"
447+
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
448+
"@param {Object.<string,*>} object Plain object",
449+
"@returns {" + fullName + "} " + type.name
452450
]);
453-
buildFunction(type, "convert", protobuf.converter(type), {
451+
buildFunction(type, "fromObject", protobuf.converter.fromObject(type), {
454452
util : "$protobuf.util",
455453
types : hasTypes ? "$types" : undefined
456454
});
457455

458456
push("");
459457
pushComment([
460-
"Creates " + aOrAn(type.name) + " message from JSON.",
461-
"@param {Object.<string,*>} source Source object",
462-
"@param {Object.<string,*>} [options] Conversion options",
458+
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
459+
"This is an alias of {@link " + fullName + ".fromObject}.",
460+
"@function",
461+
"@param {Object.<string,*>} object Plain object",
463462
"@returns {" + fullName + "} " + type.name
464463
]);
465-
push(name(type.name) + ".from = function from(source, options) {");
464+
push(name(type.name) + ".from = " + name(type.name) + ".fromObject;");
465+
466+
push("");
467+
pushComment([
468+
"Creates a plain object from " + aOrAn(type.name) + " message. Also converts values to other types if specified.",
469+
"@param {" + fullName + "} message " + type.name,
470+
"@param {$protobuf.ConversionOptions} [options] Conversion options",
471+
"@returns {Object.<string,*>} Plain object"
472+
]);
473+
buildFunction(type, "toObject", protobuf.converter.toObject(type), {
474+
util : "$protobuf.util",
475+
types : hasTypes ? "$types" : undefined
476+
});
477+
478+
push("");
479+
pushComment([
480+
"Creates a plain object from this " + type.name + " message. Also converts values to other types if specified.",
481+
"@param {$protobuf.ConversionOptions} [options] Conversion options",
482+
"@returns {Object.<string,*>} Plain object"
483+
]);
484+
push("$prototype.toObject = function toObject(options) {");
466485
++indent;
467-
push("return this.convert(source, $protobuf.converters.message, options);");
486+
push("return this.constructor.toObject(this, options);");
468487
--indent;
469488
push("};");
470489

471490
push("");
472491
pushComment([
473-
"Converts this " + type.name + " message to JSON.",
474-
"@param {Object.<string,*>} [options] Conversion options",
492+
"Converts this " + type.name + " to JSON.",
475493
"@returns {Object.<string,*>} JSON object"
476494
]);
477-
push("$prototype.asJSON = function asJSON(options) {");
495+
push("$prototype.toJSON = function toJSON() {");
478496
++indent;
479-
push("return this.constructor.convert(this, $protobuf.converters.json, options);");
497+
push("return this.constructor.toObject(this, {");
498+
++indent;
499+
push("longs: String,");
500+
push("enums: String,");
501+
push("bytes: String");
502+
--indent;
503+
push("});");
480504
--indent;
481505
push("};");
482506
}

0 commit comments

Comments
 (0)