LoginSignup
1
0

More than 3 years have passed since last update.

Akka HTTPの Directive (path や parameter)で 独自に定義したクラスに変換する

Posted at

願望

IntLong などの プリミティブ型 ではなく 独自に定義した 値クラス などを Akka HTTP の route 定義で使いたい

(意図しない使用を回避するため)

独自に定義したクラス

final case class AccountId(value: Long) extends AnyVal
final case class Amount(value: Int) extends AnyVal

改善前

プリミティブ型なので、独自定義の型への変換を自分でやる必要がある

Path

pathPrefix("account" / LongNumber) { rawAccountId: Long =>
  val accountId = AccountId(rawAccountId)

Parameter

parameter("amount".as[Int]) { rawAmount: Int => 
  val amount = Amount(rawAmount)

改善後

Akka HTTPが型変換をするので、プリミティブ型は登場しない。

Path

PathMatcher1[T]def map[R](f: T ⇒ R): PathMatcher1[R] を使用して型を変換

pathPrefix("account" / LongNumber.map(AccountId.apply)) { accountId: AccountId =>

各型の対応

  • TLong
  • RAccountId

Parameter

NameReceptacle[T]def as[B](unmarshaller: Unmarshaller[T, B]) を使用して型を変換

Unmarshaller[T, B] が必要なので、 Unmarshaller.strict[A, B](f: A ⇒ B): Unmarshaller[A, B] を使用して作成

implicit class ToNameUnmarshallerReceptacleEnhancements(name: String) {
  def asAmount: NameUnmarshallerReceptacle[Amount] =
    name.as[Int].as[Amount](Unmarshaller.strict(Amount.apply))
}
parameter("amount".asAmount) { amount: Amount =>

各型の対応

  • TInt
  • AInt
  • BAmount
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0