Skip to content

Commit

Permalink
Added Namespace#getEnum and changed #lookupEnum to the same behavior,…
Browse files Browse the repository at this point in the history
… see #576
  • Loading branch information
dcodeIO committed Dec 19, 2016
1 parent 7fac9d6 commit ef43acf
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 48 deletions.
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This license applies to all parts of protobuf.js except those files explicitly
including or referencing a different license or files located in a directory
containing a different LICENSE file.
This license applies to all parts of protobuf.js except those files either
explicitly including or referencing a different license or located in a
directory containing a different LICENSE file.

Apache License
Version 2.0, January 2004
Expand Down
34 changes: 22 additions & 12 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Type, // cyclic

var nestedTypes, // contains cyclics
nestedError;

function initNested() {

/* istanbul ignore next */
Expand Down Expand Up @@ -163,6 +164,19 @@ NamespacePrototype.get = function get(name) {
return this.nested[name] || null;
};

/**
* Gets the values of the nested {@link Enum|enum} of the specified name.
* This methods differs from {@link Namespace#get} in that it returns an enum's values directly and throws instead of returning `null`.
* @param {string} name Nested enum name
* @returns {Object.<string,number>} Enum values
* @throws {Error} If there is no such enum
*/
NamespacePrototype.getEnum = function getEnum(name) {
if (this.nested && this.nested[name] instanceof Enum)
return this.nested[name].values;
throw Error("no such enum");
};

/**
* Adds a nested object to this namespace.
* @param {ReflectionObject} object Nested object to add
Expand Down Expand Up @@ -352,15 +366,15 @@ NamespacePrototype.lookupService = function lookupService(path) {
};

/**
* Looks up the {@link Enum|enum} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
* Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it returns the enum's values directly and throws instead of returning `null`.
* @param {string|string[]} path Path to look up
* @returns {Type} Looked up enum
* @returns {Object.<string,number>} Enum values
* @throws {Error} If `path` does not point to an enum
*/
NamespacePrototype.lookupEnum = function lookupEnum(path) {
var found = this.lookup(path, Enum);
if (!found)
throw Error("no such enum");
return found;
return found.values;
};
8 changes: 2 additions & 6 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ function Op(fn, len, val) {
this.val = val; // type varies
}

Writer.Op = Op;

/* istanbul ignore next */
function noop() {} // eslint-disable-line no-empty-function

Expand Down Expand Up @@ -91,8 +89,6 @@ function State(writer) {
this.next = writer.states;
}

Writer.State = State;

/**
* Constructs a new writer instance.
* @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
Expand Down Expand Up @@ -159,7 +155,7 @@ Writer.alloc = function alloc(size) {

// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice);
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray);

/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
Expand Down Expand Up @@ -504,6 +500,6 @@ WriterPrototype.finish = function finish() {
pos += head.len;
head = head.next;
}
this.head = this.tail = null; // gc
// this.head = this.tail = null;
return buf;
};
2 changes: 1 addition & 1 deletion src/writer_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ BufferWriter.alloc = function alloc_buffer(size) {
})(size);
};

var writeBytesBuffer = Buffer && Buffer.from && Buffer.prototype.set.name !== "deprecated"
var writeBytesBuffer = Buffer && Buffer.from && Buffer.prototype.set.name[0] === "s" // node v4: set.name == "deprecated"
? function writeBytesBuffer_set(val, buf, pos) {
buf.set(val, pos); // faster than copy (requires node > 0.12)
}
Expand Down
19 changes: 14 additions & 5 deletions types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// $> pbts --name protobufjs --out types/protobuf.js.d.ts src
// Generated Sun, 18 Dec 2016 22:48:04 UTC
// Generated Mon, 19 Dec 2016 12:17:04 UTC
declare module "protobufjs" {

/**
Expand Down Expand Up @@ -726,6 +726,15 @@ declare module "protobufjs" {
*/
get(name: string): ReflectionObject;

/**
* Gets the values of the nested {@link Enum|enum} of the specified name.
* This methods differs from {@link Namespace#get} in that it returns an enum's values directly and throws instead of returning `null`.
* @param {string} name Nested enum name
* @returns {Object.<string,number>} Enum values
* @throws {Error} If there is no such enum
*/
getEnum(name: string): { [k: string]: number };

/**
* Adds a nested object to this namespace.
* @param {ReflectionObject} object Nested object to add
Expand Down Expand Up @@ -797,13 +806,13 @@ declare module "protobufjs" {
lookupService(path: (string|string[])): Service;

/**
* Looks up the {@link Enum|enum} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it throws instead of returning `null`.
* Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
* Besides its signature, this methods differs from {@link Namespace#lookup} in that it returns the enum's values directly and throws instead of returning `null`.
* @param {string|string[]} path Path to look up
* @returns {Type} Looked up enum
* @returns {Object.<string,number>} Enum values
* @throws {Error} If `path` does not point to an enum
*/
lookupEnum(path: (string|string[])): Type;
lookupEnum(path: (string|string[])): { [k: string]: number };
}

/**
Expand Down

0 comments on commit ef43acf

Please sign in to comment.