Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fact Decorator Rough Sketch #381

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/engine-default-fact-decorators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { JSONPath } from "jsonpath-plus";

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Extra semicolon

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Extra semicolon

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Extra semicolon

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Extra semicolon

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Extra semicolon

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 1 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Extra semicolon
import FactDecorator from "./fact-decorator";

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Extra semicolon

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Extra semicolon

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Extra semicolon

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Extra semicolon

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Extra semicolon

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 2 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Extra semicolon

export const PathDecorator = new FactDecorator("path", (params, almanac, next) => {

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Strings must use singlequote

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Strings must use singlequote

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 4 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Strings must use singlequote
if (Object.prototype.hasOwnProperty.call(params, 'path')) {

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 2 spaces but found 4

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 2 spaces but found 4

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 2 spaces but found 4

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 2 spaces but found 4

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 2 spaces but found 4

Check failure on line 5 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 2 spaces but found 4
const path = params.path

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check failure on line 6 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8
const paramCopy = Object.assign({}, params)

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check failure on line 7 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8
delete paramCopy.path

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check failure on line 8 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8
return Promise.resolve(next(paramCopy, almanac)).then(factValue => {

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (14.x)

Expected indentation of 4 spaces but found 8

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (16.x)

Expected indentation of 4 spaces but found 8

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check failure on line 9 in src/engine-default-fact-decorators.js

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8
if (factValue != null && typeof factValue === 'object') {
const pathValue = JSONPath({ json: factValue, path, wrap: false })
debug('condition::evaluate extracting object', { property: path, received: pathValue })
return pathValue
} else {
debug('condition::evaluate could not compute object path of non-object', { path, factValue, type: typeof factValue })
return factValue
}
})

} else {
return next(params, almanac);
}
})

export const KeysOfDecorator = new FactDecorator("keysOf", (params, almanac, next) => {
const n = next(params, almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty.call(n, 'keys') && typeof n.keys === 'function') {
return Array.from(n.keys())
}
return Object.keys(n)
}
return n;
})

export const ValuesOfDecorator = new FactDecorator("valuesOf", (params, almanac, next) => {
const n = next(params,almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty(n, 'values') && typeof n.values === 'function') {
return Array.from(n.values())
}
return Object.values(n)
}
return n
})

export const SizeOfDecorator = new FactDecorator("sizeOf", (params, almanac, next) => {
const n = next(params, almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty(n, 'length')) {
return n.length
} else if (Object.prototype.hasOwnProperty(n, 'size') && typeof n.size === 'function') {
return n.size()
}
}
return 1
})

/**
* Options (arg 3) are merged onto fact options and override
* This allows us to do things like create a noCache version of a fact
* noCache:name for instance would access the name fact without hitting the cache
*/
export const NoCacheDecorator = new FactDecorator("noCache", (params, almanac, next) => next(params, almanac), { cache: false })
39 changes: 39 additions & 0 deletions src/fact-decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Fact from "./fact";

class FactDecorator {

/**
*
* @param {string} id
* @param {function} cb Function that computes the new fact value by invoking a 3rd argument
* that is a function to produce the next value
* @param {object} options options to override the defaults from the decorated fact
*/
constructor(id, cb, options) {
this.id = id
this.cb = cb
if (options) {
this.options = options
} else {
this.options = {}
}
}

/**
*
* @param {Fact} fact to decorate
* @returns {Fact} the decorated fact
*/
decorate(fact) {
const next = fact.calculate.bind(fact);
return new Fact(
`${this.id}:${fact.id}`,
(params, almanac) => this.cb(params, almanac, next),
Object.assign({}, this.options, fact.options)
)
}

}


export default FactDecorator
Loading
Oops, something went wrong.