Skip to content

Commit 19c269f

Browse files
committed
Generate lookupTYPE functions programmatically; Ensure working reflection class names with minified builds
1 parent 6f9ffb6 commit 19c269f

21 files changed

+96
-56
lines changed

dist/protobuf.js

+37-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

83 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

0 Bytes
Binary file not shown.

scripts/bundle.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function bundle(compress, runtime) {
4949
.pipe(sourcemaps.init({ loadMaps: true }))
5050
.pipe(
5151
gulpif(compress, uglify({
52-
mangleProperties: { regex: /^_/ }
52+
mangleProperties: {
53+
regex: /^_/
54+
}
5355
}))
5456
)
5557
.pipe(header(license, {

src/enum.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Enum;
33

4+
Enum.className = "Enum";
5+
46
var ReflectionObject = require("./object");
57
/** @alias Enum.prototype */
68
var EnumPrototype = ReflectionObject.extend(Enum);

src/field.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Field;
33

4+
Field.className = "Field";
5+
46
var ReflectionObject = require("./object");
57
/** @alias Field.prototype */
68
var FieldPrototype = ReflectionObject.extend(Field);

src/mapfield.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = MapField;
33

4+
MapField.className = "MapField";
5+
46
var Field = require("./field");
57
/** @alias Field.prototype */
68
var FieldPrototype = Field.prototype;

src/method.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Method;
33

4+
Method.className = "Method";
5+
46
var ReflectionObject = require("./object");
57
/** @alias Method.prototype */
68
var MethodPrototype = ReflectionObject.extend(Method);

src/namespace.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Namespace;
33

4+
Namespace.className = "Namespace";
5+
46
var ReflectionObject = require("./object");
57
/** @alias Namespace.prototype */
68
var NamespacePrototype = ReflectionObject.extend(Namespace);
@@ -268,30 +270,29 @@ NamespacePrototype.lookup = function lookup(path, parentAlreadyChecked) {
268270
return this.parent.lookup(path);
269271
};
270272

273+
[ Type, Service ].forEach(function(T) {
274+
NamespacePrototype['lookup' + T.className] = function lookupT() {
275+
var found = this.lookup(path);
276+
if (!(found instanceof T))
277+
throw Error("no such " + T.className);
278+
return found;
279+
};
280+
});
281+
271282
/**
272283
* Looks up the {@link Type|type} at the specified path, relative to this namespace.
273284
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
285+
* @name Namespace#lookupType
274286
* @param {string|string[]} path Path to look up
275287
* @returns {Type} Looked up type
276288
* @throws {Error} If `path` does not point to a type
277289
*/
278-
NamespacePrototype.lookupType = function lookupType(path) {
279-
var found = this.lookup(path);
280-
if (!(found instanceof Type))
281-
throw Error("no such type");
282-
return found;
283-
};
284290

285291
/**
286292
* Looks up the {@link Service|service} at the specified path, relative to this namespace.
287293
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
294+
* @name Namespace#lookupService
288295
* @param {string|string[]} path Path to look up
289296
* @returns {Service} Looked up service
290297
* @throws {Error} If `path` does not point to a service
291298
*/
292-
NamespacePrototype.lookupService = function lookupService(path) {
293-
var found = this.lookup(path);
294-
if (!(found instanceof Service))
295-
throw Error("no such service");
296-
return found;
297-
};

src/object.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
module.exports = ReflectionObject;
33

4+
ReflectionObject.className = "ReflectionObject";
45
ReflectionObject.extend = extend;
56

67
var Root = require("./root"),
@@ -190,8 +191,12 @@ ReflectionObjectPrototype.setOptions = function setOptions(options, ifNotSet) {
190191

191192
/**
192193
* Converts this instance to its string representation.
193-
* @returns {string} Constructor name, space, full name
194+
* @returns {string} Class name[, space, full name]
194195
*/
195196
ReflectionObjectPrototype.toString = function toString() {
196-
return this.constructor.name + " " + this.getFullName();
197+
var className = this.constructor.className;
198+
var fullName = this.getFullName();
199+
if (fullName.length)
200+
return className + " " + fullName;
201+
return className;
197202
};

src/oneof.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = OneOf;
33

4+
OneOf.className = "OneOf";
5+
46
var ReflectionObject = require("./object");
57
/** @alias OneOf.prototype */
68
var OneOfPrototype = ReflectionObject.extend(OneOf);

src/root.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Root;
33

4+
Root.className = "Root";
5+
46
var Namespace = require("./namespace");
57
/** @alias Root.prototype */
68
var RootPrototype = Namespace.extend(Root);
@@ -270,10 +272,3 @@ RootPrototype._handleRemove = function handleRemove(object) {
270272
this._handleRemove(nested[i]);
271273
}
272274
};
273-
274-
/**
275-
* @override
276-
*/
277-
RootPrototype.toString = function toString() {
278-
return this.constructor.name;
279-
};

src/service.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Service;
33

4+
Service.className = "Service";
5+
46
var Namespace = require("./namespace");
57
/** @alias Namespace.prototype */
68
var NamespacePrototype = Namespace.prototype;

src/type.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
22
module.exports = Type;
33

4+
Type.className = "Type";
5+
46
var Namespace = require("./namespace");
57
/** @alias Namespace.prototype */
68
var NamespacePrototype = Namespace.prototype;

tests/browser.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3-
<head><script src="../dist/protobuf.js"></script></head>
3+
<head><script src="../dist/protobuf.min.js"></script></head>
44
<body>
55
<script>
66
protobuf.util.fetch("browser.html")
@@ -13,6 +13,12 @@
1313
.catch(function(err) {
1414
console.log("error", err);
1515
});
16+
17+
var root = new protobuf.Root();
18+
console.log(root.toString());
19+
var message = new protobuf.Type("Message");
20+
root.add(message);
21+
console.log(message.toString());
1622
</script>
1723
</body>
1824
</html>

0 commit comments

Comments
 (0)