summaryrefslogtreecommitdiff
path: root/lib/nodejs/lib/thrift
diff options
context:
space:
mode:
authorHenrique Mendonça <henrique@apache.org>2015-05-27 22:15:46 +1000
committerHenrique Mendonça <henrique@apache.org>2015-05-27 22:15:46 +1000
commit1568aef7d499153469131449ec682998598f0d3c (patch)
tree40fbe11ee1a3f20dbd76a944dd6a1251b74c06a0 /lib/nodejs/lib/thrift
parent549a9e1e6dfbfaf1f7685dc5e26440c501517738 (diff)
downloadthrift-1568aef7d499153469131449ec682998598f0d3c.tar.gz
THRIFT-3122 Javascript struct constructor should properly initialize struct and container members from plain js arguments
Client: Node and JS Patch: Igor Tkach This closes #476
Diffstat (limited to 'lib/nodejs/lib/thrift')
-rw-r--r--lib/nodejs/lib/thrift/json_protocol.js10
-rw-r--r--lib/nodejs/lib/thrift/thrift.js67
2 files changed, 75 insertions, 2 deletions
diff --git a/lib/nodejs/lib/thrift/json_protocol.js b/lib/nodejs/lib/thrift/json_protocol.js
index 77339f7f1..e98650c50 100644
--- a/lib/nodejs/lib/thrift/json_protocol.js
+++ b/lib/nodejs/lib/thrift/json_protocol.js
@@ -546,9 +546,15 @@ TJSONProtocol.prototype.readFieldEnd = function() {
*/
TJSONProtocol.prototype.readMapBegin = function() {
var map = this.rstack.pop();
+ var first = map.shift();
+ if (first instanceof Array) {
+ this.rstack.push(map);
+ map = first;
+ first = map.shift();
+ }
var r = {};
- r.ktype = TJSONProtocol.RType[map.shift()];
+ r.ktype = TJSONProtocol.RType[first];
r.vtype = TJSONProtocol.RType[map.shift()];
r.size = map.shift();
@@ -582,7 +588,7 @@ TJSONProtocol.prototype.readListBegin = function() {
r.size = list.shift();
this.rpos.push(this.rstack.length);
- this.rstack.push(list);
+ this.rstack.push(list.shift());
return r;
};
diff --git a/lib/nodejs/lib/thrift/thrift.js b/lib/nodejs/lib/thrift/thrift.js
index 89c789d9f..e6edf9efe 100644
--- a/lib/nodejs/lib/thrift/thrift.js
+++ b/lib/nodejs/lib/thrift/thrift.js
@@ -151,3 +151,70 @@ exports.objectLength = function(obj) {
exports.inherits = function(constructor, superConstructor) {
util.inherits(constructor, superConstructor);
};
+
+var copyList, copyMap;
+
+copyList = function(lst, types) {
+
+ if (!lst) {return lst; }
+
+ var type;
+
+ if (types.shift === undefined) {
+ type = types;
+ }
+ else {
+ type = types[0];
+ }
+ var Type = type;
+
+ var len = lst.length, result = [], i, val;
+ for (i = 0; i < len; i++) {
+ val = lst[i];
+ if (type === null) {
+ result.push(val);
+ }
+ else if (type === copyMap || type === copyList) {
+ result.push(type(val, types.slice(1)));
+ }
+ else {
+ result.push(new Type(val));
+ }
+ }
+ return result;
+};
+
+copyMap = function(obj, types){
+
+ if (!obj) {return obj; }
+
+ var type;
+
+ if (types.shift === undefined) {
+ type = types;
+ }
+ else {
+ type = types[0];
+ }
+ var Type = type;
+
+ var result = {}, val;
+ for(var prop in obj) {
+ if(obj.hasOwnProperty(prop)) {
+ val = obj[prop];
+ if (type === null) {
+ result[prop] = val;
+ }
+ else if (type === copyMap || type === copyList) {
+ result[prop] = type(val, types.slice(1));
+ }
+ else {
+ result[prop] = new Type(val);
+ }
+ }
+ }
+ return result;
+};
+
+module.exports.copyMap = copyMap;
+module.exports.copyList = copyList;