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

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

import scala.reflect.ClassTag

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

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