Skip to content

Commit

Permalink
Breaking: Renamed asJSON option keys (enum to enums, long to longs) b…
Browse files Browse the repository at this point in the history
…ecause enum is a reserved keyword
  • Loading branch information
dcodeIO committed Dec 22, 2016
1 parent c60cd39 commit c144e73
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 49 deletions.
31 changes: 19 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.

14 changes: 10 additions & 4 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.

1 change: 1 addition & 0 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function create(type, ctor) {
if (keys[i] !== value)
delete this[keys[i]];
}
// see util.prop for IE8 support
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ function convert(type, source, destination, options, converter) {
* @typedef JSONConversionOptions
* @type {Object}
* @property {boolean} [fieldsOnly=false] Keeps only properties that reference a field
* @property {function} [long] Long conversion type. Only relevant with a long library.
* @property {*} [longs] Long conversion type. Only relevant with a long library.
* Valid values are `String` and `Number` (the global types).
* Defaults to a possibly unsafe number without, and a `Long` with a long library.
* @property {function} [enum=Number] Enum value conversion type.
* @property {*} [enums=Number] Enum value conversion type.
* Valid values are `String` and `Number` (the global types).
* Defaults to the numeric ids.
* @property {function} [bytes] Bytes value conversion type.
* @property {*} [bytes] Bytes value conversion type.
* Valid values are `Array` and `String` (the global types).
* Defaults to return the underlying buffer type.
* @property {boolean} [defaults=false] Also sets default values on the resulting object
Expand All @@ -84,19 +84,19 @@ convert.toJson = function toJson(field, value, options) {
return convert(value.$type, value, {}, options, toJson);

// Enums as strings
if (options["enum"] && field.resolvedType instanceof Enum)
return options["enum"] === String
if (options.enums && field.resolvedType instanceof Enum)
return options.enums === String
? field.resolvedType.getValuesById()[value]
: value | 0;

// Longs as numbers or strings
if (options.long && field.long) {
if (options.longs && field.long) {
var unsigned = field.type.charAt(0) === "u";
if (options.long === Number)
if (options.longs === Number)
return typeof value === "number"
? value
: util.LongBits.from(value).toNumber(unsigned);
if (options.long === String) {
if (options.longs === String) {
if(typeof value === "number")
return util.Long.fromNumber(value, unsigned).toString();
value = util.Long.fromValue(value); // TODO: fromValue is missing an unsigned option (long.js 3.2.0)
Expand Down
12 changes: 9 additions & 3 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ util.pool = require("@protobufjs/pool");
*/
util.isNode = Boolean(global.process && global.process.versions && global.process.versions.node);

/**
* Whether running within IE8 or not.
* @memberof util
* @type {boolean}
*/
util.isIE8 = false; try { util.isIE8 = eval("!-[1,]"); } catch (e) {} // eslint-disable-line no-eval, no-empty

/**
* Node's Buffer class if available.
* @type {?function(new: Buffer)}
Expand Down Expand Up @@ -148,18 +155,17 @@ util.props = function props(target, descriptors) {
* @returns {undefined}
*/
util.prop = function prop(target, key, descriptor) {
var ie8 = !-[1,];
var ucKey = util.ucFirst(key);
if (descriptor.get)
target["get" + ucKey] = descriptor.get;
if (descriptor.set)
target["set" + ucKey] = ie8
target["set" + ucKey] = util.isIE8
? function(value) {
descriptor.set.call(this, value);
this[key] = value;
}
: descriptor.set;
if (ie8) {
if (util.isIE8) {
if (descriptor.value !== undefined)
target[key] = descriptor.value;
} else
Expand Down
6 changes: 3 additions & 3 deletions tests/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ tape.test("convert", function(test) {
enumRepeated: [1, 2]
});

test.equal(msg.asJSON({ long: Number }).uint64Val, 1, "longs to numbers");
test.equal(msg.asJSON({ long: String }).uint64Val, "1", "longs to strings");
test.equal(msg.asJSON({ longs: Number }).uint64Val, 1, "longs to numbers");
test.equal(msg.asJSON({ longs: String }).uint64Val, "1", "longs to strings");

test.equal(Object.prototype.toString.call(msg.asJSON({ bytes: Array }).bytesVal), "[object Array]", "bytes to arrays");
test.equal(msg.asJSON({ bytes: String }).bytesVal, "MTEx", "bytes to base64 strings");
if (protobuf.util.isNode)
test.ok(Buffer.isBuffer(msg.asJSON({ bytes: Buffer }).bytesVal), "bytes to buffers");

test.equal(msg.asJSON({ enum: String }).enumVal, "ONE", "enums to strings");
test.equal(msg.asJSON({ enums: String }).enumVal, "ONE", "enums to strings");

test.end();
});
Expand Down
6 changes: 3 additions & 3 deletions tests/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tape.test("RPC", function(test) {
test.test("should call the rpc impl with", function(test) {
test.equal(method, MyMethod, "the reflected method");
test.ok(requestData.length, "a buffer");
test.ok(typeof callback === 'function', "a callback function");
test.ok(typeof callback === "function", "a callback function");
test.end();
});
test.test("should call with a buffer that contains", function(test) {
Expand All @@ -36,15 +36,15 @@ tape.test("RPC", function(test) {
});
}

var MyService = root.lookup("MyService");
MyService = root.lookup("MyService");
var service = MyService.create(rpc, true, false);

service.myMethod(MyRequest.create({
path: "/"
}), function(err, response) {
if (err)
return test.fail(err.message);
test.ok(response instanceof MyResponse.ctor, "should return an instance of MyResponse");
test.ok(response instanceof MyResponse.getCtor(), "should return an instance of MyResponse");
test.deepEqual(response, {
status: 200
}, "should return status 200");
Expand Down
21 changes: 14 additions & 7 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 Thu, 22 Dec 2016 13:18:42 UTC
// Generated Thu, 22 Dec 2016 22:25:58 UTC
declare module "protobufjs" {

/**
Expand Down Expand Up @@ -119,22 +119,22 @@ declare module "protobufjs" {
* @typedef JSONConversionOptions
* @type {Object}
* @property {boolean} [fieldsOnly=false] Keeps only properties that reference a field
* @property {function} [long] Long conversion type. Only relevant with a long library.
* @property {*} [longs] Long conversion type. Only relevant with a long library.
* Valid values are `String` and `Number` (the global types).
* Defaults to a possibly unsafe number without, and a `Long` with a long library.
* @property {function} [enum=Number] Enum value conversion type.
* @property {*} [enums=Number] Enum value conversion type.
* Valid values are `String` and `Number` (the global types).
* Defaults to the numeric ids.
* @property {function} [bytes] Bytes value conversion type.
* @property {*} [bytes] Bytes value conversion type.
* Valid values are `Array` and `String` (the global types).
* Defaults to return the underlying buffer type.
* @property {boolean} [defaults=false] Also sets default values on the resulting object
*/
interface JSONConversionOptions {
fieldsOnly: boolean;
long: () => any;
enum: () => any;
bytes: () => any;
longs: any;
enums: any;
bytes: any;
defaults: boolean;
}

Expand Down Expand Up @@ -2174,6 +2174,13 @@ declare module "protobufjs" {
*/
var isNode: boolean;

/**
* Whether running within IE8 or not.
* @memberof util
* @type {boolean}
*/
var isIE8: boolean;

/**
* Node's Buffer class if available.
* @type {?function(new: Buffer)}
Expand Down

0 comments on commit c144e73

Please sign in to comment.