Skip to content

Commit

Permalink
#55 add fieldName to KeyReader.read
Browse files Browse the repository at this point in the history
  • Loading branch information
eld0727 committed Jul 3, 2019
1 parent 87cf690 commit 87c93d5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ lazy val benchmarks = project.in(file("./modules/benchmarks"))
"org.json4s" %% "json4s-native" % "3.6.7",
"org.json4s" %% "json4s-jackson" % "3.6.7",
"com.typesafe.play" %% "play-json" % "2.7.4",
"io.circe" %% "circe-core" % "0.12.0-M1",
"io.circe" %% "circe-generic" % "0.12.0-M1",
"io.circe" %% "circe-jawn" % "0.12.0-M1",
"io.circe" %% "circe-jackson29" % "0.12.0-M1",
"io.circe" %% "circe-core" % "0.12.0-M3",
"io.circe" %% "circe-generic" % "0.12.0-M3",
"io.circe" %% "circe-jawn" % "0.12.0-M3",
"io.circe" %% "circe-jackson29" % "0.12.0-M3",

"org.knowm.xchart" % "xchart" % "3.5.4" exclude("de.erichseifert.vectorgraphics2d", "VectorGraphics2D") withSources()
),
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/main/scala/tethys/readers/KeyReader.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tethys.readers

trait KeyReader[A] {
def read(s: String): A
def read(s: String)(implicit fieldName: FieldName): A
}

object KeyReader {
implicit lazy val stringKeyReader: KeyReader[String] = new KeyReader[String] {
override def read(s: String): String = s
override def read(s: String)(implicit fieldName: FieldName): String = s
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ private[tethys] trait LowPriorityMapReaders extends IterableReaders {
builder.result()
case token if token.isFieldName =>
val name = it.fieldName()
appendBuilder(it.next(), builder, keyReader.read(name))(fieldName.appendFieldName(name))
val nextFieldName = fieldName.appendFieldName(name)
appendBuilder(it.next(), builder, keyReader.read(name)(nextFieldName))(nextFieldName)
recRead(it, builder)(fieldName)

case token => ReaderError.wrongJson(s"Expect end of object or field name but '$token' found")(fieldName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tethys.writers.KeyWriter
import tethys.{JsonReader, JsonWriter}

object Enumeratum {
private val RootFieldName = FieldName()

def reader[A <: EnumEntry](enum: Enum[A]): JsonReader[A] = new JsonReader[A] {
def read(it: TokenIterator)(implicit fieldName: FieldName): A =
Expand All @@ -18,7 +17,7 @@ object Enumeratum {
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): A = decode(enum)(fn, str)(RootFieldName)
def read(str: String)(implicit fieldName: FieldName): A = decode(enum)(fn, str)
}

def keyWriter[A](fn: A => String): KeyWriter[A] = new KeyWriter[A] {
Expand All @@ -36,7 +35,7 @@ object Enumeratum {
enum: ValueEnum[ValueType, EntryType]
): JsonWriter[EntryType] = JsonWriter[ValueType].contramap[EntryType](_.value)

private [this] 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")
Expand Down

0 comments on commit 87c93d5

Please sign in to comment.