zio-schema
zio-schema copied to clipboard
Unable to convert to TypedValue
If the schema is rebuilt from the AST, it doesn't correctly convert a dynamic value back to a typed value. This is specifically happening with records.
import zio.schema._
final case class User(name: String, age: Int)
implicit val schema = DeriveSchema.gen[User]
val user = User("John", 30)
val userDynamic = schema.toDynamic(user)
println(userDynamic)
println(userDynamic.toTypedValue(schema))
println(userDynamic.toTypedValue(schema.ast.toSchema))
https://scastie.scala-lang.org/TJmOjesrS5KY5ZYHOwJTUg
/bounty $200
💎 $200 bounty • ZIO
Steps to solve:
-
Start working: Comment
/attempt #511
with your implementation plan -
Submit work: Create a pull request including
/claim #511
in the PR body to claim the bounty - Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts
Additional opportunities:
-
🔴 Livestream on Algora TV while solving this bounty & earn $200 upon merge! Make sure to have your camera and microphone on. Comment
/livestream
once live
Thank you for contributing to zio/zio-schema!
Add a bounty • Share on socials
Attempt | Started (GMT+0) | Solution |
---|---|---|
🔴 @alankritdabral | Nov 12, 2023, 12:25:29 PM | WIP |
🔴 @foxmoder | Jan 7, 2024, 10:52:07 PM | WIP |
Looking at zio-schema architecture I don't understand how would it work. Using schema.toDynamic
and schema.ast
move object and schema from typesafe to typeless world.
While toTypedValue(schema)
can return it back to typesafe because schema
has type Schema[A]
, userDynamic.toTypedValue(schema.ast.toSchema)
combines typeless object with typeless schema.
@alankritdabral: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏
The bounty is up for grabs! Everyone is welcome to /attempt #511
🙌
Just dove in to make sure I understand the problem--agree with @andrzejressel on those concerns. Apologies in advance that I'm very new to ZIO, so if anything below doesn't make sense, please let me know!
For the purposes of this MR, @jdegoes would it make sense to add additional typing information to MetaSchema
and change the record
method in Schema
to leverage this (i.e. to construct e.g. a CaseClass2
via deriveCaseObject
based on this typing information, rather than a GenericRecord
)?
The alternatives I thought of were just adding a new, separate TypedMetaSchema
class that carries more typing information, or the more conservative option of somehow adding warnings when trying to use toTypedValue
on typeless Schema
s.
Any of these seem like good options for me to implement?
@andrzejressel @pqcfox
I think the only way to support this would be to do so using reflection. The class name can be stored (indeed, we already have TypeId
), but then to use it when creating the record seems to require reflection.
An alternative might be to allow passing a 'registry' to toTypedValue
, something like a PartialFunction[TypeId, Constructor]
, which would avoid java reflection at the cost of introducing our own version of that.
@tusharmath I know you since moved on but did you have any ideas on how you originally wanted this issue to be solved?
Hey John!
I realized that it was not possible to do this a while back. Basically, any custom type can't be decoded this way. We shouldn't allow generating schema of any schema and instead trigger a compile-time error. For eg: Schema[Schema[List[String]]]
should be allowed but Schema[Schema[User]]
should not be possible to create ideally. That would be a reasonable developer experience to have, because currently, this introduces runtime bugs.
Makes sense to me! I can implement that--any list of allowed types I should work off when modifying Metaschema
?
(Additionally, if I wanted to address this in a PR, would I do so under this issue, and would it be in scope for the existing bounty?)
@foxmoder: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏
The bounty is up for grabs! Everyone is welcome to /attempt #511
🙌
I'm new to zio-schema and just wanted to check if the approach taken in https://github.com/zio/zio-schema/pull/725 makes sense. I naively implemented the registry approach mentioned in https://github.com/zio/zio-schema/issues/511#issuecomment-1880819029. If it does make sense, I'd be happy to gather feedback and finish it.