Permalink
Browse files

Breaking: ReflectionObject#toJSON properly omits explicit undefined v…

…alues
  • Loading branch information...
dcodeIO committed Apr 13, 2017
1 parent 6493f52 commit b9f179064f3ddf683f13e0d4e17840301be64010
Showing with 84 additions and 92 deletions.
  1. +4 −4 src/enum.js
  2. +7 −7 src/field.js
  3. +7 −7 src/mapfield.js
  4. +8 −8 src/method.js
  5. +4 −4 src/namespace.js
  6. +4 −4 src/oneof.js
  7. +5 −5 src/service.js
  8. +9 −9 src/type.js
  9. +16 −0 src/util.js
  10. +1 −2 tests/api_enum.js
  11. +0 −2 tests/api_field.js
  12. +1 −3 tests/api_mapfield.js
  13. +7 −11 tests/api_namespace.js
  14. +2 −4 tests/api_oneof.js
  15. +1 −4 tests/api_service.js
  16. +8 −18 tests/api_type.js
@@ -74,10 +74,10 @@ Enum.fromJSON = function fromJSON(name, json) {
* @returns {EnumDescriptor} Enum descriptor
*/
Enum.prototype.toJSON = function toJSON() {
return {
options : this.options,
values : this.values
};
return util.toObject([
"options" , this.options,
"values" , this.values
]);
};
/**
@@ -235,13 +235,13 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
* @returns {FieldDescriptor} Field descriptor
*/
Field.prototype.toJSON = function toJSON() {
return {
rule : this.rule !== "optional" && this.rule || undefined,
type : this.type,
id : this.id,
extend : this.extend,
options : this.options
};
return util.toObject([
"rule" , this.rule !== "optional" && this.rule || undefined,
"type" , this.type,
"id" , this.id,
"extend" , this.extend,
"options" , this.options
]);
};
/**
@@ -79,13 +79,13 @@ MapField.fromJSON = function fromJSON(name, json) {
* @returns {MapFieldDescriptor} Map field descriptor
*/
MapField.prototype.toJSON = function toJSON() {
return {
keyType : this.keyType,
type : this.type,
id : this.id,
extend : this.extend,
options : this.options
};
return util.toObject([
"keyType" , this.keyType,
"type" , this.type,
"id" , this.id,
"extend" , this.extend,
"options" , this.options
]);
};
/**
@@ -115,14 +115,14 @@ Method.fromJSON = function fromJSON(name, json) {
* @returns {MethodDescriptor} Method descriptor
*/
Method.prototype.toJSON = function toJSON() {
return {
type : this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
requestType : this.requestType,
requestStream : this.requestStream,
responseType : this.responseType,
responseStream : this.responseStream,
options : this.options
};
return util.toObject([
"type" , this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
"requestType" , this.requestType,
"requestStream" , this.requestStream,
"responseType" , this.responseType,
"responseStream" , this.responseStream,
"options" , this.options
]);
};
/**
@@ -131,10 +131,10 @@ Object.defineProperty(Namespace.prototype, "nestedArray", {
* @returns {NamespaceBaseDescriptor} Namespace descriptor
*/
Namespace.prototype.toJSON = function toJSON() {
return {
options : this.options,
nested : arrayToJSON(this.nestedArray)
};
return util.toObject([
"options" , this.options,
"nested" , arrayToJSON(this.nestedArray)
]);
};
/**
@@ -66,10 +66,10 @@ OneOf.fromJSON = function fromJSON(name, json) {
* @returns {OneOfDescriptor} Oneof descriptor
*/
OneOf.prototype.toJSON = function toJSON() {
return {
oneof : this.oneof,
options : this.options
};
return util.toObject([
"options" , this.options,
"oneof" , this.oneof
]);
};
/**
@@ -68,11 +68,11 @@ Service.fromJSON = function fromJSON(name, json) {
*/
Service.prototype.toJSON = function toJSON() {
var inherited = Namespace.prototype.toJSON.call(this);
return {
options : inherited && inherited.options || undefined,
methods : Namespace.arrayToJSON(this.methodsArray) || /* istanbul ignore next */ {},
nested : inherited && inherited.nested || undefined
};
return util.toObject([
"options" , inherited && inherited.options || undefined,
"methods" , Namespace.arrayToJSON(this.methodsArray) || /* istanbul ignore next */ {},
"nested" , inherited && inherited.nested || undefined
]);
};
/**
@@ -281,15 +281,15 @@ Type.fromJSON = function fromJSON(name, json) {
*/
Type.prototype.toJSON = function toJSON() {
var inherited = Namespace.prototype.toJSON.call(this);
return {
options : inherited && inherited.options || undefined,
oneofs : Namespace.arrayToJSON(this.oneofsArray),
fields : Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; })) || {},
extensions : this.extensions && this.extensions.length ? this.extensions : undefined,
reserved : this.reserved && this.reserved.length ? this.reserved : undefined,
group : this.group || undefined,
nested : inherited && inherited.nested || undefined
};
return util.toObject([
"options" , inherited && inherited.options || undefined,
"oneofs" , Namespace.arrayToJSON(this.oneofsArray),
"fields" , Namespace.arrayToJSON(this.fieldsArray.filter(function(obj) { return !obj.declaringField; })) || {},
"extensions" , this.extensions && this.extensions.length ? this.extensions : undefined,
"reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
"group" , this.group || undefined,
"nested" , inherited && inherited.nested || undefined
]);
};
/**
@@ -34,6 +34,22 @@ util.toArray = function toArray(object) {
return array;
};
/**
* Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
* @param {Array.<*>} array Array to convert
* @returns {Object.<string,*>} Converted object
*/
util.toObject = function toObject(array) {
var object = {};
for (var i = 0; i < array.length; i += 2) {
var key = array[i ],
val = array[i + 1];
if (val !== undefined)
object[key] = val;
}
return object;
};
var safePropBackslashRe = /\\/g,
safePropQuoteRe = /"/g;
@@ -68,12 +68,11 @@ tape.test("reflected enums", function(test) {
}, "should no longer expose any removed values by id");
test.same(enm.toJSON(), {
options: undefined,
values: {
a: 1,
c: 3
}
}, "should export options and values to JSON");
}, "should export values to JSON");
enm_allow_alias.add( 'b', 0 );
test.same( enm_allow_alias.values, {
@@ -43,10 +43,8 @@ tape.test("reflected fields", function(test) {
field = new protobuf.Field("a", 1, "uint32", /* rule */ undefined, /* skipped extend, */ /* options */ {});
test.same(field.toJSON(), {
rule: undefined,
type: "uint32",
id: 1,
extend: undefined,
options: {}
}, "should export to JSON");
@@ -5,9 +5,7 @@ var protobuf = require("..");
var def = {
keyType: "bytes",
type: "string",
id: 1,
extend: undefined,
options: undefined
id: 1
};
tape.test("reflected map fields", function(test) {
@@ -2,10 +2,7 @@ var tape = require("tape");
var protobuf = require("..");
var def = {
nested: undefined,
options: undefined
};
var def = {};
var proto = "package ns;\
enum Enm {\
@@ -124,13 +121,12 @@ tape.test("reflected namespaces", function(test) {
});
test.same(ns.toJSON(), {
nested: {
Message: { extensions: undefined, fields: {}, group: undefined, nested: undefined, oneofs: undefined, options: undefined, reserved: undefined },
Enum: { options: undefined, values: {} },
Service: { methods: {}, nested: undefined, options: undefined },
extensionField: { extend: "Message", id: 1000, options: undefined, rule: undefined, type: "string" },
Other: { nested: undefined, options: undefined }
},
options: undefined
Message: { fields: {} },
Enum: { values: {} },
Service: { methods: {} },
extensionField: { extend: "Message", id: 1000, type: "string" },
Other: { }
}
}, "should create from Type, Enum, Service, extension Field and Namespace JSON");
test.end();
@@ -31,15 +31,13 @@ tape.test("reflected oneofs", function(test) {
kind.add(field);
test.same(kind.toJSON(), {
oneof: ["a", "b", "c"],
options: undefined
oneof: ["a", "b", "c"]
}, "should allow adding fields");
test.ok(Test.get("c"), "should still have the field on the parent");
kind.remove(field);
test.same(kind.toJSON(), {
oneof: ["a", "b"],
options: undefined
oneof: ["a", "b"]
}, "should allow removing fields");
test.ok(Test.get("c"), "should still have the field on the parent");
@@ -6,15 +6,12 @@ var def = {
methods: {},
nested: {
SomeEnum: {
options: undefined,
values: {}
}
},
options: undefined
}
};
var methodDef = {
type: undefined,
requestType: "MyRequest",
requestStream: true,
responseType: "MyResponse",
@@ -3,13 +3,7 @@ var tape = require("tape");
var protobuf = require("..");
var def = {
fields: {},
oneofs: undefined,
extensions: undefined,
reserved: undefined,
group: undefined,
nested: undefined,
options: undefined,
fields: {}
};
var def2 = {
@@ -75,20 +69,16 @@ tape.test("reflected types", function(test) {
});
test.same(type.toJSON(), {
fields: {
a: { extend: undefined, id: 1, options: undefined, rule: undefined, type: "string" }
a: { id: 1, type: "string" }
},
oneofs: undefined,
extensions: undefined,
reserved: [[900, 999], "b"],
group: undefined,
nested: {
Type: { extensions: undefined, fields: {}, group: undefined, nested: undefined, oneofs: undefined, options: undefined, reserved: undefined },
Enum: { options: undefined, values: {} },
Service: { methods: {}, nested: undefined, options: undefined },
extensionField: { extend: "Message", id: 1000, options: undefined, rule: undefined, type: "string" },
Other: { nested: undefined, options: undefined }
},
options: undefined
Type: { fields: {} },
Enum: { values: {} },
Service: { methods: {} },
extensionField: { extend: "Message", id: 1000, type: "string" },
Other: { }
}
}, "should create from Field, Type, Enum, Service, extension Field and Namespace JSON");
test.throws(function() {

0 comments on commit b9f1790

Please sign in to comment.