Skip to content

Commit

Permalink
CLI: Made sure that static target's replacement regexes don't match f…
Browse files Browse the repository at this point in the history
…ields
  • Loading branch information
dcodeIO committed Jan 14, 2017
1 parent cda5c54 commit 1cc8a24
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 135 deletions.
22 changes: 14 additions & 8 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function static_target(root, options, callback) {
buildNamespace(null, root);
push("");
if (config.comments)
push("// Resolve lazy type names to actual types");
push("// Resolve lazy type references to actual types");
push("$util.lazyResolve($root, $lazyTypes);");
return callback(null, out.join("\n"));
} catch (err) {
Expand Down Expand Up @@ -172,10 +172,10 @@ function buildFunction(type, functionName, gen, scope) {
var code = gen.str(functionName)
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
.replace(/(types\[\d+])(\.values)/g, "$1") // enums: use types[N] instead of reflected types[N].values
.replace(/\bWriter\b/g, "$Writer") // use common aliases instead of binding through an iife
.replace(/\bReader\b/g, "$Reader")
.replace(/\butil\b/g, "$util")
.replace(/\btypes\b/g, "$types");
.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[");

if (config.beautify)
code = beautify(code);
Expand Down Expand Up @@ -331,15 +331,21 @@ function buildType(ref, type) {
type.fieldsArray.forEach(function(field, index) {
if (field.resolve().resolvedType) { // including enums!
hasTypes = true;
types.push(index + ":"+JSON.stringify(field.resolvedType.fullName.substring(1)));
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 referenced types");
push("var $types = {" + types.join(",") + "}; $lazyTypes.push($types);");
push("// Lazily resolved type references");
push("var $types = {");
++indent;
types.forEach(function(line, i) {
push(line + (i === types.length - 1 ? "" : ","));
});
--indent;
push("}; $lazyTypes.push($types);");
}

if (config.create) {
Expand Down
4 changes: 1 addition & 3 deletions cli/wrappers/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ define(["protobuf"], function($protobuf) {

%OUTPUT%

$protobuf.roots[%ROOT%] = $root;

return $root;
return $protobuf.roots[%ROOT%] = $root;
});
4 changes: 1 addition & 3 deletions cli/wrappers/commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ var $protobuf = require(%DEPENDENCY%);

%OUTPUT%

$protobuf.roots[%ROOT%] = $root;

module.exports = $root;
module.exports = $protobuf.roots[%ROOT%] = $root;
4 changes: 1 addition & 3 deletions cli/wrappers/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@

%OUTPUT%

$protobuf.roots[%ROOT%] = $root;

return $root;
return $protobuf.roots[%ROOT%] = $root;
});
12 changes: 6 additions & 6 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ $root.B = (function() {
*/
B.prototype.A = null;

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

/**
* Creates a new B instance using the specified properties.
Expand Down Expand Up @@ -366,9 +368,7 @@ $root.B = (function() {
return B;
})();

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

$protobuf.roots["test_ambiguous-names"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_ambiguous-names"] = $root;
6 changes: 2 additions & 4 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,7 @@ $root.Test3 = (function() {
return values;
})();

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

$protobuf.roots["test_comments"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_comments"] = $root;
13 changes: 7 additions & 6 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ $root.Message = (function() {
*/
Message.prototype.int64Map = $util.emptyObject;

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

/**
* Creates a new Message instance using the specified properties.
Expand Down Expand Up @@ -637,9 +640,7 @@ $root.Message = (function() {
return Message;
})();

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

$protobuf.roots["test_convert"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_convert"] = $root;
25 changes: 15 additions & 10 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ $root.vector_tile = (function() {
*/
Tile.prototype.layers = $util.emptyArray;

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

/**
* Creates a new Tile instance using the specified properties.
Expand Down Expand Up @@ -685,8 +687,10 @@ $root.vector_tile = (function() {
*/
Feature.prototype.geometry = $util.emptyArray;

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

/**
* Creates a new Feature instance using the specified properties.
Expand Down Expand Up @@ -1058,8 +1062,11 @@ $root.vector_tile = (function() {
*/
Layer.prototype.extent = 4096;

// Lazily resolved referenced types
var $types = {2:"vector_tile.Tile.Feature",4:"vector_tile.Tile.Value"}; $lazyTypes.push($types);
// 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.
Expand Down Expand Up @@ -1373,9 +1380,7 @@ $root.vector_tile = (function() {
return vector_tile;
})();

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

$protobuf.roots["test_vector_tile"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_vector_tile"] = $root;
36 changes: 18 additions & 18 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ $root.Package = (function() {
*/
Package.prototype.cliDependencies = $util.emptyArray;

// Lazily resolved referenced types
var $types = {5:"Package.Repository"}; $lazyTypes.push($types);
// Lazily resolved type references
var $types = {
5: "Package.Repository"
}; $lazyTypes.push($types);

/**
* Creates a new Package instance using the specified properties.
Expand Down Expand Up @@ -211,8 +213,8 @@ $root.Package = (function() {
writer.uint32(122).fork().uint32(10).string(keys[i]).uint32(18).string(message.devDependencies[keys[i]]).ldelim();
}
}
if (message.$types !== undefined && message.$types !== "") {
writer.uint32(138).string(message.$types);
if (message.types !== undefined && message.types !== "") {
writer.uint32(138).string(message.types);
}
if (message.cliDependencies) {
for (var i = 0; i < message.cliDependencies.length; ++i) {
Expand Down Expand Up @@ -340,7 +342,7 @@ $root.Package = (function() {
break;

case 17:
message.$types = reader.string();
message.types = reader.string();
break;

case 18:
Expand Down Expand Up @@ -485,9 +487,9 @@ $root.Package = (function() {
}
}
}
if (message.$types !== undefined) {
if (!$util.isString(message.$types)) {
return "$types: string expected";
if (message.types !== undefined) {
if (!$util.isString(message.types)) {
return "types: string expected";
}
}
if (message.cliDependencies !== undefined) {
Expand Down Expand Up @@ -573,8 +575,8 @@ $root.Package = (function() {
message.devDependencies[keys[i]] = String(object.devDependencies[keys[i]]);
}
}
if (object.$types !== undefined && object.$types !== null) {
message.$types = String(object.$types);
if (object.types !== undefined && object.types !== null) {
message.types = String(object.types);
}
if (object.cliDependencies) {
message.cliDependencies = [];
Expand Down Expand Up @@ -625,7 +627,7 @@ $root.Package = (function() {
object.bugs = "";
object.homepage = "";
object.main = "";
object.$types = "";
object.types = "";
}
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i) {
switch (keys[i]) {
Expand Down Expand Up @@ -737,9 +739,9 @@ $root.Package = (function() {
}
break;

case "$types":
if (message.$types !== undefined && message.$types !== null) {
object.$types = message.$types;
case "types":
if (message.types !== undefined && message.types !== null) {
object.types = message.types;
}
break;

Expand Down Expand Up @@ -978,9 +980,7 @@ $root.Package = (function() {
return Package;
})();

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

$protobuf.roots["test_package"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_package"] = $root;
6 changes: 2 additions & 4 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,7 @@ $root.MyResponse = (function() {
return MyResponse;
})();

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

$protobuf.roots["test_rpc"] = $root;

module.exports = $root;
module.exports = $protobuf.roots["test_rpc"] = $root;
Loading

0 comments on commit 1cc8a24

Please sign in to comment.