zio-schema icon indicating copy to clipboard operation
zio-schema copied to clipboard

Unable to convert to TypedValue

Open tusharmath opened this issue 2 years ago • 15 comments

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

tusharmath avatar Feb 10 '23 06:02 tusharmath

/bounty $200

jdegoes avatar Apr 25 '23 08:04 jdegoes

💎 $200 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #511 with your implementation plan
  2. Submit work: Create a pull request including /claim #511 in the PR body to claim the bounty
  3. 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 bountyShare 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

algora-pbc[bot] avatar Apr 25 '23 08:04 algora-pbc[bot]

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.

andrzejressel avatar Apr 28 '23 18:04 andrzejressel

/attempt #511

Options

alankritdabral avatar Nov 12 '23 12:11 alankritdabral

@alankritdabral: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

algora-pbc[bot] avatar Nov 19 '23 12:11 algora-pbc[bot]

The bounty is up for grabs! Everyone is welcome to /attempt #511 🙌

algora-pbc[bot] avatar Nov 26 '23 12:11 algora-pbc[bot]

Going to go ahead and toss my hat in the ring! /attempt #511

Options

pqcfox avatar Jan 07 '24 22:01 pqcfox

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

Any of these seem like good options for me to implement?

pqcfox avatar Jan 08 '24 00:01 pqcfox

@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?

jdegoes avatar Jan 08 '24 11:01 jdegoes

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.

tusharmath avatar Jan 08 '24 15:01 tusharmath

Makes sense to me! I can implement that--any list of allowed types I should work off when modifying Metaschema?

pqcfox avatar Jan 08 '24 19:01 pqcfox

(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?)

pqcfox avatar Jan 14 '24 08:01 pqcfox

@foxmoder: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

algora-pbc[bot] avatar Jan 14 '24 22:01 algora-pbc[bot]

The bounty is up for grabs! Everyone is welcome to /attempt #511 🙌

algora-pbc[bot] avatar Jan 21 '24 22:01 algora-pbc[bot]

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.

stanislav-chetvertkov avatar Aug 10 '24 05:08 stanislav-chetvertkov