zio / zio-schema

Compositional, type-safe schema definitions, which enable auto-derivation of codecs and migrations.
https://zio.dev/zio-schema
Apache License 2.0
135 stars 152 forks source link

Schema deterministic hashcode for DynamicValue #655

Open carlos-verdes opened 5 months ago

carlos-verdes commented 5 months ago

I'm creating a ddd store for Entity and ValueObject.

ValueObject will use it's hashcode as key in the store (and I'll manage collisions storing a list instead of a single value object)

My current approach use something like this:

trait ValueObject[R]:

  def schema: Schema[R]

  extension (valueObject: R)
    def isSameValueObjectAs(other: R): Boolean =
      schema.diff(valueObject, other).isIdentical

    def valueHashCode: Long =
      schema.toDynamic(valueObject).hashCode

This way I can do something like store.getValueObject[T: ValueObject](valueObject: T).filter(found => found.isSameValueObjectAs(valueObject).runHead

However in my tests I observe the hashCode generated using DynamicValue changes, making my code fail.

jdegoes commented 4 weeks ago

/bounty $175

algora-pbc[bot] commented 4 weeks ago

💎 $175 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #655 with your implementation plan
  2. Submit work: Create a pull request including /claim #655 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

Thank you for contributing to zio/zio-schema!

Add a bounty • Share on socials

Attempt Started (GMT+0) Solution
🟢 @BijenderKumar1 Jun 25, 2024, 3:53:55 AM #696
BijenderKumar1 commented 1 week ago

/attempt #655 I reproduced the issue by creating a DynamicValue object and logging the hashCode on repeated runs, and it does change. Diving deep into the issue, I observed that there is no implementation for the hashCode method in the DynamicValue class, and thus the original methods from the Any class are being used, leading to inconsistency. I am adding an implementation for the hashCode in the DynamicValue class to resolve the issue. Thanks!

Algora profile Completed bounties Tech Active attempts Options
@BijenderKumar1 1 ZIO bounty
Cancel attempt
algora-pbc[bot] commented 1 week ago

💡 @BijenderKumar1 submitted a pull request that claims the bounty. You can visit your bounty board to reward.