Path matching and extracting on multiple segments

4563 views
Skip to first unread message

stevem

unread,
Aug 29, 2013, 6:01:39 AM8/29/13
to spray...@googlegroups.com
Hi

Spent a frustrating hour trying to work out how to do this but hopefully this is trivial for more experienced spray folks.

Given the path: "{special-prefix}" / p1 / p2 / p3 / "{special-suffix}".

How could the p1 / p2 / p3 segments be extracted as a string value?

Note, there would be other routes such as "{special-prefix}" / p1 / p2 /p3 that would be matched and handled separately.

Regads
Steve

Mathias Doenitz

unread,
Aug 29, 2013, 6:05:03 AM8/29/13
to spray...@googlegroups.com
Steve,

would

path("foo" / Segment / Segment / Segment / "bar") { (p1, p2, p3) =>
...
}

do the trick?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

stevem

unread,
Aug 29, 2013, 9:26:08 AM8/29/13
to spray...@googlegroups.com
Matthias

Thanks for the reply.

In my case the number of segments in unknown and variable so this wouldn't be flexible enough.

Cheers
Steve

Johannes Rudolph

unread,
Aug 29, 2013, 9:38:49 AM8/29/13
to spray...@googlegroups.com
Hi Steve,

then you can use something like this:

pathPrefix("foo") {
  pathSuffix("bar") {
    path(RestPath) { path =>
      complete(s"The inner path is $path")
    }
  }
}

path is of type `Path` which is basically a special kind of linked list which contains of `Slash`, `Segment` and an `Empty` element. You would need some deconstruction logic which extracts the elements in the way you need it.

Johannes
--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Johannes Rudolph

unread,
Aug 29, 2013, 9:49:35 AM8/29/13
to spray...@googlegroups.com
Or you use a new PathMatcher:

object Segments extends PathMatcher1[List[String]] {
    def apply(path: Path): Matching[List[String] :: HNil] =
      path match {
        case Path.Segment(segment, tail) ⇒
          apply(tail).map {
            case rest :: HNil ⇒ (segment :: rest) :: HNil
          }
        case Path.Slash(tail) ⇒ apply(tail)
        case Path.Empty       ⇒ Matched(Path.Empty, Nil :: HNil)
      }
  }


then you can use something like this (where segs is of type List[String]):

pathPrefix("foo") {
  pathSuffix("bar") {
    path(Segments) { segs =>
      complete(s"The inner segments are $segs")
    }
  }
}

 

stevem

unread,
Sep 1, 2013, 11:57:07 PM9/1/13
to spray...@googlegroups.com, johannes...@googlemail.com

That solved the issue for me.

Many thanks for the help on this.

Nicolas Colomer

unread,
Nov 17, 2016, 3:29:20 AM11/17/16
to spray.io User List
Heya,

I try to achieve exactly this, but it doesn't compile : I get "Missing parameter type" errors.

val route: Route =
path(
"foo" / Segment / Segment / Segment / "bar") { (p1, p2, p3) =>
complete(
OK)
}

It's probably a dummy question, I'm sure I am missing something (obvious)
I'm using the last spray verion ("com.typesafe.akka/akka-http" 3.0.0-RC1)

Cheers,
Nicolas

Nicolas Colomer

unread,
Nov 17, 2016, 3:44:27 AM11/17/16
to spray...@googlegroups.com
Sorry for the previous message, I finally fixed the issue by upgrading IntelliJ to the latest version.
Cheers, Nicolas

--
You received this message because you are subscribed to a topic in the Google Groups "spray.io User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spray-user/XXAaoubOEqg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spray-user+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/1a489092-d12d-4555-9270-0f489ed9e748%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Age Mooij

unread,
Dec 12, 2016, 6:53:35 AM12/12/16
to spray...@googlegroups.com
Hi Nicolas. 
For akka-http, please ask your questions on the akka-http mailing list or Gitter channel. You will get much quicker answers there because it is an actively supported project.

See below for the list of community support channels for akka-http:


This mailing list is about the (deprecated) Spray project and does not get much traffic/attention anymore.

Age

You received this message because you are subscribed to the Google Groups "spray.io User List" group.

To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages