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

Schema deterministic hashcode for DynamicValue

Open carlos-verdes opened this issue 1 year ago • 5 comments

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.

carlos-verdes avatar Jan 30 '24 18:01 carlos-verdes

/bounty $175

jdegoes avatar Jun 08 '24 02:06 jdegoes

💎 $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 bountyShare on socials

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

algora-pbc[bot] avatar Jun 08 '24 02:06 algora-pbc[bot]

/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

BijenderKumar1 avatar Jun 25 '24 03:06 BijenderKumar1

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

algora-pbc[bot] avatar Jun 25 '24 06:06 algora-pbc[bot]

@jdegoes Just a gentle reminder to please review my pull request. Thanks!

BijenderKumar1 avatar Aug 31 '24 09:08 BijenderKumar1