Let's say I have an array of objects that contains some String, Integer and Enum values. And also contains arrays of these types and methods that return these types.
For example an array containing the following ExampleObject:
object WeekDay extends Enumeration { type WeekDay = Value; val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value }
class ExampleObject (val integerValue1 : Integer, val integerValue2 : Integer, val stringValue1 : String, val weekDay: WeekDay.Value, val integerArray : Array[Integer])
{ def intReturningMethod1()= {0} }
From the command line I pass in a string with filter criteria to the scala application. For example:
-filter_criteria "((integerValue1 > 100 || integerValue2 < 50) && (stringValue1 == "A" || weekDay != "Mon")) || (integerArray(15) == 1) "
The operators should do what you expect in a normal if statement with these types of values.
How can I parse the filter criteria string and use it to filter ExampleObjects from an array?
Or where should I start reading to find out how to do this?
You might want to have a look at Twitter's Eval utility library, which you can find here on GitHub. You could just substitute the passed in filtering logic into a String at the point you want to use it and pass it to the eval function.
If you want to restrict the input to a limited language, you can easily create a parser for that language using only the Scala core library.
I have done this for a stripped down version of your example
First I use an import and create some helpers:
Then I create the parser:
This parser takes your input string and returns a function that maps an ExampleObject to a boolean value. This test function is constructed once while parsing the input string, using the pre-defined helper functions and the anonymous functions defined in the parser rules. The interpretation of the input string is only done once, while constructing the test function. When you execute the test function, you will run compiled Scale code. So it should run quite fast.
The test function is safe, because it does not allow the user to run arbitrary Scala code. It will just be constructed from the partial function provided in the parser and the pre-defined helpers.
You can easily extend the parser yourself, when you want it to use more functions or more fields in your ExampleObject.