sparsetech / trail

Routing library for the Scala platform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Match error for 3 or more Params

windbird123 opened this issue · comments

compile error when 3 or more params

    val details = Root / "details" & Param[String]("engine") & Param[String]("device") & Param[String]("query")
    val s       = "/details?engine=google&device=mobile&query=covid19"
    s match {
      case details(engine, device, query) =>
        println(s"engine=$engine")
        println(s"device=$device")
        println(s"query=$query")
      case _ =>
    }
  }

Result:

too many patterns for trait Route offering ((String, String), String): expected 2, found 3
case details(engine, device, query) =>

And if I change the code like below

    val details = Root / "details" & Param[String]("engine") & Param[String]("device") & Param[String]("query")
    val s       = "/details?engine=google&device=mobile&query=covid19"
    s match {
      case details(x, y) =>
        println(s"x=$x")
        println(s"y=$y")
      case _ =>
    }

Result:

x=(google,mobile)
y=covid19

@windbird123

the output gives the hint: params are nested as tuples to the left and you need to match them as such:

import trail._

val details = Root / "details" & Param[String]("engine") & Param[String]("device") & Param[String]("query")
val s = "/details?engine=google&device=mobile&query=covid19&fourth=4"

s match {
  case details((engine, device), query) =>
    println(s"params: $engine, $device, $query")
  case _ =>
}

params: google, mobile, covid19

if you would add a fourth:

case details(((engine, device), query), fourth)

@adrobisch thank you.