aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/database/Converters.scala
blob: cfc5afd2b396c96ad42174d7d5abbae5d10bdf6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package xyz.driver.core.database

import xyz.driver.core.rest.errors.DatabaseException

import scala.reflect.ClassTag

trait Converters {
  def fromStringOrThrow[T](entityStr: String, mapper: (String => Option[T]), entityName: String): T =
    mapper(entityStr).getOrElse(throw DatabaseException(s"Invalid $entityName in database: $entityStr"))

  def expectValid[T](mapper: String => Option[T], query: String)(implicit ct: ClassTag[T]): T = {
    fromStringOrThrow[T](query, mapper, ct.toString())
  }
}