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

Feature/interpolation #276

Closed

Conversation

akmjenkins
Copy link

Closes #274. Allows rules to interpolate stuff from runtime facts by a configuration interpolation pattern.

const interpolate = (subject = '', params = {}, regexp, resolver) => {
let shouldReplaceFull, found;

const replaced = subject.replace(regexp, (full, matched) => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird looking interpolation function, but it's been in my toolbelt for a while now. It allows you to preserve types when interpolating. e.g.

const subject = {
   myField: '{{a}}',
   myArray: '{{b}}'
}

interpolateDeep(
  subject,
  {
    a: 1,
    b: ['some','array']
  }
) 

// results in { myField: 1, myArray: ['some','array'] }

@akmjenkins
Copy link
Author

akmjenkins commented Sep 2, 2021

Usage is like this:

let reusableCompanyXmasPTORule = {
  conditions: {
    all: [{
      fact: 'account-information',
      operator: 'equal',
      value: '{{company.name}}',
      path: '$.company' // access the 'company' property of "account-information"
    }, {
      fact: 'account-information',
      operator: 'in',
      value: '{{allowedStatii}}'
      path: '$.status' // access the 'status' property of "account-information"
    }, {
      fact: 'account-information',
      operator: 'contains', // the 'ptoDaysTaken' property (an array) must contain '2016-12-25'
      value: '2016-12-25',
      path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information"
    }]
  },
  event: {
    type: '{{company.name}}-christmas-pto',
    params: {
      message: 'current {{company.name}} employee taking christmas day off'
    }
  }
}

engine.addRule(reusableCompanyXmasPTORule);

engine.run({
  accountId: 'lincoln',
  allowedStatii: ['active', 'paid-leave'], 
  company: {
     name: 'microsoft'
  }
});

@akmjenkins akmjenkins force-pushed the feature/interpolation branch from 5f8b536 to 81b3c78 Compare September 2, 2021 15:40
@MattCopenhaver
Copy link
Contributor

As pointed out in the related issue, the portion of this happening within conditions is already possible, and I would not recommend introducing a new syntax to accomplish the same thing.

@akmjenkins akmjenkins closed this Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible make value as a dynamic data like using path?
2 participants