Skip to content

Move const to JsonReader #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ import scala.compiletime.{
}

private[tethys] trait JsonReaderDerivation:
def const[A](value: A): JsonReader[A] =
new JsonReader[A]:
override def read(it: TokenIterator)(implicit fieldName: FieldName): A =
if !it.currentToken().isObjectStart then
ReaderError.wrongJson(
"Expected object start but found: " + it.currentToken().toString
)
else {
it.skipExpression()
value
}

inline def derived[A](inline config: ReaderBuilder[A])(using
mirror: Mirror.ProductOf[A]
): JsonReader[A] =
Expand Down
14 changes: 13 additions & 1 deletion modules/core/src/main/scala/tethys/JsonReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tethys

import tethys.readers.instances.AllJsonReaders
import tethys.readers.tokens.TokenIterator
import tethys.readers.{FieldName, JsonReaderBuilder}
import tethys.readers.{FieldName, JsonReaderBuilder, ReaderError}

import scala.language.higherKinds

Expand All @@ -26,5 +26,17 @@ trait JsonReader[@specialized(specializations) A] {
object JsonReader extends AllJsonReaders with derivation.JsonReaderDerivation {
def apply[A](implicit jsonReader: JsonReader[A]): JsonReader[A] = jsonReader

def const[A](value: A): JsonReader[A] = new JsonReader[A] {
override def read(it: TokenIterator)(implicit fieldName: FieldName): A =
if (!it.currentToken().isObjectStart)
ReaderError.wrongJson(
"Expected object start but found: " + it.currentToken().toString
)
else {
it.skipExpression()
value
}
}

val builder: JsonReaderBuilder.type = JsonReaderBuilder
}