Skip to content

Commit

Permalink
resultsMap function to handle special cases of nested collections. In
Browse files Browse the repository at this point in the history
this case the inconsistent response from CF to instance inquiries,
wherein the result is an object with string index -> instance key/values
instead of an array
  • Loading branch information
gyllstromk committed Sep 30, 2013
1 parent d3ec014 commit 16ac750
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
26 changes: 22 additions & 4 deletions lib/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var factory = module.exports = function (request) {
};
};

var Collection = function (collection, schema, innerCollections, parent) {
var Collection = function (collection, schema, innerCollections, options) {
/**
* Collection manages interaction with RESTful collection on
* cloudfoundry, such as orgs, apps, services, spaces, etc
Expand All @@ -47,16 +47,24 @@ var factory = module.exports = function (request) {
Object.merge(obj, id);
if (id.metadata) {
id = id.metadata.guid;
} else if (typeof id.index !== 'undefined') {
id = id.index;
}
}

(innerCollections || []).each(function (each) {
var method, innerCollectionName, isNestedCollection = false;
var method,
innerCollectionName,
options = {},
isNestedCollection = false;

if (typeof each === 'object') {
method = each.method;
innerCollectionName = each.endpoint || method;
isNestedCollection = !! each.nested;
if (each.resultsMap) {
options.resultsMap = each.resultsMap;
}
} else {
method = each.camelize(false);
innerCollectionName = each;
Expand All @@ -65,7 +73,9 @@ var factory = module.exports = function (request) {
if (isNestedCollection) {
obj[method] =
new Collection(util.format('%s/%s/%s', collection, id,
innerCollectionName), {}, each.nested);
innerCollectionName),
{},
each.nested, options);
} else {
obj[method] = new InnerCollection(collection,
id,
Expand Down Expand Up @@ -150,9 +160,17 @@ var factory = module.exports = function (request) {
return callback(err);
}

if (options && options.resultsMap) {
results = options.resultsMap(results);
}

results = results.map(makeObject);

callback(null, multiResults ? results : results[0]);
if (! multiResults) {
results = results[0];
}

callback(null, results);
});
};

Expand Down
21 changes: 18 additions & 3 deletions lib/vcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,24 @@ var VcapClient = module.exports = function (info) {
}, [ 'routes', 'summary', 'service_bindings', {
method: 'instances',
nested: [{
method: 'logs',
endpoint: 'files/logs'
}]
method: 'logs',
endpoint: 'files/logs'
}],
resultsMap: function (results) {
// the result from CF seems to be inconsistent. rather than an
// array of instances, it's a map where keys are string index
// numbers. We convert it here, recording the index to the `index`
// field as a parsed int

var asArray = [];

Object.keys(results[0], function (key, value) {
value.index = parseInt(key, 10);
asArray[value.index] = value;
});

return asArray;
}
}]);

this.services = collections.create('services', {
Expand Down
14 changes: 12 additions & 2 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,15 @@ describe('collections', function () {
method: 'inner2',
nested: [{
method: 'inner2inner2'
}]
}],

resultsMap: function (results) {
results.each(function (each, index) {
each.fake = index;
});

return results;
}
}
]
);
Expand All @@ -206,8 +214,10 @@ describe('collections', function () {
done();
});

collection.get(0).inner2.get(function (err, result_) {
collection.get(0).inner2.get(function (err, results) {
assert(! err, err);
assert.equal(results[0].fake, 0); // proves resultsMap was
// executed
});
});
});
Expand Down

0 comments on commit 16ac750

Please sign in to comment.