Maybe dumb, but is there a way to write a when sta...
# getting-started
c
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.
Copy code
when (request.path) {
    "/api/one/two" ->
        return "blah"
    "/api/three/four?arg=myArg" ->
        return "boop"
I want the second case to basically be
api/three/four*
instead of hard coding the arg. Or do I have to basically refactor this and just use endsWith or something?
d
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
Copy code
when {
   request.path == “/api/one/two“ ->
request.path.startsWith(“/api/three/four”) ->
Or even
Copy code
Val path = request.path
when {
   path ==
You can also use a with outside the when to achieve something similar and less repeating https://stackoverflow.com/a/54254623
c
Never heard of
with
lol. Thanks!
Just re-read your first message. That's actually a good point. Let me see what request.path actually is.