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

Implement parsing string representations back to AST. #83

Closed
wants to merge 5 commits into from

Conversation

estum
Copy link
Contributor

@estum estum commented Apr 4, 2021

RuleInterpreter takes string representations of rules, predicates or operations and parses them back to AST, which can be compiled to a rule object. So, it makes an ability to store rules as a plain text, for example in db columns, and run them as usual.

interpreter = Dry::Logic::RuleInterpreter.new(Dry::Logic::Predicates)
rule = "key?(:email)"

interpreter.(rule) 
# => [[:predicate, [:key?, [[:name, :email], [:input, Undefined]]]]]

interpreter.compile(rule)
# => #<Dry::Logic::Rule::Predicate::Predicate2Arity1Curried predicate=#<Method: Dry::Logic::Predicates.key?> options={:args=>[:user], :arity=>2}>

interpreter.compile(rule)[0].to_s == rule
# => true

To implement that feature, I have had to override Dry::Logic::Operations::Check#to_s method to hold required args.

The name of the class may be controversial, I will rename it if you've got a better one.

Remove duplication in specs
RuleInterpreter takes a string representation of a rule, predicate or operation and parses it back to AST, which  compiles to a rule object. So, it makes an ability to store rules as a plain text, for example in db columns, and run them as usual.
@estum estum requested a review from solnic as a code owner April 4, 2021 09:22
estum added a commit to estum/dry-logic that referenced this pull request Apr 4, 2021
@flash-gordon
Copy link
Member

flash-gordon commented Apr 4, 2021

@estum could you start a discussion at our forum per the guildelines? The problem with submitting large PRs is that in order for them to get merged we need to be on board with the idea :) Even though serialization/deserialization facilities are a good thing I would strongly argue about the format and limitations. For instance, using JSON-compatible types for dumping AST would be superior IMO. And even then something like lambdas or arbitrary objects cannot be serialized to strings or JSON, users must be properly warned about this.

@estum estum closed this Jan 15, 2023
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.

2 participants