Skip to content
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

Fix New anonymous class definition will be duplicated at each inline site warning #132

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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 @@ -11,36 +11,32 @@ import neotype.*
inline given newtypeCodec[A, B](using
newType: Newtype.WithType[A, B],
inline config: CodecMakerConfig = CodecMakerConfig
): JsonValueCodec[B] =
new JsonValueCodec[B]:
private val codec = JsonCodecMaker.make[A](config)
): JsonValueCodec[B] = new NewtypeJsonValueCodec(JsonCodecMaker.make[A](config), newType)

override def decodeValue(in: JsonReader, default: B): B =
val decoded = codec.decodeValue(in, newType.unwrap(default))
newType.make(decoded) match
case Right(value) => value
case Left(value) => in.decodeError(s"Failed to decode $value")
private class NewtypeJsonValueCodec[A, B](aCodec: JsonValueCodec[A], newType: Newtype.WithType[A, B])
extends JsonValueCodec[B]:
override def decodeValue(in: JsonReader, default: B): B =
newType.make(aCodec.decodeValue(in, newType.unwrap(default))) match
case Right(value) => value
case Left(value) => in.decodeError(s"Failed to decode $value")

override def encodeValue(x: B, out: JsonWriter): Unit =
codec.encodeValue(newType.unwrap(x), out)
override def encodeValue(x: B, out: JsonWriter): Unit = aCodec.encodeValue(newType.unwrap(x), out)

override def nullValue: B = null.asInstanceOf[B]
override def nullValue: B = null.asInstanceOf[B]

// Subtype
inline given subtypeCodec[A, B <: A](using
subType: Subtype.WithType[A, B],
inline config: CodecMakerConfig = CodecMakerConfig
): JsonValueCodec[B] =
new JsonValueCodec[B]:
private val codec = JsonCodecMaker.make[A](config)
): JsonValueCodec[B] = new SubtypeJsonValueCodec(JsonCodecMaker.make[A](config), subType)

override def decodeValue(in: JsonReader, default: B): B =
val decoded = codec.decodeValue(in, default)
subType.make(decoded) match
case Right(value) => value
case Left(value) => in.decodeError(s"Failed to decode $value")
private class SubtypeJsonValueCodec[A, B <: A](aCodec: JsonValueCodec[A], subType: Subtype.WithType[A, B])
extends JsonValueCodec[B]:
override def decodeValue(in: JsonReader, default: B): B =
subType.make(aCodec.decodeValue(in, default)) match
case Right(value) => value
case Left(value) => in.decodeError(s"Failed to decode $value")

override def encodeValue(x: B, out: JsonWriter): Unit =
codec.encodeValue(x, out)
override def encodeValue(x: B, out: JsonWriter): Unit = aCodec.encodeValue(x, out)

override def nullValue: B = null.asInstanceOf[B]
override def nullValue: B = null.asInstanceOf[B]