camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lorenzo Bolzani <l.bolz...@gmail.com>
Subject Camel DSL: generic match proposal
Date Thu, 21 Nov 2013 09:11:46 GMT
Hi,
working on a small project (https://bitbucket.org/nottrz/when) I noticed
that it's not easy to pass arguments for expression evaluation (or at least
I did not found out how).

The idea is to add a method similar to isEqualTo to perform a generic
"match" on the left part of the expression.

header("location").isAMatchTo(regex("[a-zA-Z]+,London,UK"))

In this example you can see that a regex is just a special case of this
kind of "match". The expression on the right receives the left value as a
parameter during evaluation.

Of course you can already do this, but the effort for the "language plugin"
developer is higher, because you need a custom "resolve syntax" to access
the header values, like this:

groovy("properties.resolve(request.headers.get('foo')) % 2 == 0")

where you could use

header("foo").isAMatchTo("val % 2 == 0")

where "val" is a magic variable holding the left value (header, body,
ecc.). Any language can choose what to do with the left value (name,
syntax, ecc.).


The xpath language uses this

xpath("in:header('foo') = 'bar'")

and could be

header("foo").isAMatchTo("val = 'bar'"))


This would allow expressions like this for the immaginary "math" language
plugin:

header("size").isAMatchTo(language("math", "x^2 + x*3 > 300"))

and with one more step:

header("size").evaluateOn(language("math", "x^2 + x*3"))


I'm new to Camel so I do not know if this could be of real use or not. My
toy language could benefit from this.


I wrote a small patch to make this test pass:

public void testIsAMatchTo() throws Exception {

  // if the right expression does not accept arguments fall back to
isEqualTo
  assertMatches(header("name").isAMatchTo(constant("James")));
  assertDoesNotMatch(header("name").isAMatchTo(constant("Claus")));


assertMatches(header("location").isAMatchTo(regex("[a-zA-Z]+,London,UK")));

assertDoesNotMatch(header("location").isAMatchTo(regex("[a-zA-Z]+,Westminster,[a-zA-Z]+")));

  assertMatches(header("name").isAMatchTo(language("simple", "James")));
  assertDoesNotMatch(header("name").isAMatchTo(language("simple",
"Claus")));

  // TODO: this already works, but I do not know how to register a "test
language" inside a test
  //assertMatches(header("size").isAMatchTo(language("math", "123 > x <
345")));
  //assertDoesNotMatch(header("size").isAMatchTo(language("math", "x^2 +
x*3 > 300")));
}


Let me know what do you think, if it could be useful to open a bug to
better evaluate this or not.


Bye

Lorenzo

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message