Skip to content

Commit

Permalink
Add implementation of hashCode method for Dynamic Value along with th…
Browse files Browse the repository at this point in the history
…e corresponding test
  • Loading branch information
BijenderKumar1 committed Jun 25, 2024
1 parent 3feff56 commit fd95baa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ object DynamicValueSpec extends ZIOSpecDefault {
assertTrue(json2 == Right(json))
}
} @@ TestAspect.size(250) @@ TestAspect.ignore
),
suite("hashCode consistency")(
test("hashCode does not change across runs") {
val primitive1 = DynamicValue.Primitive(123, StandardType.IntType)
val hash1 = primitive1.hashCode()
val hash2 = primitive1.hashCode()
assert(hash1)(equalTo(hash2))
}
)
)

Expand Down
16 changes: 16 additions & 0 deletions zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ sealed trait DynamicValue {
case _ =>
Left(DecodeError.CastError(self, schema))
}
override def hashCode(): Int = this match {
case DynamicValue.Primitive(value, standardType) => 31 * value.hashCode() + standardType.hashCode()
case DynamicValue.Record(id, values) => 31 * id.hashCode() + values.hashCode()
case DynamicValue.Enumeration(id, value) => 31 * id.hashCode() + value.hashCode()
case DynamicValue.Sequence(values) => values.hashCode()
case DynamicValue.Dictionary(entries) => entries.hashCode()
case DynamicValue.SetValue(values) => values.hashCode()
case DynamicValue.SomeValue(value) => value.hashCode()
case DynamicValue.NoneValue => 0
case DynamicValue.Tuple(left, right) => 31 * left.hashCode() + right.hashCode()
case DynamicValue.LeftValue(value) => value.hashCode()
case DynamicValue.RightValue(value) => value.hashCode()
case DynamicValue.BothValue(left, right) => 31 * left.hashCode() + right.hashCode()
case DynamicValue.DynamicAst(ast) => ast.hashCode()
case DynamicValue.Error(message) => message.hashCode()
}

}

Expand Down

0 comments on commit fd95baa

Please sign in to comment.