-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
815 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package enumeratum | ||
|
||
import anorm.{Column, TypeDoesNotMatch} | ||
|
||
private[enumeratum] object AnormColumn { | ||
def column[A <: EnumEntry](enum: Enum[A], insensitive: Boolean): Column[A] = | ||
if (insensitive) { | ||
parse[A](enum)(enum.withNameInsensitiveOption) | ||
} else { | ||
parse[A](enum)(enum.withNameOption) | ||
} | ||
|
||
def lowercaseOnlyColumn[A <: EnumEntry](enum: Enum[A]): Column[A] = | ||
parse[A](enum)(enum.withNameLowercaseOnlyOption) | ||
|
||
def uppercaseOnlyColumn[A <: EnumEntry](enum: Enum[A]): Column[A] = | ||
parse[A](enum)(enum.withNameUppercaseOnlyOption) | ||
|
||
// --- | ||
|
||
private def parse[A <: EnumEntry](enum: Enum[A])(extract: String => Option[A]): Column[A] = | ||
Column.nonNull[A] { | ||
case (s: String, _) => | ||
extract(s) match { | ||
case Some(result) => Right(result) | ||
case None => Left(TypeDoesNotMatch(s"Invalid value: $s")) | ||
} | ||
|
||
case (_, meta) => | ||
Left( | ||
TypeDoesNotMatch( | ||
s"Column '${meta.column.qualified}' expected to be String; Found ${meta.clazz}")) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package enumeratum | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.{Column, ToStatement} | ||
|
||
/** | ||
* Provides instances for Anorm typeclasses: | ||
* | ||
* - [[anorm.Column]] | ||
* - [[anorm.ToStatement]] | ||
*/ | ||
trait AnormEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val column: Column[A] = | ||
AnormColumn.column[A](self, insensitive = false) | ||
|
||
implicit val toStatement: ToStatement[A] = new ToStatement[A] { | ||
def set(s: PreparedStatement, i: Int, v: A) = s.setString(i, v.entryName) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
enumeratum-anorm/src/main/scala/AnormInsensitiveEnum.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package enumeratum | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.{Column, ToStatement} | ||
|
||
/** | ||
* Provides insensitive instances for Anorm typeclasses: | ||
* | ||
* - [[anorm.Column]] | ||
* - [[anorm.ToStatement]] | ||
*/ | ||
trait AnormInsensitiveEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val column: Column[A] = | ||
AnormColumn.column[A](self, insensitive = true) | ||
|
||
implicit val toStatement = new ToStatement[A] { | ||
def set(s: PreparedStatement, i: Int, v: A) = | ||
s.setString(i, v.entryName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package enumeratum | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.{Column, ToStatement} | ||
|
||
/** | ||
* Provides lowercase instances for Anorm typeclasses: | ||
* | ||
* - [[anorm.Column]] | ||
* - [[anorm.ToStatement]] | ||
*/ | ||
trait AnormLowercaseEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val column: Column[A] = | ||
AnormColumn.lowercaseOnlyColumn[A](self) | ||
|
||
implicit val toStatement = new ToStatement[A] { | ||
def set(s: PreparedStatement, i: Int, v: A) = | ||
s.setString(i, v.entryName.toLowerCase) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package enumeratum | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.{Column, ToStatement} | ||
|
||
/** | ||
* Provides uppercase instances for Anorm typeclasses: | ||
* | ||
* - [[anorm.Column]] | ||
* - [[anorm.ToStatement]] | ||
*/ | ||
trait AnormUppercaseEnum[A <: EnumEntry] { self: Enum[A] => | ||
implicit val column: Column[A] = | ||
AnormColumn.uppercaseOnlyColumn[A](self) | ||
|
||
implicit val toStatement = new ToStatement[A] { | ||
def set(s: PreparedStatement, i: Int, v: A) = | ||
s.setString(i, v.entryName.toUpperCase) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package enumeratum.values | ||
|
||
import anorm.{Column, TypeDoesNotMatch} | ||
|
||
private[values] object AnormColumn { | ||
def apply[ValueType, EntryType <: ValueEnumEntry[ValueType]]( | ||
enum: ValueEnum[ValueType, EntryType] | ||
)( | ||
implicit baseColumn: Column[ValueType] | ||
): Column[EntryType] = Column.nonNull[EntryType] { | ||
case (value, meta) => | ||
baseColumn(value, meta) match { | ||
case Left(err) => | ||
Left(err) | ||
|
||
case Right(s) => | ||
enum.withValueOpt(s) match { | ||
case Some(obj) => Right(obj) | ||
case None => Left(TypeDoesNotMatch(s"Invalid value: $s")) | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
enumeratum-anorm/src/main/scala/values/AnormToStatement.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package enumeratum.values | ||
|
||
import java.sql.PreparedStatement | ||
|
||
import anorm.ToStatement | ||
|
||
private[values] object AnormToStatement { | ||
def apply[ValueType, EntryType <: ValueEnumEntry[ValueType]]( | ||
enum: ValueEnum[ValueType, EntryType] | ||
)( | ||
implicit baseToStmt: ToStatement[ValueType] | ||
): ToStatement[EntryType] = new ToStatement[EntryType] { | ||
def set(s: PreparedStatement, i: Int, e: EntryType) = | ||
baseToStmt.set(s, i, e.value) | ||
} | ||
} |
Oops, something went wrong.