From 87c93d5498c6066f865aad95a5f1440f4e297f6c Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 3 Jul 2019 06:59:19 +0300 Subject: [PATCH] #55 add fieldName to KeyReader.read --- build.sbt | 8 ++++---- .../core/src/main/scala/tethys/readers/KeyReader.scala | 4 ++-- .../main/scala/tethys/readers/instances/MapReaders.scala | 3 ++- .../src/main/scala/tethys/enumeratum/Enumeratum.scala | 5 ++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/build.sbt b/build.sbt index db91730b..ccddcb89 100644 --- a/build.sbt +++ b/build.sbt @@ -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() ), diff --git a/modules/core/src/main/scala/tethys/readers/KeyReader.scala b/modules/core/src/main/scala/tethys/readers/KeyReader.scala index 6c45e800..f600420b 100644 --- a/modules/core/src/main/scala/tethys/readers/KeyReader.scala +++ b/modules/core/src/main/scala/tethys/readers/KeyReader.scala @@ -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 } } diff --git a/modules/core/src/main/scala/tethys/readers/instances/MapReaders.scala b/modules/core/src/main/scala/tethys/readers/instances/MapReaders.scala index 7856219a..6cd964c0 100644 --- a/modules/core/src/main/scala/tethys/readers/instances/MapReaders.scala +++ b/modules/core/src/main/scala/tethys/readers/instances/MapReaders.scala @@ -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) diff --git a/modules/enumeratum/src/main/scala/tethys/enumeratum/Enumeratum.scala b/modules/enumeratum/src/main/scala/tethys/enumeratum/Enumeratum.scala index a5590148..4e3ba815 100644 --- a/modules/enumeratum/src/main/scala/tethys/enumeratum/Enumeratum.scala +++ b/modules/enumeratum/src/main/scala/tethys/enumeratum/Enumeratum.scala @@ -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 = @@ -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] { @@ -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")