From ce0cae713ece9e3362bbb0ec136a3fb856f458b1 Mon Sep 17 00:00:00 2001 From: Stacey Sern Date: Mon, 3 Feb 2020 13:25:56 -0500 Subject: [PATCH 1/2] Add to existing namespace instead of overwriting it --- cli/targets/static.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/cli/targets/static.js b/cli/targets/static.js index d4a061f6e..1c1464a09 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -110,11 +110,19 @@ 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; } @@ -122,15 +130,6 @@ function buildNamespace(ref, ns) { 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) @@ -142,7 +141,7 @@ function buildNamespace(ref, ns) { push(""); push("return " + escapeName(ns.name) + ";"); --indent; - push("})();"); + push("})(" + escapeName(ref) + util.safeProp(escapeName(ns.name)) + " || {});"); } } From beab152bea8f8169c82af907b05b585620dd1e7b Mon Sep 17 00:00:00 2001 From: Taylor McIntyre Date: Fri, 16 Oct 2020 16:15:48 -0700 Subject: [PATCH 2/2] regenerate fixtures and add a test --- bench/data/static_pbjs.js | 16 +- scripts/gentests.js | 21 +- tests/data/comments.js | 8 +- tests/data/convert.js | 28 +- tests/data/mapbox/vector_tile.js | 31 +- tests/data/package.js | 98 ++++-- tests/data/rpc-es6.js | 18 +- tests/data/rpc-reserved.js | 12 +- tests/data/rpc.js | 12 +- tests/data/same_package_1.js | 183 +++++++++++ tests/data/same_package_1.proto | 5 + tests/data/same_package_2.js | 183 +++++++++++ tests/data/same_package_2.proto | 5 + tests/data/test.js | 524 ++++++++++++++++++++----------- tests/gen_same_package.js | 11 + 15 files changed, 887 insertions(+), 268 deletions(-) create mode 100644 tests/data/same_package_1.js create mode 100644 tests/data/same_package_1.proto create mode 100644 tests/data/same_package_2.js create mode 100644 tests/data/same_package_2.proto create mode 100644 tests/gen_same_package.js diff --git a/bench/data/static_pbjs.js b/bench/data/static_pbjs.js index b1bcfc8e2..8ca7607ef 100644 --- a/bench/data/static_pbjs.js +++ b/bench/data/static_pbjs.js @@ -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) @@ -62,7 +62,7 @@ $root.Test = (function() { return message; }; - Test.Inner = (function() { + Test.Inner = (function(Inner) { function Inner(properties) { if (properties) @@ -111,7 +111,7 @@ $root.Test = (function() { return message; }; - Inner.InnerInner = (function() { + Inner.InnerInner = (function(InnerInner) { function InnerInner(properties) { if (properties) @@ -161,10 +161,10 @@ $root.Test = (function() { }; return InnerInner; - })(); + })(Inner.InnerInner || {}); return Inner; - })(); + })(Test.Inner || {}); Test.Enum = (function() { var valuesById = {}, values = Object.create(valuesById); @@ -177,9 +177,9 @@ $root.Test = (function() { })(); return Test; -})(); +})($root.Test || {}); -$root.Outer = (function() { +$root.Outer = (function(Outer) { function Outer(properties) { this.bool = []; @@ -235,6 +235,6 @@ $root.Outer = (function() { }; return Outer; -})(); +})($root.Outer || {}); module.exports = $root; diff --git a/scripts/gentests.js b/scripts/gentests.js index a635a8923..5660de725 100644 --- a/scripts/gentests.js +++ b/scripts/gentests.js @@ -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" ] }, @@ -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", "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, "/"); diff --git a/tests/data/comments.js b/tests/data/comments.js index fa9c1750d..8633b62f1 100644 --- a/tests/data/comments.js +++ b/tests/data/comments.js @@ -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. @@ -242,9 +242,9 @@ $root.Test1 = (function() { }; return Test1; -})(); +})($root.Test1 || {}); -$root.Test2 = (function() { +$root.Test2 = (function(Test2) { /** * Properties of a Test2. @@ -402,7 +402,7 @@ $root.Test2 = (function() { }; return Test2; -})(); +})($root.Test2 || {}); /** * Test3 enum. diff --git a/tests/data/convert.js b/tests/data/convert.js index 27fd285d3..cacd7fabc 100644 --- a/tests/data/convert.js +++ b/tests/data/convert.js @@ -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. @@ -201,7 +201,7 @@ $root.Message = (function() { Message.decode = function decode(reader, length) { if (!(reader instanceof $Reader)) reader = $Reader.create(reader); - var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Message(), key; + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Message(), key, value; while (reader.pos < end) { var tag = reader.uint32(); switch (tag >>> 3) { @@ -248,12 +248,26 @@ $root.Message = (function() { message.enumRepeated.push(reader.int32()); break; case 9: - reader.skip().pos++; if (message.int64Map === $util.emptyObject) message.int64Map = {}; - key = reader.string(); - reader.pos++; - message.int64Map[key] = reader.int64(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = 0; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.int64(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.int64Map[key] = value; break; default: reader.skipType(tag & 7); @@ -563,6 +577,6 @@ $root.Message = (function() { })(); return Message; -})(); +})($root.Message || {}); module.exports = $root; diff --git a/tests/data/mapbox/vector_tile.js b/tests/data/mapbox/vector_tile.js index ac2c1f7b7..6036fb91a 100644 --- a/tests/data/mapbox/vector_tile.js +++ b/tests/data/mapbox/vector_tile.js @@ -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. @@ -241,7 +240,7 @@ $root.vector_tile = (function() { return values; })(); - Tile.Value = (function() { + Tile.Value = (function(Value) { /** * Properties of a Value. @@ -601,9 +600,9 @@ $root.vector_tile = (function() { }; return Value; - })(); + })(Tile.Value || {}); - Tile.Feature = (function() { + Tile.Feature = (function(Feature) { /** * Properties of a Feature. @@ -942,9 +941,9 @@ $root.vector_tile = (function() { }; return Feature; - })(); + })(Tile.Feature || {}); - Tile.Layer = (function() { + Tile.Layer = (function(Layer) { /** * Properties of a Layer. @@ -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; diff --git a/tests/data/package.js b/tests/data/package.js index 4fdda1b8f..faa2b6e67 100644 --- a/tests/data/package.js +++ b/tests/data/package.js @@ -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. @@ -285,7 +285,7 @@ $root.Package = (function() { Package.decode = function decode(reader, length) { if (!(reader instanceof $Reader)) reader = $Reader.create(reader); - var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Package(), key; + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Package(), key, value; while (reader.pos < end) { var tag = reader.uint32(); switch (tag >>> 3) { @@ -325,36 +325,92 @@ $root.Package = (function() { message.main = reader.string(); break; case 11: - reader.skip().pos++; if (message.bin === $util.emptyObject) message.bin = {}; - key = reader.string(); - reader.pos++; - message.bin[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.bin[key] = value; break; case 12: - reader.skip().pos++; if (message.scripts === $util.emptyObject) message.scripts = {}; - key = reader.string(); - reader.pos++; - message.scripts[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.scripts[key] = value; break; case 13: - reader.skip().pos++; if (message.dependencies === $util.emptyObject) message.dependencies = {}; - key = reader.string(); - reader.pos++; - message.dependencies[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.dependencies[key] = value; break; case 15: - reader.skip().pos++; if (message.devDependencies === $util.emptyObject) message.devDependencies = {}; - key = reader.string(); - reader.pos++; - message.devDependencies[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.devDependencies[key] = value; break; case 17: message.types = reader.string(); @@ -668,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. @@ -876,9 +932,9 @@ $root.Package = (function() { }; return Repository; - })(); + })(Package.Repository || {}); return Package; -})(); +})($root.Package || {}); module.exports = $root; diff --git a/tests/data/rpc-es6.js b/tests/data/rpc-es6.js index be0e37c04..c277dbb18 100644 --- a/tests/data/rpc-es6.js +++ b/tests/data/rpc-es6.js @@ -1,5 +1,7 @@ /*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; @@ -7,7 +9,7 @@ const $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf. // 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. @@ -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. @@ -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. @@ -447,6 +449,6 @@ export const MyResponse = $root.MyResponse = (() => { }; return MyResponse; -})(); +})($root.MyResponse || {}); -export { $root as default }; +module.exports = $root; diff --git a/tests/data/rpc-reserved.js b/tests/data/rpc-reserved.js index ef555698c..b7a714c2f 100644 --- a/tests/data/rpc-reserved.js +++ b/tests/data/rpc-reserved.js @@ -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. @@ -75,9 +75,9 @@ $root.MyService = (function() { */ return MyService; -})(); +})($root.MyService || {}); -$root.MyRequest = (function() { +$root.MyRequest = (function(MyRequest) { /** * Properties of a MyRequest. @@ -262,9 +262,9 @@ $root.MyRequest = (function() { }; return MyRequest; -})(); +})($root.MyRequest || {}); -$root.MyResponse = (function() { +$root.MyResponse = (function(MyResponse) { /** * Properties of a MyResponse. @@ -449,6 +449,6 @@ $root.MyResponse = (function() { }; return MyResponse; -})(); +})($root.MyResponse || {}); module.exports = $root; diff --git a/tests/data/rpc.js b/tests/data/rpc.js index bc493aa4f..ea21edd95 100644 --- a/tests/data/rpc.js +++ b/tests/data/rpc.js @@ -9,7 +9,7 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut // Exported root namespace var $root = $protobuf.roots.test_rpc || ($protobuf.roots.test_rpc = {}); -$root.MyService = (function() { +$root.MyService = (function(MyService) { /** * Constructs a new MyService service. @@ -75,9 +75,9 @@ $root.MyService = (function() { */ return MyService; -})(); +})($root.MyService || {}); -$root.MyRequest = (function() { +$root.MyRequest = (function(MyRequest) { /** * Properties of a MyRequest. @@ -262,9 +262,9 @@ $root.MyRequest = (function() { }; return MyRequest; -})(); +})($root.MyRequest || {}); -$root.MyResponse = (function() { +$root.MyResponse = (function(MyResponse) { /** * Properties of a MyResponse. @@ -449,6 +449,6 @@ $root.MyResponse = (function() { }; return MyResponse; -})(); +})($root.MyResponse || {}); module.exports = $root; diff --git a/tests/data/same_package_1.js b/tests/data/same_package_1.js new file mode 100644 index 000000000..e95288dbf --- /dev/null +++ b/tests/data/same_package_1.js @@ -0,0 +1,183 @@ +/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/ +"use strict"; + +var $protobuf = require("../../minimal"); + +// Common aliases +var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; + +// Exported root namespace +var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {}); + +/** + * Namespace some_pkg. + * @exports some_pkg + * @namespace + */ + +$root.some_pkg = (function(some_pkg) { + + some_pkg.MessageA = (function(MessageA) { + + /** + * Properties of a MessageA. + * @memberof some_pkg + * @interface IMessageA + */ + + /** + * Constructs a new MessageA. + * @memberof some_pkg + * @classdesc Represents a MessageA. + * @implements IMessageA + * @constructor + * @param {some_pkg.IMessageA=} [properties] Properties to set + */ + function MessageA(properties) { + if (properties) + for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) + if (properties[keys[i]] != null) + this[keys[i]] = properties[keys[i]]; + } + + /** + * Creates a new MessageA instance using the specified properties. + * @function create + * @memberof some_pkg.MessageA + * @static + * @param {some_pkg.IMessageA=} [properties] Properties to set + * @returns {some_pkg.MessageA} MessageA instance + */ + MessageA.create = function create(properties) { + return new MessageA(properties); + }; + + /** + * Encodes the specified MessageA message. Does not implicitly {@link some_pkg.MessageA.verify|verify} messages. + * @function encode + * @memberof some_pkg.MessageA + * @static + * @param {some_pkg.IMessageA} message MessageA message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MessageA.encode = function encode(message, writer) { + if (!writer) + writer = $Writer.create(); + return writer; + }; + + /** + * Encodes the specified MessageA message, length delimited. Does not implicitly {@link some_pkg.MessageA.verify|verify} messages. + * @function encodeDelimited + * @memberof some_pkg.MessageA + * @static + * @param {some_pkg.IMessageA} message MessageA message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MessageA.encodeDelimited = function encodeDelimited(message, writer) { + return this.encode(message, writer).ldelim(); + }; + + /** + * Decodes a MessageA message from the specified reader or buffer. + * @function decode + * @memberof some_pkg.MessageA + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @param {number} [length] Message length if known beforehand + * @returns {some_pkg.MessageA} MessageA + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MessageA.decode = function decode(reader, length) { + if (!(reader instanceof $Reader)) + reader = $Reader.create(reader); + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.some_pkg.MessageA(); + while (reader.pos < end) { + var tag = reader.uint32(); + switch (tag >>> 3) { + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }; + + /** + * Decodes a MessageA message from the specified reader or buffer, length delimited. + * @function decodeDelimited + * @memberof some_pkg.MessageA + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @returns {some_pkg.MessageA} MessageA + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MessageA.decodeDelimited = function decodeDelimited(reader) { + if (!(reader instanceof $Reader)) + reader = new $Reader(reader); + return this.decode(reader, reader.uint32()); + }; + + /** + * Verifies a MessageA message. + * @function verify + * @memberof some_pkg.MessageA + * @static + * @param {Object.} message Plain object to verify + * @returns {string|null} `null` if valid, otherwise the reason why it is not + */ + MessageA.verify = function verify(message) { + if (typeof message !== "object" || message === null) + return "object expected"; + return null; + }; + + /** + * Creates a MessageA message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof some_pkg.MessageA + * @static + * @param {Object.} object Plain object + * @returns {some_pkg.MessageA} MessageA + */ + MessageA.fromObject = function fromObject(object) { + if (object instanceof $root.some_pkg.MessageA) + return object; + return new $root.some_pkg.MessageA(); + }; + + /** + * Creates a plain object from a MessageA message. Also converts values to other types if specified. + * @function toObject + * @memberof some_pkg.MessageA + * @static + * @param {some_pkg.MessageA} message MessageA + * @param {$protobuf.IConversionOptions} [options] Conversion options + * @returns {Object.} Plain object + */ + MessageA.toObject = function toObject() { + return {}; + }; + + /** + * Converts this MessageA to JSON. + * @function toJSON + * @memberof some_pkg.MessageA + * @instance + * @returns {Object.} JSON object + */ + MessageA.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return MessageA; + })(some_pkg.MessageA || {}); + + return some_pkg; +})($root.some_pkg || {}); + +module.exports = $root; diff --git a/tests/data/same_package_1.proto b/tests/data/same_package_1.proto new file mode 100644 index 000000000..02463f403 --- /dev/null +++ b/tests/data/same_package_1.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; +package some_pkg; + +message MessageA { +} diff --git a/tests/data/same_package_2.js b/tests/data/same_package_2.js new file mode 100644 index 000000000..479fa6fdf --- /dev/null +++ b/tests/data/same_package_2.js @@ -0,0 +1,183 @@ +/*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/ +"use strict"; + +var $protobuf = require("../../minimal"); + +// Common aliases +var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; + +// Exported root namespace +var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {}); + +/** + * Namespace some_pkg. + * @exports some_pkg + * @namespace + */ + +$root.some_pkg = (function(some_pkg) { + + some_pkg.MessageB = (function(MessageB) { + + /** + * Properties of a MessageB. + * @memberof some_pkg + * @interface IMessageB + */ + + /** + * Constructs a new MessageB. + * @memberof some_pkg + * @classdesc Represents a MessageB. + * @implements IMessageB + * @constructor + * @param {some_pkg.IMessageB=} [properties] Properties to set + */ + function MessageB(properties) { + if (properties) + for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) + if (properties[keys[i]] != null) + this[keys[i]] = properties[keys[i]]; + } + + /** + * Creates a new MessageB instance using the specified properties. + * @function create + * @memberof some_pkg.MessageB + * @static + * @param {some_pkg.IMessageB=} [properties] Properties to set + * @returns {some_pkg.MessageB} MessageB instance + */ + MessageB.create = function create(properties) { + return new MessageB(properties); + }; + + /** + * Encodes the specified MessageB message. Does not implicitly {@link some_pkg.MessageB.verify|verify} messages. + * @function encode + * @memberof some_pkg.MessageB + * @static + * @param {some_pkg.IMessageB} message MessageB message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MessageB.encode = function encode(message, writer) { + if (!writer) + writer = $Writer.create(); + return writer; + }; + + /** + * Encodes the specified MessageB message, length delimited. Does not implicitly {@link some_pkg.MessageB.verify|verify} messages. + * @function encodeDelimited + * @memberof some_pkg.MessageB + * @static + * @param {some_pkg.IMessageB} message MessageB message or plain object to encode + * @param {$protobuf.Writer} [writer] Writer to encode to + * @returns {$protobuf.Writer} Writer + */ + MessageB.encodeDelimited = function encodeDelimited(message, writer) { + return this.encode(message, writer).ldelim(); + }; + + /** + * Decodes a MessageB message from the specified reader or buffer. + * @function decode + * @memberof some_pkg.MessageB + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @param {number} [length] Message length if known beforehand + * @returns {some_pkg.MessageB} MessageB + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MessageB.decode = function decode(reader, length) { + if (!(reader instanceof $Reader)) + reader = $Reader.create(reader); + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.some_pkg.MessageB(); + while (reader.pos < end) { + var tag = reader.uint32(); + switch (tag >>> 3) { + default: + reader.skipType(tag & 7); + break; + } + } + return message; + }; + + /** + * Decodes a MessageB message from the specified reader or buffer, length delimited. + * @function decodeDelimited + * @memberof some_pkg.MessageB + * @static + * @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from + * @returns {some_pkg.MessageB} MessageB + * @throws {Error} If the payload is not a reader or valid buffer + * @throws {$protobuf.util.ProtocolError} If required fields are missing + */ + MessageB.decodeDelimited = function decodeDelimited(reader) { + if (!(reader instanceof $Reader)) + reader = new $Reader(reader); + return this.decode(reader, reader.uint32()); + }; + + /** + * Verifies a MessageB message. + * @function verify + * @memberof some_pkg.MessageB + * @static + * @param {Object.} message Plain object to verify + * @returns {string|null} `null` if valid, otherwise the reason why it is not + */ + MessageB.verify = function verify(message) { + if (typeof message !== "object" || message === null) + return "object expected"; + return null; + }; + + /** + * Creates a MessageB message from a plain object. Also converts values to their respective internal types. + * @function fromObject + * @memberof some_pkg.MessageB + * @static + * @param {Object.} object Plain object + * @returns {some_pkg.MessageB} MessageB + */ + MessageB.fromObject = function fromObject(object) { + if (object instanceof $root.some_pkg.MessageB) + return object; + return new $root.some_pkg.MessageB(); + }; + + /** + * Creates a plain object from a MessageB message. Also converts values to other types if specified. + * @function toObject + * @memberof some_pkg.MessageB + * @static + * @param {some_pkg.MessageB} message MessageB + * @param {$protobuf.IConversionOptions} [options] Conversion options + * @returns {Object.} Plain object + */ + MessageB.toObject = function toObject() { + return {}; + }; + + /** + * Converts this MessageB to JSON. + * @function toJSON + * @memberof some_pkg.MessageB + * @instance + * @returns {Object.} JSON object + */ + MessageB.prototype.toJSON = function toJSON() { + return this.constructor.toObject(this, $protobuf.util.toJSONOptions); + }; + + return MessageB; + })(some_pkg.MessageB || {}); + + return some_pkg; +})($root.some_pkg || {}); + +module.exports = $root; diff --git a/tests/data/same_package_2.proto b/tests/data/same_package_2.proto new file mode 100644 index 000000000..f43b66038 --- /dev/null +++ b/tests/data/same_package_2.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; +package some_pkg; + +message MessageB { +} diff --git a/tests/data/test.js b/tests/data/test.js index 00fb41c9f..de874a30b 100644 --- a/tests/data/test.js +++ b/tests/data/test.js @@ -9,25 +9,23 @@ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.ut // Exported root namespace var $root = $protobuf.roots.test_test || ($protobuf.roots.test_test = {}); -$root.jspb = (function() { +/** + * Namespace jspb. + * @exports jspb + * @namespace + */ + +$root.jspb = (function(jspb) { /** - * Namespace jspb. - * @exports jspb + * Namespace test. + * @memberof jspb * @namespace */ - var jspb = {}; - jspb.test = (function() { + jspb.test = (function(test) { - /** - * Namespace test. - * @memberof jspb - * @namespace - */ - var test = {}; - - test.Empty = (function() { + test.Empty = (function(Empty) { /** * Properties of an Empty. @@ -185,7 +183,7 @@ $root.jspb = (function() { }; return Empty; - })(); + })(test.Empty || {}); /** * OuterEnum enum. @@ -201,7 +199,7 @@ $root.jspb = (function() { return values; })(); - test.EnumContainer = (function() { + test.EnumContainer = (function(EnumContainer) { /** * Properties of an EnumContainer. @@ -399,9 +397,9 @@ $root.jspb = (function() { }; return EnumContainer; - })(); + })(test.EnumContainer || {}); - test.Simple1 = (function() { + test.Simple1 = (function(Simple1) { /** * Properties of a Simple1. @@ -648,9 +646,9 @@ $root.jspb = (function() { }; return Simple1; - })(); + })(test.Simple1 || {}); - test.Simple2 = (function() { + test.Simple2 = (function(Simple2) { /** * Properties of a Simple2. @@ -874,9 +872,9 @@ $root.jspb = (function() { }; return Simple2; - })(); + })(test.Simple2 || {}); - test.SpecialCases = (function() { + test.SpecialCases = (function(SpecialCases) { /** * Properties of a SpecialCases. @@ -1128,9 +1126,9 @@ $root.jspb = (function() { }; return SpecialCases; - })(); + })(test.SpecialCases || {}); - test.OptionalFields = (function() { + test.OptionalFields = (function(OptionalFields) { /** * Properties of an OptionalFields. @@ -1447,7 +1445,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - OptionalFields.Nested = (function() { + OptionalFields.Nested = (function(Nested) { /** * Properties of a Nested. @@ -1632,12 +1630,12 @@ $root.jspb = (function() { }; return Nested; - })(); + })(OptionalFields.Nested || {}); return OptionalFields; - })(); + })(test.OptionalFields || {}); - test.HasExtensions = (function() { + test.HasExtensions = (function(HasExtensions) { /** * Properties of a HasExtensions. @@ -2053,9 +2051,9 @@ $root.jspb = (function() { }; return HasExtensions; - })(); + })(test.HasExtensions || {}); - test.Complex = (function() { + test.Complex = (function(Complex) { /** * Properties of a Complex. @@ -2372,7 +2370,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - Complex.Nested = (function() { + Complex.Nested = (function(Nested) { /** * Properties of a Nested. @@ -2557,12 +2555,12 @@ $root.jspb = (function() { }; return Nested; - })(); + })(Complex.Nested || {}); return Complex; - })(); + })(test.Complex || {}); - test.OuterMessage = (function() { + test.OuterMessage = (function(OuterMessage) { /** * Properties of an OuterMessage. @@ -2719,7 +2717,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - OuterMessage.Complex = (function() { + OuterMessage.Complex = (function(Complex) { /** * Properties of a Complex. @@ -2904,12 +2902,12 @@ $root.jspb = (function() { }; return Complex; - })(); + })(OuterMessage.Complex || {}); return OuterMessage; - })(); + })(test.OuterMessage || {}); - test.IsExtension = (function() { + test.IsExtension = (function(IsExtension) { /** * Properties of an IsExtension. @@ -3094,9 +3092,9 @@ $root.jspb = (function() { }; return IsExtension; - })(); + })(test.IsExtension || {}); - test.IndirectExtension = (function() { + test.IndirectExtension = (function(IndirectExtension) { /** * Properties of an IndirectExtension. @@ -3254,9 +3252,9 @@ $root.jspb = (function() { }; return IndirectExtension; - })(); + })(test.IndirectExtension || {}); - test.DefaultValues = (function() { + test.DefaultValues = (function(DefaultValues) { /** * Properties of a DefaultValues. @@ -3606,9 +3604,9 @@ $root.jspb = (function() { })(); return DefaultValues; - })(); + })(test.DefaultValues || {}); - test.FloatingPointFields = (function() { + test.FloatingPointFields = (function(FloatingPointFields) { /** * Properties of a FloatingPointFields. @@ -3992,9 +3990,9 @@ $root.jspb = (function() { }; return FloatingPointFields; - })(); + })(test.FloatingPointFields || {}); - test.TestClone = (function() { + test.TestClone = (function(TestClone) { /** * Properties of a TestClone. @@ -4331,9 +4329,9 @@ $root.jspb = (function() { }; return TestClone; - })(); + })(test.TestClone || {}); - test.CloneExtension = (function() { + test.CloneExtension = (function(CloneExtension) { /** * Properties of a CloneExtension. @@ -4518,9 +4516,9 @@ $root.jspb = (function() { }; return CloneExtension; - })(); + })(test.CloneExtension || {}); - test.TestGroup = (function() { + test.TestGroup = (function(TestGroup) { /** * Properties of a TestGroup. @@ -4859,7 +4857,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - TestGroup.RepeatedGroup = (function() { + TestGroup.RepeatedGroup = (function(RepeatedGroup) { /** * Properties of a RepeatedGroup. @@ -5090,9 +5088,9 @@ $root.jspb = (function() { }; return RepeatedGroup; - })(); + })(TestGroup.RepeatedGroup || {}); - TestGroup.RequiredGroup = (function() { + TestGroup.RequiredGroup = (function(RequiredGroup) { /** * Properties of a RequiredGroup. @@ -5279,9 +5277,9 @@ $root.jspb = (function() { }; return RequiredGroup; - })(); + })(TestGroup.RequiredGroup || {}); - TestGroup.OptionalGroup = (function() { + TestGroup.OptionalGroup = (function(OptionalGroup) { /** * Properties of an OptionalGroup. @@ -5468,12 +5466,12 @@ $root.jspb = (function() { }; return OptionalGroup; - })(); + })(TestGroup.OptionalGroup || {}); return TestGroup; - })(); + })(test.TestGroup || {}); - test.TestGroup1 = (function() { + test.TestGroup1 = (function(TestGroup1) { /** * Properties of a TestGroup1. @@ -5663,9 +5661,9 @@ $root.jspb = (function() { }; return TestGroup1; - })(); + })(test.TestGroup1 || {}); - test.TestReservedNames = (function() { + test.TestReservedNames = (function(TestReservedNames) { /** * Properties of a TestReservedNames. @@ -5873,9 +5871,9 @@ $root.jspb = (function() { }; return TestReservedNames; - })(); + })(test.TestReservedNames || {}); - test.TestReservedNamesExtension = (function() { + test.TestReservedNamesExtension = (function(TestReservedNamesExtension) { /** * Properties of a TestReservedNamesExtension. @@ -6033,9 +6031,9 @@ $root.jspb = (function() { }; return TestReservedNamesExtension; - })(); + })(test.TestReservedNamesExtension || {}); - test.TestMessageWithOneof = (function() { + test.TestMessageWithOneof = (function(TestMessageWithOneof) { /** * Properties of a TestMessageWithOneof. @@ -6529,9 +6527,9 @@ $root.jspb = (function() { }; return TestMessageWithOneof; - })(); + })(test.TestMessageWithOneof || {}); - test.TestEndsWithBytes = (function() { + test.TestEndsWithBytes = (function(TestEndsWithBytes) { /** * Properties of a TestEndsWithBytes. @@ -6748,9 +6746,9 @@ $root.jspb = (function() { }; return TestEndsWithBytes; - })(); + })(test.TestEndsWithBytes || {}); - test.TestMapFieldsNoBinary = (function() { + test.TestMapFieldsNoBinary = (function(TestMapFieldsNoBinary) { /** * Properties of a TestMapFieldsNoBinary. @@ -6985,100 +6983,254 @@ $root.jspb = (function() { TestMapFieldsNoBinary.decode = function decode(reader, length) { if (!(reader instanceof $Reader)) reader = $Reader.create(reader); - var end = length === undefined ? reader.len : reader.pos + length, message = new $root.jspb.test.TestMapFieldsNoBinary(), key; + var end = length === undefined ? reader.len : reader.pos + length, message = new $root.jspb.test.TestMapFieldsNoBinary(), key, value; while (reader.pos < end) { var tag = reader.uint32(); switch (tag >>> 3) { case 1: - reader.skip().pos++; if (message.mapStringString === $util.emptyObject) message.mapStringString = {}; - key = reader.string(); - reader.pos++; - message.mapStringString[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringString[key] = value; break; case 2: - reader.skip().pos++; if (message.mapStringInt32 === $util.emptyObject) message.mapStringInt32 = {}; - key = reader.string(); - reader.pos++; - message.mapStringInt32[key] = reader.int32(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = 0; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.int32(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringInt32[key] = value; break; case 3: - reader.skip().pos++; if (message.mapStringInt64 === $util.emptyObject) message.mapStringInt64 = {}; - key = reader.string(); - reader.pos++; - message.mapStringInt64[key] = reader.int64(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = 0; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.int64(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringInt64[key] = value; break; case 4: - reader.skip().pos++; if (message.mapStringBool === $util.emptyObject) message.mapStringBool = {}; - key = reader.string(); - reader.pos++; - message.mapStringBool[key] = reader.bool(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = false; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.bool(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringBool[key] = value; break; case 5: - reader.skip().pos++; if (message.mapStringDouble === $util.emptyObject) message.mapStringDouble = {}; - key = reader.string(); - reader.pos++; - message.mapStringDouble[key] = reader.double(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = 0; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.double(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringDouble[key] = value; break; case 6: - reader.skip().pos++; if (message.mapStringEnum === $util.emptyObject) message.mapStringEnum = {}; - key = reader.string(); - reader.pos++; - message.mapStringEnum[key] = reader.int32(); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = 0; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = reader.int32(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringEnum[key] = value; break; case 7: - reader.skip().pos++; if (message.mapStringMsg === $util.emptyObject) message.mapStringMsg = {}; - key = reader.string(); - reader.pos++; - message.mapStringMsg[key] = $root.jspb.test.MapValueMessageNoBinary.decode(reader, reader.uint32()); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = null; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = $root.jspb.test.MapValueMessageNoBinary.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringMsg[key] = value; break; case 8: - reader.skip().pos++; if (message.mapInt32String === $util.emptyObject) message.mapInt32String = {}; - key = reader.int32(); - reader.pos++; - message.mapInt32String[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = 0; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.int32(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapInt32String[key] = value; break; case 9: - reader.skip().pos++; if (message.mapInt64String === $util.emptyObject) message.mapInt64String = {}; - key = reader.int64(); - reader.pos++; - message.mapInt64String[typeof key === "object" ? $util.longToHash(key) : key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = 0; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.int64(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapInt64String[typeof key === "object" ? $util.longToHash(key) : key] = value; break; case 10: - reader.skip().pos++; if (message.mapBoolString === $util.emptyObject) message.mapBoolString = {}; - key = reader.bool(); - reader.pos++; - message.mapBoolString[key] = reader.string(); + var end2 = reader.uint32() + reader.pos; + key = false; + value = ""; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.bool(); + break; + case 2: + value = reader.string(); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapBoolString[key] = value; break; case 11: message.testMapFields = $root.jspb.test.TestMapFieldsNoBinary.decode(reader, reader.uint32()); break; case 12: - reader.skip().pos++; if (message.mapStringTestmapfields === $util.emptyObject) message.mapStringTestmapfields = {}; - key = reader.string(); - reader.pos++; - message.mapStringTestmapfields[key] = $root.jspb.test.TestMapFieldsNoBinary.decode(reader, reader.uint32()); + var end2 = reader.uint32() + reader.pos; + key = ""; + value = null; + while (reader.pos < end2) { + var tag2 = reader.uint32(); + switch (tag2 >>> 3) { + case 1: + key = reader.string(); + break; + case 2: + value = $root.jspb.test.TestMapFieldsNoBinary.decode(reader, reader.uint32()); + break; + default: + reader.skipType(tag2 & 7); + break; + } + } + message.mapStringTestmapfields[key] = value; break; default: reader.skipType(tag & 7); @@ -7457,7 +7609,7 @@ $root.jspb = (function() { }; return TestMapFieldsNoBinary; - })(); + })(test.TestMapFieldsNoBinary || {}); /** * MapValueEnumNoBinary enum. @@ -7475,7 +7627,7 @@ $root.jspb = (function() { return values; })(); - test.MapValueMessageNoBinary = (function() { + test.MapValueMessageNoBinary = (function(MapValueMessageNoBinary) { /** * Properties of a MapValueMessageNoBinary. @@ -7660,9 +7812,9 @@ $root.jspb = (function() { }; return MapValueMessageNoBinary; - })(); + })(test.MapValueMessageNoBinary || {}); - test.Deeply = (function() { + test.Deeply = (function(Deeply) { /** * Properties of a Deeply. @@ -7819,7 +7971,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - Deeply.Nested = (function() { + Deeply.Nested = (function(Nested) { /** * Properties of a Nested. @@ -7976,7 +8128,7 @@ $root.jspb = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - Nested.Message = (function() { + Nested.Message = (function(Message) { /** * Properties of a Message. @@ -8161,39 +8313,37 @@ $root.jspb = (function() { }; return Message; - })(); + })(Nested.Message || {}); return Nested; - })(); + })(Deeply.Nested || {}); return Deeply; - })(); + })(test.Deeply || {}); return test; - })(); + })(jspb.test || {}); return jspb; -})(); +})($root.jspb || {}); + +/** + * Namespace google. + * @exports google + * @namespace + */ -$root.google = (function() { +$root.google = (function(google) { /** - * Namespace google. - * @exports google + * Namespace protobuf. + * @memberof google * @namespace */ - var google = {}; - google.protobuf = (function() { - - /** - * Namespace protobuf. - * @memberof google - * @namespace - */ - var protobuf = {}; + google.protobuf = (function(protobuf) { - protobuf.FileDescriptorSet = (function() { + protobuf.FileDescriptorSet = (function(FileDescriptorSet) { /** * Properties of a FileDescriptorSet. @@ -8399,9 +8549,9 @@ $root.google = (function() { }; return FileDescriptorSet; - })(); + })(protobuf.FileDescriptorSet || {}); - protobuf.FileDescriptorProto = (function() { + protobuf.FileDescriptorProto = (function(FileDescriptorProto) { /** * Properties of a FileDescriptorProto. @@ -8983,9 +9133,9 @@ $root.google = (function() { }; return FileDescriptorProto; - })(); + })(protobuf.FileDescriptorProto || {}); - protobuf.DescriptorProto = (function() { + protobuf.DescriptorProto = (function(DescriptorProto) { /** * Properties of a DescriptorProto. @@ -9538,7 +9688,7 @@ $root.google = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - DescriptorProto.ExtensionRange = (function() { + DescriptorProto.ExtensionRange = (function(ExtensionRange) { /** * Properties of an ExtensionRange. @@ -9746,9 +9896,9 @@ $root.google = (function() { }; return ExtensionRange; - })(); + })(DescriptorProto.ExtensionRange || {}); - DescriptorProto.ReservedRange = (function() { + DescriptorProto.ReservedRange = (function(ReservedRange) { /** * Properties of a ReservedRange. @@ -9956,12 +10106,12 @@ $root.google = (function() { }; return ReservedRange; - })(); + })(DescriptorProto.ReservedRange || {}); return DescriptorProto; - })(); + })(protobuf.DescriptorProto || {}); - protobuf.FieldDescriptorProto = (function() { + protobuf.FieldDescriptorProto = (function(FieldDescriptorProto) { /** * Properties of a FieldDescriptorProto. @@ -10523,9 +10673,9 @@ $root.google = (function() { })(); return FieldDescriptorProto; - })(); + })(protobuf.FieldDescriptorProto || {}); - protobuf.OneofDescriptorProto = (function() { + protobuf.OneofDescriptorProto = (function(OneofDescriptorProto) { /** * Properties of an OneofDescriptorProto. @@ -10738,9 +10888,9 @@ $root.google = (function() { }; return OneofDescriptorProto; - })(); + })(protobuf.OneofDescriptorProto || {}); - protobuf.EnumDescriptorProto = (function() { + protobuf.EnumDescriptorProto = (function(EnumDescriptorProto) { /** * Properties of an EnumDescriptorProto. @@ -10997,9 +11147,9 @@ $root.google = (function() { }; return EnumDescriptorProto; - })(); + })(protobuf.EnumDescriptorProto || {}); - protobuf.EnumValueDescriptorProto = (function() { + protobuf.EnumValueDescriptorProto = (function(EnumValueDescriptorProto) { /** * Properties of an EnumValueDescriptorProto. @@ -11234,9 +11384,9 @@ $root.google = (function() { }; return EnumValueDescriptorProto; - })(); + })(protobuf.EnumValueDescriptorProto || {}); - protobuf.ServiceDescriptorProto = (function() { + protobuf.ServiceDescriptorProto = (function(ServiceDescriptorProto) { /** * Properties of a ServiceDescriptorProto. @@ -11493,9 +11643,9 @@ $root.google = (function() { }; return ServiceDescriptorProto; - })(); + })(protobuf.ServiceDescriptorProto || {}); - protobuf.MethodDescriptorProto = (function() { + protobuf.MethodDescriptorProto = (function(MethodDescriptorProto) { /** * Properties of a MethodDescriptorProto. @@ -11796,9 +11946,9 @@ $root.google = (function() { }; return MethodDescriptorProto; - })(); + })(protobuf.MethodDescriptorProto || {}); - protobuf.FileOptions = (function() { + protobuf.FileOptions = (function(FileOptions) { /** * Properties of a FileOptions. @@ -12348,9 +12498,9 @@ $root.google = (function() { })(); return FileOptions; - })(); + })(protobuf.FileOptions || {}); - protobuf.MessageOptions = (function() { + protobuf.MessageOptions = (function(MessageOptions) { /** * Properties of a MessageOptions. @@ -12646,9 +12796,9 @@ $root.google = (function() { }; return MessageOptions; - })(); + })(protobuf.MessageOptions || {}); - protobuf.FieldOptions = (function() { + protobuf.FieldOptions = (function(FieldOptions) { /** * Properties of a FieldOptions. @@ -13056,9 +13206,9 @@ $root.google = (function() { })(); return FieldOptions; - })(); + })(protobuf.FieldOptions || {}); - protobuf.OneofOptions = (function() { + protobuf.OneofOptions = (function(OneofOptions) { /** * Properties of an OneofOptions. @@ -13264,9 +13414,9 @@ $root.google = (function() { }; return OneofOptions; - })(); + })(protobuf.OneofOptions || {}); - protobuf.EnumOptions = (function() { + protobuf.EnumOptions = (function(EnumOptions) { /** * Properties of an EnumOptions. @@ -13540,9 +13690,9 @@ $root.google = (function() { }; return EnumOptions; - })(); + })(protobuf.EnumOptions || {}); - protobuf.EnumValueOptions = (function() { + protobuf.EnumValueOptions = (function(EnumValueOptions) { /** * Properties of an EnumValueOptions. @@ -13771,9 +13921,9 @@ $root.google = (function() { }; return EnumValueOptions; - })(); + })(protobuf.EnumValueOptions || {}); - protobuf.ServiceOptions = (function() { + protobuf.ServiceOptions = (function(ServiceOptions) { /** * Properties of a ServiceOptions. @@ -14002,9 +14152,9 @@ $root.google = (function() { }; return ServiceOptions; - })(); + })(protobuf.ServiceOptions || {}); - protobuf.MethodOptions = (function() { + protobuf.MethodOptions = (function(MethodOptions) { /** * Properties of a MethodOptions. @@ -14290,9 +14440,9 @@ $root.google = (function() { })(); return MethodOptions; - })(); + })(protobuf.MethodOptions || {}); - protobuf.UninterpretedOption = (function() { + protobuf.UninterpretedOption = (function(UninterpretedOption) { /** * Properties of an UninterpretedOption. @@ -14668,7 +14818,7 @@ $root.google = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - UninterpretedOption.NamePart = (function() { + UninterpretedOption.NamePart = (function(NamePart) { /** * Properties of a NamePart. @@ -14876,12 +15026,12 @@ $root.google = (function() { }; return NamePart; - })(); + })(UninterpretedOption.NamePart || {}); return UninterpretedOption; - })(); + })(protobuf.UninterpretedOption || {}); - protobuf.SourceCodeInfo = (function() { + protobuf.SourceCodeInfo = (function(SourceCodeInfo) { /** * Properties of a SourceCodeInfo. @@ -15086,7 +15236,7 @@ $root.google = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - SourceCodeInfo.Location = (function() { + SourceCodeInfo.Location = (function(Location) { /** * Properties of a Location. @@ -15426,12 +15576,12 @@ $root.google = (function() { }; return Location; - })(); + })(SourceCodeInfo.Location || {}); return SourceCodeInfo; - })(); + })(protobuf.SourceCodeInfo || {}); - protobuf.GeneratedCodeInfo = (function() { + protobuf.GeneratedCodeInfo = (function(GeneratedCodeInfo) { /** * Properties of a GeneratedCodeInfo. @@ -15636,7 +15786,7 @@ $root.google = (function() { return this.constructor.toObject(this, $protobuf.util.toJSONOptions); }; - GeneratedCodeInfo.Annotation = (function() { + GeneratedCodeInfo.Annotation = (function(Annotation) { /** * Properties of an Annotation. @@ -15913,15 +16063,15 @@ $root.google = (function() { }; return Annotation; - })(); + })(GeneratedCodeInfo.Annotation || {}); return GeneratedCodeInfo; - })(); + })(protobuf.GeneratedCodeInfo || {}); return protobuf; - })(); + })(google.protobuf || {}); return google; -})(); +})($root.google || {}); module.exports = $root; diff --git a/tests/gen_same_package.js b/tests/gen_same_package.js new file mode 100644 index 000000000..90e5ca86d --- /dev/null +++ b/tests/gen_same_package.js @@ -0,0 +1,11 @@ +var tape = require("tape"); + +var some_pkg_1 = require("./data/same_package_1").some_pkg; +var some_pkg_2 = require("./data/same_package_2").some_pkg; // adds to $root.some_pkg as a side-effect + +tape.test("multiple .protos with the same package", function(test) { + test.equal(some_pkg_1, some_pkg_2, "should have one instance of a package node when using the default root setting"); + test.ok("MessageA" in some_pkg_1, "should have message from first .proto"); + test.ok("MessageB" in some_pkg_1, "should have message from second .proto"); + test.end(); +});