Permalink
Browse files

New: Removed various clutter from generated static code

  • Loading branch information...
dcodeIO committed Jan 14, 2017
1 parent c7e14b1 commit dbd19fd9d3a57d033aad1d7173f7f66db8f8db3e
@@ -35,26 +35,8 @@ function static_target(root, options, callback) {
buildNamespace(null, root);
push("");
if (config.comments)
push("// Resolve lazy types");
push("$lazyTypes.forEach(function(types) {");
++indent;
push("types.forEach(function(path, i) {");
++indent;
push("if (!path)");
++indent;
push("return;");
--indent;
push("path = path.split(\".\");");
push("var ptr = $root;");
push("while (path.length)");
++indent;
push("ptr = ptr[path.shift()];");
--indent;
push("types[i] = ptr;");
--indent;
push("});");
--indent;
push("});");
push("// Resolve lazy type names to actual types");
push("$protobuf.util.lazyResolve($root, $lazyTypes);");
return callback(null, out.join("\n"));
} catch (err) {
return callback(err);
@@ -330,19 +312,19 @@ function buildType(ref, type) {
});
var hasTypes = false;
var types = type.fieldsArray.map(function(field) {
var types = [];
type.fieldsArray.forEach(function(field, index) {
if (field.resolve().resolvedType) { // including enums!
hasTypes = true;
return JSON.stringify(field.resolvedType.fullName.substring(1));
types.push(index + ":"+JSON.stringify(field.resolvedType.fullName.substring(1)));
}
return "null";
}).join(", ");
});
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
push("");
if (config.comments)
push("// Referenced types");
push("var $types = [" + types + "]; $lazyTypes.push($types);");
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
}
if (config.create) {
@@ -492,13 +474,7 @@ function buildType(ref, type) {
]);
push("$prototype.toJSON = function toJSON() {");
++indent;
push("return this.constructor.toObject(this, {");
++indent;
push("longs: String,");
push("enums: String,");
push("bytes: String");
--indent;
push("});");
push("return this.constructor.toObject(this, $protobuf.util.toJSONOptions);");
--indent;
push("};");
}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +72 Bytes (100%) dist/noparse/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +71 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +99 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -2374,6 +2374,20 @@ export namespace util {
*/
function oneOfSetter(fieldNames: string[]): () => any;
/**
* Lazily resolves fully qualified type names against the specified root.
* @param {Root} root Root instanceof
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
* @returns {undefined}
*/
function lazyResolve(root: Root, lazyTypes: { [k: number]: (string|ReflectionObject) }): void;
/**
* Default conversion options used for toJSON implementations.
* @type {ConversionOptions}
*/
var toJSONOptions: ConversionOptions;
/**
* A minimal UTF8 implementation for number arrays.
* @memberof util
@@ -96,6 +96,8 @@ function genValuePartial_fromObject(gen, field, fieldIndex, prop) {
converter.fromObject = function fromObject(mtype) {
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
var fields = mtype.fieldsArray;
if (!fields.length)
return util.codegen()("return new(this.ctor)");
var gen = util.codegen("d")
("var m=new(this.ctor)");
for (var i = 0; i < fields.length; ++i) {
@@ -1,6 +1,8 @@
"use strict";
module.exports = Message;
var util = require("./util");
/**
* Constructs a new message instance.
*
@@ -127,9 +129,5 @@ Message.prototype.toObject = function toObject(options) {
* @returns {Object.<string,*>} JSON object
*/
Message.prototype.toJSON = function toJSON() {
return this.$type.toObject(this, {
longs: String,
enums: String,
bytes: String
});
return this.$type.toObject(this, util.toJSONOptions);
};
@@ -222,3 +222,31 @@ util.oneOfSetter = function setOneOf(fieldNames) {
delete this[fieldNames[i]];
};
};
/**
* Lazily resolves fully qualified type names against the specified root.
* @param {Root} root Root instanceof
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
* @returns {undefined}
*/
util.lazyResolve = function lazyResolve(root, lazyTypes) {
lazyTypes.forEach(function(types) {
Object.keys(types).forEach(function(index) {
var path = types[index |= 0].split("."),
ptr = root;
while (path.length)
ptr = ptr[path.shift()];
types[index] = ptr || null;
});
});
};
/**
* Default conversion options used for toJSON implementations.
* @type {ConversionOptions}
*/
util.toJSONOptions = {
longs: String,
enums: String,
bytes: String
};
Oops, something went wrong.

0 comments on commit dbd19fd

Please sign in to comment.