Skip to content

Commit

Permalink
update scala version and libs
Browse files Browse the repository at this point in the history
  • Loading branch information
e.ustyantseva committed Nov 7, 2023
1 parent 48950a9 commit 5ad18bc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ object CollectionBuilder {

implicit def iterableFactoryCollectionBuilder[A, C[
X
] <: IterableFactoryDefaults[X, C]]
: IterableFactoryCollectionBuilder[A, C] = macro
CollectionBuilderMacroImpl.fromIterableFactory[A, C]
] <: IterableFactoryDefaults[X, C]]: IterableFactoryCollectionBuilder[A, C] =
macro CollectionBuilderMacroImpl.fromIterableFactory[A, C]
implicit def mapFactoryCollectionBuilder[K, V, M[X, Y] <: Map[X, Y]]
: MapFactoryCollectionBuilder[K, V, M] = macro
CollectionBuilderMacroImpl.fromMapFactory[K, V, M]
: MapFactoryCollectionBuilder[K, V, M] =
macro CollectionBuilderMacroImpl.fromMapFactory[K, V, M]

private class CollectionBuilderMacroImpl(val c: blackbox.Context) {
import c.universe._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,41 @@ object Enumeratum {
decode(enum)(_.withNameOption, JsonReader.stringReader.read(it))
}

def writer[A <: EnumEntry](enum: Enum[A]): JsonWriter[A] = JsonWriter.stringWriter.contramap[A](_.entryName)
def writer[A <: EnumEntry](enum: Enum[A]): JsonWriter[A] =
JsonWriter.stringWriter.contramap[A](_.entryName)

def keyReader[E, A](enum: E)(fn: E => String => Option[A]): KeyReader[A] = new KeyReader[A] {
def read(str: String)(implicit fieldName: FieldName): A = decode(enum)(fn, str)
}
def keyReader[E, A](enum: E)(fn: E => String => Option[A]): KeyReader[A] =
new KeyReader[A] {
def read(str: String)(implicit fieldName: FieldName): A =
decode(enum)(fn, str)
}

def keyWriter[A](fn: A => String): KeyWriter[A] = new KeyWriter[A] {
def toKey(value: A): String = fn(value)
}

def valueReader[ValueType: JsonReader, EntryType <: ValueEnumEntry[ValueType]](
def valueReader[
ValueType: JsonReader,
EntryType <: ValueEnumEntry[ValueType]
](
enum: ValueEnum[ValueType, EntryType]
): JsonReader[EntryType] = new JsonReader[EntryType] {
def read(it: TokenIterator)(implicit fieldName: FieldName): EntryType =
decode(enum)(_.withValueOpt, JsonReader[ValueType].read(it))
}

def valueWriter[ValueType: JsonWriter, EntryType <: ValueEnumEntry[ValueType]](
def valueWriter[
ValueType: JsonWriter,
EntryType <: ValueEnumEntry[ValueType]
](
enum: ValueEnum[ValueType, EntryType]
): JsonWriter[EntryType] = JsonWriter[ValueType].contramap[EntryType](_.value)

def decode[E, A, V](enum: E)(fn: E => V => Option[A], value: V)(implicit fieldName: FieldName): A =
def decode[E, A, V](
enum: E
)(fn: E => V => Option[A], value: V)(implicit fieldName: FieldName): A =
fn(enum)(value) match {
case Some(result) => result
case _ => ReaderError.wrongJson(s"$value is not a member of enum $enum")
case _ => ReaderError.wrongJson(s"$value is not a member of enum $enum")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import scala.language.experimental.macros

trait AutoDerivation {
implicit def jsonWriterMaterializer[A]
: LowPriorityInstance[JsonObjectWriter[A]] = macro
AutoDerivationMacro.jsonWriter[A]
implicit def jsonReaderMaterializer[A]
: LowPriorityInstance[JsonReader[A]] = macro
AutoDerivationMacro.jsonReader[A]
: LowPriorityInstance[JsonObjectWriter[A]] =
macro AutoDerivationMacro.jsonWriter[A]
implicit def jsonReaderMaterializer[A]: LowPriorityInstance[JsonReader[A]] =
macro AutoDerivationMacro.jsonReader[A]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import scala.annotation.compileTimeOnly
import scala.language.experimental.macros

trait SemiautoDerivation {
def jsonWriter[A]: JsonObjectWriter[A] = macro
SemiautoDerivationMacro.simpleJsonWriter[A]
def jsonWriter[A](description: WriterDescription[A]): JsonObjectWriter[A] = macro
SemiautoDerivationMacro.describedJsonWriter[A]
def jsonWriter[A](builder: WriterBuilder[A]): JsonObjectWriter[A] = macro
SemiautoDerivationMacro.jsonWriterWithBuilder[A]
def jsonWriter[A](config: WriterDerivationConfig): JsonObjectWriter[A] = macro
SemiautoDerivationMacro.jsonWriterWithConfig[A]

def describe[A](builder: WriterBuilder[A]): WriterDescription[A] = macro
WriterDescriptorMacro.simpleDescription[A]

def jsonReader[A]: JsonReader[A] = macro
SemiautoDerivationMacro.simpleJsonReader[A]
def jsonReader[A](description: ReaderDescription[A]): JsonReader[A] = macro
SemiautoDerivationMacro.describedJsonReader[A]
def jsonReader[A](builder: ReaderBuilder[A]): JsonReader[A] = macro
SemiautoDerivationMacro.jsonReaderWithBuilder[A]
def jsonReader[A](config: ReaderDerivationConfig): JsonReader[A] = macro
SemiautoDerivationMacro.jsonReaderWithConfig[A]

def describe[A](builder: ReaderBuilder[A]): ReaderDescription[A] = macro
ReaderDescriptionMacro.readerDescription[A]
def jsonWriter[A]: JsonObjectWriter[A] =
macro SemiautoDerivationMacro.simpleJsonWriter[A]
def jsonWriter[A](description: WriterDescription[A]): JsonObjectWriter[A] =
macro SemiautoDerivationMacro.describedJsonWriter[A]
def jsonWriter[A](builder: WriterBuilder[A]): JsonObjectWriter[A] =
macro SemiautoDerivationMacro.jsonWriterWithBuilder[A]
def jsonWriter[A](config: WriterDerivationConfig): JsonObjectWriter[A] =
macro SemiautoDerivationMacro.jsonWriterWithConfig[A]

def describe[A](builder: WriterBuilder[A]): WriterDescription[A] =
macro WriterDescriptorMacro.simpleDescription[A]

def jsonReader[A]: JsonReader[A] =
macro SemiautoDerivationMacro.simpleJsonReader[A]
def jsonReader[A](description: ReaderDescription[A]): JsonReader[A] =
macro SemiautoDerivationMacro.describedJsonReader[A]
def jsonReader[A](builder: ReaderBuilder[A]): JsonReader[A] =
macro SemiautoDerivationMacro.jsonReaderWithBuilder[A]
def jsonReader[A](config: ReaderDerivationConfig): JsonReader[A] =
macro SemiautoDerivationMacro.jsonReaderWithConfig[A]

def describe[A](builder: ReaderBuilder[A]): ReaderDescription[A] =
macro ReaderDescriptionMacro.readerDescription[A]

implicit class ReaderFieldStringOps(val s: String) {

Expand Down

0 comments on commit 5ad18bc

Please sign in to comment.