fix handling of volume list filters

This commit is contained in:
Julien Gilli 2017-03-22 18:19:02 -07:00
parent 00f92d67a5
commit 984c692d2f
3 changed files with 57 additions and 31 deletions

View File

@ -125,6 +125,40 @@ function jsonStream(arr, stream) {
});
}
/**
* Parses the string "filterComponent" of the form 'key=value' and returns an
* object that represents it with the form {'key': 'value'}. If "key"" in the
* "filterComponent" string is not included in the list "validKeys", it throws
* an error. It also throws an error if the string "filterComponent" is
* malformed.
*
* @param {String} filterComponent
* @param {arrayOfString} validKeys: Optional
*/
function _parseFilterKeyAndValue(filterComponent, validKeys) {
assert.string(filterComponent, 'filterComponent');
assert.optionalArrayOfString(validKeys, 'validKeys');
var idx = filterComponent.indexOf('=');
if (idx === -1) {
throw new errors.UsageError(format(
'invalid filter: "%s" (must be of the form "field=value")',
filterComponent));
}
var k = filterComponent.slice(0, idx);
var v = filterComponent.slice(idx + 1);
if (validKeys && validKeys.indexOf(k) === -1) {
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, validKeys.join('", "')));
}
return {
key: k,
value: v
};
}
/**
* given an array of key=value pairs, break them into an object
*
@ -138,20 +172,11 @@ function kvToObj(kvs, valid) {
assert.optionalArrayOfString(valid, 'valid');
var o = {};
var parsedKeyValue;
for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i];
var idx = kv.indexOf('=');
if (idx === -1)
throw new errors.UsageError(format(
'invalid filter: "%s" (must be of the form "field=value")',
kv));
var k = kv.slice(0, idx);
var v = kv.slice(idx + 1);
if (valid && valid.indexOf(k) === -1)
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, valid.join('", "')));
o[k] = v;
parsedKeyValue = _parseFilterKeyAndValue(kvs[i], valid);
o[parsedKeyValue.key] = parsedKeyValue.value;
}
return o;
}
@ -174,24 +199,23 @@ function jsonPredFromKv(kvs, validKeys, compositionType) {
'compositionType');
var predicate = {};
predicate[compositionType] = [];
var parsedKeyValue;
for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i];
var idx = kv.indexOf('=');
if (idx === -1) {
throw new errors.UsageError(format(
'invalid filter: "%s" (must be of the form "field=value")',
kv));
if (kvs.length === 0) {
return predicate;
}
if (kvs.length === 1) {
parsedKeyValue = _parseFilterKeyAndValue(kvs[0], validKeys);
predicate.eq = [parsedKeyValue.key, parsedKeyValue.value];
} else {
predicate[compositionType] = [];
for (var i = 0; i < kvs.length; i++) {
parsedKeyValue = _parseFilterKeyAndValue(kvs[i], validKeys);
predicate[compositionType].push({
eq: [parsedKeyValue.key, parsedKeyValue.value]
});
}
var k = kv.slice(0, idx);
var v = kv.slice(idx + 1);
if (validKeys && validKeys.indexOf(k) === -1) {
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, validKeys.join('", "')));
}
predicate[compositionType].push({eq: [k, v]});
}
return predicate;

View File

@ -11,6 +11,7 @@
*/
var format = require('util').format;
var jsprim = require('jsprim');
var tabula = require('tabula');
var common = require('../common');
@ -64,7 +65,7 @@ function do_list(subcmd, opts, args, callback) {
}
}
if (opts.all === undefined) {
if (jsprim.deepEqual(filterPredicate, {})) {
filterPredicate = {
and: [
{ ne: ['state', 'deleted']},

View File

@ -11,7 +11,8 @@
"bunyan": "1.5.1",
"cmdln": "4.1.2",
"extsprintf": "1.0.2",
"getpass": "0.1.6",
"getpass": "0.1.6",
"jsprim": "^1.4.0",
"lomstream": "1.1.0",
"mkdirp": "0.5.1",
"once": "1.3.2",