Skip to content

Add to existing protobuf namespace instead of overwriting it #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bench/data/static_pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut

var $root = $protobuf.roots.test_bench || ($protobuf.roots.test_bench = {});

$root.Test = (function() {
$root.Test = (function(Test) {

function Test(properties) {
if (properties)
Expand Down Expand Up @@ -62,7 +62,7 @@ $root.Test = (function() {
return message;
};

Test.Inner = (function() {
Test.Inner = (function(Inner) {

function Inner(properties) {
if (properties)
Expand Down Expand Up @@ -111,7 +111,7 @@ $root.Test = (function() {
return message;
};

Inner.InnerInner = (function() {
Inner.InnerInner = (function(InnerInner) {

function InnerInner(properties) {
if (properties)
Expand Down Expand Up @@ -161,10 +161,10 @@ $root.Test = (function() {
};

return InnerInner;
})();
})(Inner.InnerInner || {});

return Inner;
})();
})(Test.Inner || {});

Test.Enum = (function() {
var valuesById = {}, values = Object.create(valuesById);
Expand All @@ -177,9 +177,9 @@ $root.Test = (function() {
})();

return Test;
})();
})($root.Test || {});

$root.Outer = (function() {
$root.Outer = (function(Outer) {

function Outer(properties) {
this.bool = [];
Expand Down Expand Up @@ -235,6 +235,6 @@ $root.Outer = (function() {
};

return Outer;
})();
})($root.Outer || {});

module.exports = $root;
23 changes: 11 additions & 12 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,26 @@ function buildNamespace(ref, ns) {
if (!ns)
return;
if (ns.name !== "") {
if (!(ns instanceof Type) && !(ns instanceof Service)) {
push("");
pushComment([
ns.comment || "Namespace " + ns.name + ".",
ns.parent instanceof protobuf.Root ? "@exports " + escapeName(ns.name) : "@memberof " + exportName(ns.parent),
"@namespace"
]);
}
push("");
if (!ref && config.es6)
push("export const " + escapeName(ns.name) + " = " + escapeName(ref) + "." + escapeName(ns.name) + " = (() => {");
push("export const " + escapeName(ns.name) + " = " + escapeName(ref) + "." + escapeName(ns.name) + " = ((" + escapeName(ns.name) + ") => {");
else
push(escapeName(ref) + "." + escapeName(ns.name) + " = (function() {");
push(escapeName(ref) + "." + escapeName(ns.name) + " = (function(" + escapeName(ns.name) + ") {");
++indent;
}

if (ns instanceof Type) {
buildType(undefined, ns);
} else if (ns instanceof Service)
buildService(undefined, ns);
else if (ns.name !== "") {
push("");
pushComment([
ns.comment || "Namespace " + ns.name + ".",
ns.parent instanceof protobuf.Root ? "@exports " + escapeName(ns.name) : "@memberof " + exportName(ns.parent),
"@namespace"
]);
push((config.es6 ? "const" : "var") + " " + escapeName(ns.name) + " = {};");
}

ns.nestedArray.forEach(function(nested) {
if (nested instanceof Enum)
Expand All @@ -142,7 +141,7 @@ function buildNamespace(ref, ns) {
push("");
push("return " + escapeName(ns.name) + ";");
--indent;
push("})();");
push("})(" + escapeName(ref) + util.safeProp(escapeName(ns.name)) + " || {});");
}
}

Expand Down
21 changes: 16 additions & 5 deletions scripts/gentests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var fs = require("fs"),
[
{ file: "tests/data/comments.proto", flags: [] },
{ file: "tests/data/convert.proto", flags: [] },
{ file: "tests/data/same_package_1.proto", flags: [], defaultRoot: true, },
{ file: "tests/data/same_package_2.proto", flags: [], defaultRoot: true, },
{ file: "tests/data/mapbox/vector_tile.proto", flags: [] },
{ file: "tests/data/package.proto", flags: [] },
{ file: "tests/data/rpc.proto", flags: [ "es6" ] },
Expand All @@ -15,18 +17,27 @@ var fs = require("fs"),
{ file: "tests/data/test.proto", flags: [] },
{ file: "bench/data/bench.proto", flags: ["no-create", "no-verify", "no-delimited", "no-convert", "no-comments"], out: "bench/data/static_pbjs.js" }
]
.forEach(function({ file, flags, out }) {
.forEach(function({ file, flags, defaultRoot, out }) {
var basename = file.replace(/\.proto$/, "");
if (!out)
out = [ basename ].concat(flags).join("-") + ".js";
pbjs.main([

var cliArgs = [];
if (!defaultRoot) {
cliArgs = ["--root", "test_" + path.basename(basename, ".js")];
}

cliArgs = cliArgs.concat([
"--target", "static-module",
"--wrap", flags.includes('es6') ? 'es6' : "commonjs",
"--root", "test_" + path.basename(basename, ".js"),
file
].concat(flags.map(function(flag) {
]);

cliArgs = cliArgs.concat(flags.map(function(flag) {
return "--" + flag;
})), function(err, output) {
}));

pbjs.main(cliArgs, function(err, output) {
if (err)
throw err;
var pathToProtobufjs = path.relative(path.dirname(out), "minimal").replace(/\\/g, "/");
Expand Down
8 changes: 4 additions & 4 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut
// Exported root namespace
var $root = $protobuf.roots.test_comments || ($protobuf.roots.test_comments = {});

$root.Test1 = (function() {
$root.Test1 = (function(Test1) {

/**
* Properties of a Test1.
Expand Down Expand Up @@ -242,9 +242,9 @@ $root.Test1 = (function() {
};

return Test1;
})();
})($root.Test1 || {});

$root.Test2 = (function() {
$root.Test2 = (function(Test2) {

/**
* Properties of a Test2.
Expand Down Expand Up @@ -402,7 +402,7 @@ $root.Test2 = (function() {
};

return Test2;
})();
})($root.Test2 || {});

/**
* Test3 enum.
Expand Down
4 changes: 2 additions & 2 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut
// Exported root namespace
var $root = $protobuf.roots.test_convert || ($protobuf.roots.test_convert = {});

$root.Message = (function() {
$root.Message = (function(Message) {

/**
* Properties of a Message.
Expand Down Expand Up @@ -577,6 +577,6 @@ $root.Message = (function() {
})();

return Message;
})();
})($root.Message || {});

module.exports = $root;
31 changes: 15 additions & 16 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut
// Exported root namespace
var $root = $protobuf.roots.test_vector_tile || ($protobuf.roots.test_vector_tile = {});

$root.vector_tile = (function() {
/**
* Namespace vector_tile.
* @exports vector_tile
* @namespace
*/

/**
* Namespace vector_tile.
* @exports vector_tile
* @namespace
*/
var vector_tile = {};
$root.vector_tile = (function(vector_tile) {

vector_tile.Tile = (function() {
vector_tile.Tile = (function(Tile) {

/**
* Properties of a Tile.
Expand Down Expand Up @@ -241,7 +240,7 @@ $root.vector_tile = (function() {
return values;
})();

Tile.Value = (function() {
Tile.Value = (function(Value) {

/**
* Properties of a Value.
Expand Down Expand Up @@ -601,9 +600,9 @@ $root.vector_tile = (function() {
};

return Value;
})();
})(Tile.Value || {});

Tile.Feature = (function() {
Tile.Feature = (function(Feature) {

/**
* Properties of a Feature.
Expand Down Expand Up @@ -942,9 +941,9 @@ $root.vector_tile = (function() {
};

return Feature;
})();
})(Tile.Feature || {});

Tile.Layer = (function() {
Tile.Layer = (function(Layer) {

/**
* Properties of a Layer.
Expand Down Expand Up @@ -1300,12 +1299,12 @@ $root.vector_tile = (function() {
};

return Layer;
})();
})(Tile.Layer || {});

return Tile;
})();
})(vector_tile.Tile || {});

return vector_tile;
})();
})($root.vector_tile || {});

module.exports = $root;
8 changes: 4 additions & 4 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut
// Exported root namespace
var $root = $protobuf.roots.test_package || ($protobuf.roots.test_package = {});

$root.Package = (function() {
$root.Package = (function(Package) {

/**
* Properties of a Package.
Expand Down Expand Up @@ -724,7 +724,7 @@ $root.Package = (function() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};

Package.Repository = (function() {
Package.Repository = (function(Repository) {

/**
* Properties of a Repository.
Expand Down Expand Up @@ -932,9 +932,9 @@ $root.Package = (function() {
};

return Repository;
})();
})(Package.Repository || {});

return Package;
})();
})($root.Package || {});

module.exports = $root;
18 changes: 10 additions & 8 deletions tests/data/rpc-es6.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/
import * as $protobuf from "../../minimal";
"use strict";

var $protobuf = require("../../minimal");

// Common aliases
const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Exported root namespace
const $root = $protobuf.roots.test_rpc || ($protobuf.roots.test_rpc = {});

export const MyService = $root.MyService = (() => {
export const MyService = $root.MyService = ((MyService) => {

/**
* Constructs a new MyService service.
Expand Down Expand Up @@ -73,9 +75,9 @@ export const MyService = $root.MyService = (() => {
*/

return MyService;
})();
})($root.MyService || {});

export const MyRequest = $root.MyRequest = (() => {
export const MyRequest = $root.MyRequest = ((MyRequest) => {

/**
* Properties of a MyRequest.
Expand Down Expand Up @@ -260,9 +262,9 @@ export const MyRequest = $root.MyRequest = (() => {
};

return MyRequest;
})();
})($root.MyRequest || {});

export const MyResponse = $root.MyResponse = (() => {
export const MyResponse = $root.MyResponse = ((MyResponse) => {

/**
* Properties of a MyResponse.
Expand Down Expand Up @@ -447,6 +449,6 @@ export const MyResponse = $root.MyResponse = (() => {
};

return MyResponse;
})();
})($root.MyResponse || {});

export { $root as default };
module.exports = $root;
12 changes: 6 additions & 6 deletions tests/data/rpc-reserved.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut
// Exported root namespace
var $root = $protobuf.roots["test_rpc-reserved"] || ($protobuf.roots["test_rpc-reserved"] = {});

$root.MyService = (function() {
$root.MyService = (function(MyService) {

/**
* Constructs a new MyService service.
Expand Down Expand Up @@ -75,9 +75,9 @@ $root.MyService = (function() {
*/

return MyService;
})();
})($root.MyService || {});

$root.MyRequest = (function() {
$root.MyRequest = (function(MyRequest) {

/**
* Properties of a MyRequest.
Expand Down Expand Up @@ -262,9 +262,9 @@ $root.MyRequest = (function() {
};

return MyRequest;
})();
})($root.MyRequest || {});

$root.MyResponse = (function() {
$root.MyResponse = (function(MyResponse) {

/**
* Properties of a MyResponse.
Expand Down Expand Up @@ -449,6 +449,6 @@ $root.MyResponse = (function() {
};

return MyResponse;
})();
})($root.MyResponse || {});

module.exports = $root;
Loading