Colton Idle

07/09/2021, 3:24 AM
Maybe dumb, but is there a way to write a when state for strings, but for one of the string cases I want there to be a wild card at the end? My scenario is when matching routes but some routes have arguments at the end.
when (request.path) {
    "/api/one/two" ->
        return "blah"
    "/api/three/four?arg=myArg" ->
        return "boop"
I want the second case to basically be
instead of hard coding the arg. Or do I have to basically refactor this and just use endsWith or something?

Dave K

07/09/2021, 6:40 AM
Shouldn’t your request path already not include query params? Either way, IIRC the when with a variable in context can only do equals, is, or in type comparisons but if you make it just an empty when it would allow you to write expressions that evaluate to some Boolean which could include a startsWith or some regex comparison
when {
   request.path == “/api/one/two“ ->
request.path.startsWith(“/api/three/four”) ->
Or even
Val path = request.path
when {
   path ==
You can also use a with outside the when to achieve something similar and less repeating

Colton Idle

07/09/2021, 3:05 PM
Never heard of
lol. Thanks!
Just re-read your first message. That's actually a good point. Let me see what request.path actually is.