chore(JobExecutor): use value-matcher (#633)
This commit is contained in:
parent
c8dc786008
commit
0b6bb65c17
@ -108,6 +108,7 @@
|
||||
"through2": "^2.0.3",
|
||||
"tmp": "^0.0.33",
|
||||
"uuid": "^3.0.1",
|
||||
"value-matcher": "^0.0.0",
|
||||
"ws": "^3.3.3",
|
||||
"xen-api": "^0.16.0",
|
||||
"xml2js": "^0.4.19",
|
||||
|
@ -1,18 +1,14 @@
|
||||
import Bluebird from 'bluebird'
|
||||
import { BaseError } from 'make-error'
|
||||
import { createPredicate } from 'value-matcher'
|
||||
import { timeout } from 'promise-toolbox'
|
||||
import {
|
||||
assign,
|
||||
every,
|
||||
filter,
|
||||
find,
|
||||
isArray,
|
||||
isEmpty,
|
||||
isPlainObject,
|
||||
map,
|
||||
mapValues,
|
||||
size,
|
||||
some,
|
||||
} from 'lodash'
|
||||
|
||||
import { crossProduct } from './math'
|
||||
@ -35,32 +31,6 @@ export class UnsupportedVectorType extends JobExecutorError {
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const match = (pattern, value) => {
|
||||
if (isPlainObject(pattern)) {
|
||||
if (size(pattern) === 1) {
|
||||
let op
|
||||
if ((op = pattern.__or) !== undefined) {
|
||||
return some(op, subpattern => match(subpattern, value))
|
||||
}
|
||||
if ((op = pattern.__not) !== undefined) {
|
||||
return !match(op, value)
|
||||
}
|
||||
}
|
||||
|
||||
return isPlainObject(value) && every(pattern, (subpattern, key) => (
|
||||
value[key] !== undefined && match(subpattern, value[key])
|
||||
))
|
||||
}
|
||||
|
||||
if (isArray(pattern)) {
|
||||
return isArray(value) && every(pattern, subpattern =>
|
||||
some(value, subvalue => match(subpattern, subvalue))
|
||||
)
|
||||
}
|
||||
|
||||
return pattern === value
|
||||
}
|
||||
|
||||
const paramsVectorActionsMap = {
|
||||
extractProperties ({ mapping, value }) {
|
||||
return mapValues(mapping, key => value[key])
|
||||
@ -71,7 +41,7 @@ const paramsVectorActionsMap = {
|
||||
))
|
||||
},
|
||||
fetchObjects ({ pattern }) {
|
||||
const objects = filter(this.xo.getObjects(), object => match(pattern, object))
|
||||
const objects = filter(this.xo.getObjects(), createPredicate(pattern))
|
||||
if (isEmpty(objects)) {
|
||||
throw new Error('no objects match this pattern')
|
||||
}
|
||||
|
15
yarn.lock
15
yarn.lock
@ -33,6 +33,13 @@
|
||||
dependencies:
|
||||
"@babel/types" "7.0.0-beta.31"
|
||||
|
||||
"@babel/polyfill@^7.0.0-beta.36":
|
||||
version "7.0.0-beta.36"
|
||||
resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.0.0-beta.36.tgz#c37320d0e38f2cb6da3a78889b380cb529c7a957"
|
||||
dependencies:
|
||||
core-js "^2.4.0"
|
||||
regenerator-runtime "^0.11.1"
|
||||
|
||||
"@babel/template@7.0.0-beta.31":
|
||||
version "7.0.0-beta.31"
|
||||
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.31.tgz#577bb29389f6c497c3e7d014617e7d6713f68bda"
|
||||
@ -5973,7 +5980,7 @@ regenerator-runtime@^0.10.5:
|
||||
version "0.10.5"
|
||||
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
|
||||
|
||||
regenerator-runtime@^0.11.0:
|
||||
regenerator-runtime@^0.11.0, regenerator-runtime@^0.11.1:
|
||||
version "0.11.1"
|
||||
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
|
||||
|
||||
@ -7099,6 +7106,12 @@ validate-npm-package-license@^3.0.1:
|
||||
spdx-correct "~1.0.0"
|
||||
spdx-expression-parse "~1.0.0"
|
||||
|
||||
value-matcher@^0.0.0:
|
||||
version "0.0.0"
|
||||
resolved "https://registry.yarnpkg.com/value-matcher/-/value-matcher-0.0.0.tgz#c0caf87dc3998a68ea56b31fd1916adefe39f7be"
|
||||
dependencies:
|
||||
"@babel/polyfill" "^7.0.0-beta.36"
|
||||
|
||||
varint@^5.0.0, varint@~5.0.0:
|
||||
version "5.0.0"
|
||||
resolved "https://registry.yarnpkg.com/varint/-/varint-5.0.0.tgz#d826b89f7490732fabc0c0ed693ed475dcb29ebf"
|
||||
|
Loading…
Reference in New Issue
Block a user