GitHub user ottobackwards opened a pull request:
https://github.com/apache/metron/pull/814
METRON-1277 Add match statement to Stellar language
as discussed on the dev list: [Stellar support for switch/case style
conditionals](http://mail-archives.apache.org/mod_mbox/metron-dev/201710.mbox/%3ccappqhk0j4bpvmppznqpzeu2jtbuxhw8yheng0lmbbl_vw+x...@mail.gmail.com%3e)
This PR adds 'match' capability to the stellar language. This is very
similar to the Scala match feature.
FROM THE README:
### Stellar Language Match Expression
Stellar provides the capability to write match expressions, which are
similar to switch statements commonly found in c like languages, but more like
Scala's match.
The syntax is:
* `match{ logical_expression1 : {evaluation expression1},
logical_expression2 : {evaluation_expression2}` : A match expression with no
default
* `match{ logical_expression1 : {evaluation expression1},
logical_expression2 : {evaluation_expression2}, default : default_expression}`
: A match expression with a default expression
Where:
* `logical_expression` is a Stellar expression that evaluates to true or
false. For instance `var > 0` or `var > 0 AND var2 == 'foo'`
* `evaluation_expression` is any Stellar Expression
> Currently the MAP() function is not supported
> Lambda expressions are supported, but they must be no argument lambdas
such as `() -> STATEMENT`
* Only the first clause that evaluates to true will be executed.
* The match must return some value. In other words, a default should be
provided unless the logical_expressions are posed such that
one will always evaluate to true. Failure to do this will result in an
error.
## Review items
* more test cases
* help with issue with MAP() function
* correctness of short circuit / grammar
This PR does not
* add the aliasing of long variables
* support variable arg lambda
* work with the MAP() function as an evaluation clause
## Testing
from the Stellar shell execute various statements involving match such as:
```bash
ttofowler@Winterfell [13:01:16]
[~/src/apache/forks/metron/metron-stellar/stellar-common] [stellar_match]
-> % mvn exec:java \
-Dexec.mainClass="org.apache.metron.stellar.common.shell.StellarShell"
-Dexec.args="-l src/test/resources/log4j.properties"
[INFO] Scanning for projects...
[INFO]
[INFO]
------------------------------------------------------------------------
[INFO] Building stellar-common 0.4.1
[INFO]
------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ stellar-common ---
Stellar, Go!
Please note that functions are loading lazily in the background and will be
unavailable until loaded fully.
[Stellar]>>> Functions loaded, you may refer to functions now...
[Stellar]>>> foo := 1
[Stellar]>>> match{foo == 1 : TO_UPPER('ok')}
OK
[Stellar]>>>
```
### For all changes:
- [x] Is there a JIRA ticket associated with this PR? If not one needs to
be created at [Metron
Jira](https://issues.apache.org/jira/browse/METRON/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel).
- [x] Does your PR title start with METRON-XXXX where XXXX is the JIRA
number you are trying to resolve? Pay particular attention to the hyphen "-"
character.
- [x] Has your PR been rebased against the latest commit within the target
branch (typically master)?
### For code changes:
- [ ] Have you included steps to reproduce the behavior or problem that is
being changed or addressed?
- [x] Have you included steps or a guide to how the change may be verified
and tested manually?
- [x] Have you ensured that the full suite of tests and checks have been
executed in the root metron folder via:
```
mvn -q clean integration-test install && build_utils/verify_licenses.sh
```
- [x] Have you written or updated unit tests and or integration tests to
verify your changes?
- [ ] If adding new dependencies to the code, are these dependencies
licensed in a way that is compatible for inclusion under [ASF
2.0](http://www.apache.org/legal/resolved.html#category-a)?
- [ ] Have you verified the basic functionality of the build by building
and running locally with Vagrant full-dev environment or the equivalent?
### For documentation related changes:
- [x] Have you ensured that format looks appropriate for the output in
which it is rendered by building and verifying the site-book? If not then run
the following commands and the verify changes via
`site-book/target/site/index.html`:
```
cd site-book
mvn site
```
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ottobackwards/metron stellar_match
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/metron/pull/814.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #814
----
----
---