zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
445 stars 54 forks source link

Escape Charachters #8

Closed jtabet010 closed 3 years ago

jtabet010 commented 3 years ago

Hello zeroSteiner, thank you for this excellent engine. I wander how it is possilbe to add escaple charachters for special charachters like @ or - for example this rule RULE= '@ffice365-AuditData_Operation == "New Account" ' is returning rule_engine.errors.RuleSyntaxError: ("syntax error (illegal character '@')", LexToken(error,'@AuditData_Operation == "New Account" ',1,9))

and this rule 'Office365-AuditDataOperation == "New Account" ' doesn't work unless we replace the - with such as 'Office365_AuditData_Operation == "New Account" '

Thank you for you work,

zeroSteiner commented 3 years ago

No sorry, I don't really want to deal with the complications of adding a control character to alter the syntax when you could just replace the characters. The - isn't a special character it all, it's the subtract operator so office365-AuditData_Operation == "New Account" would be subtract AuditData_Operation from office365 and check if it equals "New Account". This would be syntatically correct but I doubt it's what you mean since you want office365-AuditData_Operation to be the symbol.

If you have multiple variables starting with office365 I'd suggest you put them into a dictionary like and do something like:

rule_engine.Rule('office365.AuditData_Operation == "New Account"').matches({ 
    'office365' : { 
        'AuditData_Operation': 'New Account', 
        'Account': 'John Doe' 
    } 
})

Note the . to treat AuditData_Operation as an attribute of office365.

I'm moving towards eventually having full first-class support for hashes / dictionaries at which point this would be a moot point since you could put your variables that would have unsupported characters into one and do: event['@Office365-AuditData_Operation'] == "New Account". That's a ways away at this point though.

zeroSteiner commented 3 years ago

Alright, so the escape characters for symbols aren't going to happen. What you can do now instead is use the new MAPPING datatype, then reference your data using strings like record["@Office365-AuditData_Operation"] == 'New Account'. This was added in the v2.4 release that I just published.

You can experiment with this behavior using the new Debug REPL utility.

python -m rule_engine.debug_repl --edit-console
edit the 'context' and 'thing' objects as necessary
>>> thing=dict(book=dict(title='Batman', publisher='DC', issue=1))
>>> 
exiting the edit console...
rule > book['title'] == 'Superman'
result: 
False
rule >