Created
March 23, 2012 14:59
-
-
Save bennadel/2171470 to your computer and use it in GitHub Desktop.
ColdFusion 10 - Creating A Simple Expression Evaluator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfscript> | |
| // I evaluate the given expression in the context of the given | |
| // context object (for variable look-up). | |
| evaluateExpression = function( context, expression ){ | |
| // Copy the context object to the look-up. | |
| structAppend( local, context ); | |
| // Evaluate the expression in the context of the current | |
| // local scope, which has been augmented with the contents | |
| // of the context object. | |
| return( evaluate( expression ) ); | |
| }; | |
| // ------------------------------------------------------ // | |
| // ------------------------------------------------------ // | |
| // Create a context object. | |
| friend = { | |
| name: "Tricia", | |
| age: 35 | |
| }; | |
| // Evaluate the expression in the given context. | |
| condition = evaluateExpression( | |
| friend, | |
| "(name eq 'Tricia') && (age gt 30)" | |
| ); | |
| // Output the evaluated result. | |
| writeOutput( "Result: " & condition & "<br />" ); | |
| // Evaluate the expression in the given context. | |
| condition = evaluateExpression( | |
| friend, | |
| "len( name ) eq 3" | |
| ); | |
| // Output the evaluated result. | |
| writeOutput( "Result: " & condition & "<br />" ); | |
| </cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wish CF let you specify a scope, it isn't safe to allow an end user to write expressions:
condition = evaluateExpression(
friend,
"SERVER.coldfusion.productversion EQ '2023...
//Result: YES