-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: scala 3 derivation #8
base: master
Are you sure you want to change the base?
Conversation
@@ -9,12 +9,12 @@ trait Codec[A] { | |||
defaults: Codec.Defaults, | |||
index: Int | |||
): Either[Parse.Error, A] | |||
private[toml] def optional: Boolean = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field makes it easier to handle default value for missing field. toml does not have the concept of null
or nil
, so we cannot simply convert null into NullValue like AST node.
import org.scalatest.funsuite.AnyFunSuite | ||
import toml._, derivation.auto._ | ||
|
||
trait CodecSpecExtras {self: CodecSpec => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cannot pass this test yet... I hope someone will solve it in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, you can now enjoy most of the niceties of codec derivation.
case None => | ||
Right( | ||
d.defaultParams.get(witnessName) | ||
.map(_.asInstanceOf[t]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaultParams is Map[Strng, Any]
, so you need asInstanceOf
.left.map((a,m) => (witnessName +: a, m)) | ||
case None => | ||
d.defaultParams.get(witnessName) match | ||
case None if codec.optional => Right(None.asInstanceOf[t]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional returns true
if and only if codec is of type Codec[Option[?]]
, so it is safe to use None.asInstanceOf[t]
where t
is of type Option[?]
.
close #5
This PR adds Scala 3 derivation.
Supported features are
Option
fieldIt pass all the test except one which uses recursive type, optional value and default value.
I keep it as is in the hope that someone will solve the problem in the future...
See core/src/test/scala-3/toml/CodecSpec.scala.