Skip to content

Commit

Permalink
Add map and mapO methods for extractors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdziuban committed Feb 3, 2025
1 parent 1b6d23e commit 9f8374a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 16 additions & 2 deletions core/src/main/scala/routing/extractor/PathExtractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ package extractor
import java.util.UUID
import scala.util.Try

trait PathExtractor[A] {
trait PathExtractor[A] { self =>
def extract(path: String): Option[A]

final def mapO[B](f: A => Option[B]): PathExtractor[B] =
new PathExtractor[B] {
def extract(path: String): Option[B] = self.extract(path).flatMap(f)
}

final def map[B](f: A => B): PathExtractor[B] = mapO(a => Some(f(a)))
}

trait RestOfPathExtractor[A] {
trait RestOfPathExtractor[A] { self =>
def extract(path: String): Option[A]

final def mapO[B](f: A => Option[B]): RestOfPathExtractor[B] =
new RestOfPathExtractor[B] {
def extract(path: String): Option[B] = self.extract(path).flatMap(f)
}

final def map[B](f: A => B): RestOfPathExtractor[B] = mapO(a => Some(f(a)))
}

object PathExtractor {
Expand Down
9 changes: 8 additions & 1 deletion core/src/main/scala/routing/extractor/QueryExtractor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import java.util.UUID
import scala.annotation.tailrec
import scala.util.Try

trait QueryExtractor[A] {
trait QueryExtractor[A] { self =>
def extract(key: String, query: QueryMap): Option[A]

final def mapO[B](f: A => Option[B]): QueryExtractor[B] =
new QueryExtractor[B] {
def extract(key: String, query: QueryMap): Option[B] = self.extract(key, query).flatMap(f)
}

final def map[B](f: A => B): QueryExtractor[B] = mapO(a => Some(f(a)))
}

object QueryExtractor {
Expand Down

0 comments on commit 9f8374a

Please sign in to comment.