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

feat: scala 3 derivation #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

i10416
Copy link

@i10416 i10416 commented Dec 14, 2024

close #5
This PR adds Scala 3 derivation.
Supported features are

  • plain case class
  • field with default value
  • Option field

It 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.

@i10416 i10416 changed the title feat: scala 3 derivation baseline feat: scala 3 derivation Dec 14, 2024
@@ -9,12 +9,12 @@ trait Codec[A] {
defaults: Codec.Defaults,
index: Int
): Either[Parse.Error, A]
private[toml] def optional: Boolean = false
Copy link
Author

@i10416 i10416 Dec 14, 2024

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 =>
Copy link
Author

@i10416 i10416 Dec 14, 2024

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.

Copy link
Author

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])
Copy link
Author

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])
Copy link
Author

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[?].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scala 3 derivation
1 participant