Search code examples
regexwildcardkotlin

Regex or Wildcard in Kotlin's when statement?


I'm working on a RESTful app in Kotlin and for the router, I'm using a when statement, as it's the most readable and good looking conditional.

Is there a way to use Regex or a wildcard in the when statement for a string?

(So that URIs like "/article/get/" would all be passed to the same controller)

The structure of my router is as follows:

when(uri) {
    "some/url" -> return SomeController(config).someAction(session)
}

Solution

  • Yes.

    import kotlin.text.regex
    
    val regex1 = Regex( /* pattern */ )
    val regex2 = Regex( /* pattern */ )
    /* etc */
    
    when {
        regex1.matches(uri) -> /* do stuff */
        regex2.matches(uri) -> /* do stuff */
        /* etc */
    }
    

    You could also use containsMatchIn if that suits your needs better than matches.

    Explanation:

    The test expression of a when statement is optional. If no test expression is included, then the when statement functions like an if-else if chain, where the whenCondition of each whenEntry shall independently evaluate to a boolean.


    EDIT:

    So I thought about it for awhile, and I came up with a different approach that might be closer to what you want.

    import kotlin.text.regex
    
    when (RegexWhenArgument(uri)) {
        Regex(/* pattern */) -> /* do stuff */
        Regex(/* pattern */) -> /* do stuff */
        /* etc */
    }
    

    Where RegexWhenArgument is minimally defined as:

    class RegexWhenArgument (val whenArgument: CharSequence) {
        operator fun equals(whenEntry: Regex) = whenEntry.matches(whenArgument)
        override operator fun equals(whenEntry: Any?) = (whenArgument == whenEntry)
    }
    

    This approach lets you get as close as possible to the "argument-ful" when expression syntax. I think it's about as streamlined and readable as it can be (assuming that you define the RegexWhenArgument class elsewhere).

    This approach uses something similar to the visitor design pattern in combination with Kotlin's operator overloading to redefine what constitutes a "match" between a when expression argument and a whenEntry. If you really wanted to, I suppose you could take this approach a step further and generify RegexWhenArgument into a general-purpose WhenArgument and WhenArgumentDecorator that allows you to specify custom "match" criteria in a when expression for any sort of type, not just Regex.