Skip to content

Commit

Permalink
CLI: Static code statically resolves types[..], see #715
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 23, 2017
1 parent 6f8f2c1 commit 23f14a6
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 586 deletions.
34 changes: 3 additions & 31 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ function static_target(root, options, callback) {
push((config.es6 ? "const " : "var ") + aliases.map(function(name) { return "$" + name + " = $protobuf." + name; }).join(", ") + ";");
push("");
}
if (config.comments)
push("// Lazily resolved type references");
push((config.es6 ? "const" : "var") + " $lazyTypes = [];");
push("");
if (config.comments) {
if (root.comment)
pushComment("@fileoverview " + root.comment);
Expand All @@ -49,10 +45,6 @@ function static_target(root, options, callback) {
var rootProp = cliUtil.safeProp(config.root || "default");
push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
buildNamespace(null, root);
push("");
if (config.comments)
push("// Resolve lazy type references to actual types");
push("$util.lazyResolve($root, $lazyTypes);");
return callback(null, out.join("\n"));
} catch (err) {
return callback(err);
Expand Down Expand Up @@ -217,7 +209,9 @@ function buildFunction(type, functionName, gen, scope) {
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
.replace(/\b(?!\.)types\[\b/g, "$types["); // "
.replace(/\b(?!\.)types\[(\d+)\]/g, function($0, $1) {
return "$root" + type.fieldsArray[$1].resolvedType.fullName;
});

if (config.beautify)
code = beautifyCode(code);
Expand Down Expand Up @@ -362,28 +356,6 @@ function buildType(ref, type) {
push("});");
});

var hasTypes = false;
var types = [];
type.fieldsArray.forEach(function(field, index) {
if (field.resolve().resolvedType) { // including enums!
hasTypes = true;
types.push(index + ": "+JSON.stringify(field.resolvedType.fullName.substring(1)));
}
});

if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
push("");
if (config.comments)
push("// Lazily resolved type references");
push((config.es6 ? "const" : "var") + " $types = {");
++indent;
types.forEach(function(line, i) {
push(line + (i === types.length - 1 ? "" : ","));
});
--indent;
push("}; $lazyTypes.push($types);");
}

if (config.create) {
push("");
pushComment([
Expand Down
2 changes: 1 addition & 1 deletion src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Class.generate = function generate(type) { // eslint-disable-line no-unused-vars
* @param {Type} type Reflected message type
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
* @returns {Message} Message prototype
* @deprecated Assign the constructor to {@link Type#ctor} instead
* @deprecated since 6.7.0 it's possible to just assign a new constructor to {@link Type#ctor}
*/
Class.create = Class;

Expand Down
1 change: 1 addition & 0 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ util.oneOfSetter = function setOneOf(fieldNames) {
* @param {Root} root Root instanceof
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
* @returns {undefined}
* @deprecated since 6.7.0 static code does not emit lazy types anymore
*/
util.lazyResolve = function lazyResolve(root, lazyTypes) {
for (var i = 0; i < lazyTypes.length; ++i) {
Expand Down
6 changes: 0 additions & 6 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ var $protobuf = require("../../minimal");
// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];

// Exported root namespace
var $root = $protobuf.roots.test_comments || ($protobuf.roots.test_comments = {});

Expand Down Expand Up @@ -379,7 +376,4 @@ $root.Test3 = (function() {
return values;
})();

// Resolve lazy type references to actual types
$util.lazyResolve($root, $lazyTypes);

module.exports = $root;
16 changes: 2 additions & 14 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ var $protobuf = require("../../minimal");
// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];

// Exported root namespace
var $root = $protobuf.roots.test_convert || ($protobuf.roots.test_convert = {});

Expand Down Expand Up @@ -85,12 +82,6 @@ $root.Message = (function() {
*/
Message.prototype.int64Map = $util.emptyObject;

// Lazily resolved type references
var $types = {
6: "Message.SomeEnum",
7: "Message.SomeEnum"
}; $lazyTypes.push($types);

/**
* Creates a new Message instance using the specified properties.
* @param {Object.<string,*>=} [properties] Properties to set
Expand Down Expand Up @@ -471,11 +462,11 @@ $root.Message = (function() {
object.bytesRepeated[j] = options.bytes === String ? $util.base64.encode(message.bytesRepeated[j], 0, message.bytesRepeated[j].length) : options.bytes === Array ? Array.prototype.slice.call(message.bytesRepeated[j]) : message.bytesRepeated[j];
}
if (message.enumVal !== undefined && message.enumVal !== null && message.hasOwnProperty("enumVal"))
object.enumVal = options.enums === String ? $types[6][message.enumVal] : message.enumVal;
object.enumVal = options.enums === String ? $root.Message.SomeEnum[message.enumVal] : message.enumVal;
if (message.enumRepeated && message.enumRepeated.length) {
object.enumRepeated = [];
for (var j = 0; j < message.enumRepeated.length; ++j)
object.enumRepeated[j] = options.enums === String ? $types[7][message.enumRepeated[j]] : message.enumRepeated[j];
object.enumRepeated[j] = options.enums === String ? $root.Message.SomeEnum[message.enumRepeated[j]] : message.enumRepeated[j];
}
var keys2;
if (message.int64Map && (keys2 = Object.keys(message.int64Map)).length) {
Expand Down Expand Up @@ -524,7 +515,4 @@ $root.Message = (function() {
return Message;
})();

// Resolve lazy type references to actual types
$util.lazyResolve($root, $lazyTypes);

module.exports = $root;
54 changes: 16 additions & 38 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ var $protobuf = require("../../../minimal");
// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];

// Exported root namespace
var $root = $protobuf.roots.test_vector_tile || ($protobuf.roots.test_vector_tile = {});

Expand Down Expand Up @@ -42,11 +39,6 @@ $root.vector_tile = (function() {
*/
Tile.prototype.layers = $util.emptyArray;

// Lazily resolved type references
var $types = {
0: "vector_tile.Tile.Layer"
}; $lazyTypes.push($types);

/**
* Creates a new Tile instance using the specified properties.
* @param {Object.<string,*>=} [properties] Properties to set
Expand All @@ -67,7 +59,7 @@ $root.vector_tile = (function() {
writer = $Writer.create();
if (message.layers !== undefined && message.hasOwnProperty("layers"))
for (var i = 0; i < message.layers.length; ++i)
$types[0].encode(message.layers[i], writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
$root.vector_tile.Tile.Layer.encode(message.layers[i], writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
return writer;
};

Expand Down Expand Up @@ -99,7 +91,7 @@ $root.vector_tile = (function() {
case 3:
if (!(message.layers && message.layers.length))
message.layers = [];
message.layers.push($types[0].decode(reader, reader.uint32()));
message.layers.push($root.vector_tile.Tile.Layer.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
Expand Down Expand Up @@ -134,7 +126,7 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.layers))
return "layers: array expected";
for (var i = 0; i < message.layers.length; ++i) {
var error = $types[0].verify(message.layers[i]);
var error = $root.vector_tile.Tile.Layer.verify(message.layers[i]);
if (error)
return "layers." + error;
}
Expand All @@ -158,7 +150,7 @@ $root.vector_tile = (function() {
for (var i = 0; i < object.layers.length; ++i) {
if (typeof object.layers[i] !== "object")
throw TypeError(".vector_tile.Tile.layers: object expected");
message.layers[i] = $types[0].fromObject(object.layers[i]);
message.layers[i] = $root.vector_tile.Tile.Layer.fromObject(object.layers[i]);
}
}
return message;
Expand Down Expand Up @@ -188,7 +180,7 @@ $root.vector_tile = (function() {
if (message.layers && message.layers.length) {
object.layers = [];
for (var j = 0; j < message.layers.length; ++j)
object.layers[j] = $types[0].toObject(message.layers[j], options);
object.layers[j] = $root.vector_tile.Tile.Layer.toObject(message.layers[j], options);
}
return object;
};
Expand Down Expand Up @@ -592,11 +584,6 @@ $root.vector_tile = (function() {
*/
Feature.prototype.geometry = $util.emptyArray;

// Lazily resolved type references
var $types = {
2: "vector_tile.Tile.GeomType"
}; $lazyTypes.push($types);

/**
* Creates a new Feature instance using the specified properties.
* @param {Object.<string,*>=} [properties] Properties to set
Expand Down Expand Up @@ -839,7 +826,7 @@ $root.vector_tile = (function() {
object.tags[j] = message.tags[j];
}
if (message.type !== undefined && message.type !== null && message.hasOwnProperty("type"))
object.type = options.enums === String ? $types[2][message.type] : message.type;
object.type = options.enums === String ? $root.vector_tile.Tile.GeomType[message.type] : message.type;
if (message.geometry && message.geometry.length) {
object.geometry = [];
for (var j = 0; j < message.geometry.length; ++j)
Expand Down Expand Up @@ -921,12 +908,6 @@ $root.vector_tile = (function() {
*/
Layer.prototype.extent = 4096;

// Lazily resolved type references
var $types = {
2: "vector_tile.Tile.Feature",
4: "vector_tile.Tile.Value"
}; $lazyTypes.push($types);

/**
* Creates a new Layer instance using the specified properties.
* @param {Object.<string,*>=} [properties] Properties to set
Expand All @@ -948,13 +929,13 @@ $root.vector_tile = (function() {
writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
if (message.features !== undefined && message.hasOwnProperty("features"))
for (var i = 0; i < message.features.length; ++i)
$types[2].encode(message.features[i], writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim();
$root.vector_tile.Tile.Feature.encode(message.features[i], writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim();
if (message.keys !== undefined && message.hasOwnProperty("keys"))
for (var i = 0; i < message.keys.length; ++i)
writer.uint32(/* id 3, wireType 2 =*/26).string(message.keys[i]);
if (message.values !== undefined && message.hasOwnProperty("values"))
for (var i = 0; i < message.values.length; ++i)
$types[4].encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
$root.vector_tile.Tile.Value.encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
writer.uint32(/* id 5, wireType 0 =*/40).uint32(message.extent);
writer.uint32(/* id 15, wireType 0 =*/120).uint32(message.version);
Expand Down Expand Up @@ -995,7 +976,7 @@ $root.vector_tile = (function() {
case 2:
if (!(message.features && message.features.length))
message.features = [];
message.features.push($types[2].decode(reader, reader.uint32()));
message.features.push($root.vector_tile.Tile.Feature.decode(reader, reader.uint32()));
break;
case 3:
if (!(message.keys && message.keys.length))
Expand All @@ -1005,7 +986,7 @@ $root.vector_tile = (function() {
case 4:
if (!(message.values && message.values.length))
message.values = [];
message.values.push($types[4].decode(reader, reader.uint32()));
message.values.push($root.vector_tile.Tile.Value.decode(reader, reader.uint32()));
break;
case 5:
message.extent = reader.uint32();
Expand Down Expand Up @@ -1051,7 +1032,7 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.features))
return "features: array expected";
for (var i = 0; i < message.features.length; ++i) {
var error = $types[2].verify(message.features[i]);
var error = $root.vector_tile.Tile.Feature.verify(message.features[i]);
if (error)
return "features." + error;
}
Expand All @@ -1067,7 +1048,7 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.values))
return "values: array expected";
for (var i = 0; i < message.values.length; ++i) {
var error = $types[4].verify(message.values[i]);
var error = $root.vector_tile.Tile.Value.verify(message.values[i]);
if (error)
return "values." + error;
}
Expand Down Expand Up @@ -1098,7 +1079,7 @@ $root.vector_tile = (function() {
for (var i = 0; i < object.features.length; ++i) {
if (typeof object.features[i] !== "object")
throw TypeError(".vector_tile.Tile.Layer.features: object expected");
message.features[i] = $types[2].fromObject(object.features[i]);
message.features[i] = $root.vector_tile.Tile.Feature.fromObject(object.features[i]);
}
}
if (object.keys) {
Expand All @@ -1115,7 +1096,7 @@ $root.vector_tile = (function() {
for (var i = 0; i < object.values.length; ++i) {
if (typeof object.values[i] !== "object")
throw TypeError(".vector_tile.Tile.Layer.values: object expected");
message.values[i] = $types[4].fromObject(object.values[i]);
message.values[i] = $root.vector_tile.Tile.Value.fromObject(object.values[i]);
}
}
if (object.extent !== undefined && object.extent !== null)
Expand Down Expand Up @@ -1157,7 +1138,7 @@ $root.vector_tile = (function() {
if (message.features && message.features.length) {
object.features = [];
for (var j = 0; j < message.features.length; ++j)
object.features[j] = $types[1].toObject(message.features[j], options);
object.features[j] = $root.vector_tile.Tile.Feature.toObject(message.features[j], options);
}
if (message.keys && message.keys.length) {
object.keys = [];
Expand All @@ -1167,7 +1148,7 @@ $root.vector_tile = (function() {
if (message.values && message.values.length) {
object.values = [];
for (var j = 0; j < message.values.length; ++j)
object.values[j] = $types[3].toObject(message.values[j], options);
object.values[j] = $root.vector_tile.Tile.Value.toObject(message.values[j], options);
}
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
object.extent = message.extent;
Expand Down Expand Up @@ -1202,7 +1183,4 @@ $root.vector_tile = (function() {
return vector_tile;
})();

// Resolve lazy type references to actual types
$util.lazyResolve($root, $lazyTypes);

module.exports = $root;
Loading

0 comments on commit 23f14a6

Please sign in to comment.