Dependency resolution with json path expressions
You have a deeply nested object with some missing values.
You also have some rules how to calculate a value from other values.
Depresso takes an object and the calculation rules,
and does a dependency resolution.
Let's see an example.
We have an object with a placeholder for missing values:
MISSING = -1inventory =general:discount: 0.1price: MISSINGproducts:name: 'Bag'netPrice: 20tax: 0.1price: MISSINGname: 'Beer'netPrice: 10tax: 0.2price: 12
There are two rules
Here is the code describing these rules:
rules =target: '/products//price'depends:netPrice: '../netPrice'tax: '../tax': ->@netPrice * 1 + @taxtarget: '/general/price'depends:prices: '/products//price': ->_reduce@pricesx+y0
A rule consists of
Circular rules should be avoided.
Finally, we specify which node(s) should be calculated:
require 'depresso'resolve inventoryrules'/general/price'consolelog inventorygeneralprice # prints 34
The xpath-like expressions used throughout the examples are provided by SpahQL, the order of dependency resolution is calculated with DepGraph. The spahql query language has some limitations, the biggest one being the lack of the parent path. Improvements are expected, see https://github.com/danski/spahql/issues/1