Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of hashCode method for Dynamic Value along with the corresponding test #696

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ object DynamicValueGen {
}
}

// Method to generate arbitrary DynamicValue
def anyDynamicValue: Gen[Sized, DynamicValue] =
Gen.oneOf(
anyPrimitiveDynamicValue(StandardType.StringType),
anyPrimitiveDynamicValue(StandardType.IntType),
anyPrimitiveDynamicValue(StandardType.BoolType),
anyPrimitiveDynamicValue(StandardType.DoubleType),
anyPrimitiveDynamicValue(StandardType.UUIDType),
anyPrimitiveDynamicValue(StandardType.LocalDateType),
anyPrimitiveDynamicValue(StandardType.LocalDateTimeType),
anyPrimitiveDynamicValue(StandardType.LocalTimeType),
anyPrimitiveDynamicValue(StandardType.InstantType),
anyPrimitiveDynamicValue(StandardType.DurationType),
anyPrimitiveDynamicValue(StandardType.ZoneIdType),
anyPrimitiveDynamicValue(StandardType.ZoneOffsetType),
anyPrimitiveDynamicValue(StandardType.ZonedDateTimeType),
anyPrimitiveDynamicValue(StandardType.YearType),
anyPrimitiveDynamicValue(StandardType.YearMonthType),
anyPrimitiveDynamicValue(StandardType.MonthType),
anyPrimitiveDynamicValue(StandardType.MonthDayType),
anyPrimitiveDynamicValue(StandardType.DayOfWeekType),
anyPrimitiveDynamicValue(StandardType.BigDecimalType),
anyPrimitiveDynamicValue(StandardType.BigIntegerType)
)

//scalafmt: { maxColumn = 400 }
def anyDynamicValueOfSchema[A](schema: Schema[A]): Gen[Sized, DynamicValue] =
schema match {
Expand Down
17 changes: 16 additions & 1 deletion tests/shared/src/test/scala/zio/schema/DynamicValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ object DynamicValueSpec extends ZIOSpecDefault {
assertTrue(json2 == Right(json))
}
} @@ TestAspect.size(250) @@ TestAspect.ignore
),
suite("hashCode and equality consistency")(
test("hashCode does not change") {
check(DynamicValueGen.anyDynamicValue) { dynamicValue =>
val hash1 = dynamicValue.hashCode()
val hash2 = dynamicValue.hashCode()
assert(hash1)(equalTo(hash2))
}
},
test("equivalent DynamicValues have same hashCode and equality") {
check(DynamicValueGen.anyDynamicValue) { dynamicValue =>
val dynamicValueCopy = dynamicValue
assert(dynamicValue.hashCode())(equalTo(dynamicValueCopy.hashCode())) &&
assert(dynamicValue)(equalTo(dynamicValueCopy))
}
}
)
)

Expand All @@ -114,5 +130,4 @@ object DynamicValueSpec extends ZIOSpecDefault {
check(gen) { a =>
assert(schema.fromDynamic(schema.toDynamic(a)))(isRight(equalTo(a)))
}

}
53 changes: 53 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,59 @@ 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()
case DynamicValue.Singleton(instance) => instance.hashCode()
}
override def equals(obj: Any): Boolean = obj match {
case that: DynamicValue if this eq that => true
case that: DynamicValue =>
(this, that) match {
case (DynamicValue.Primitive(value1, standardType1), DynamicValue.Primitive(value2, standardType2)) =>
value1 == value2 && standardType1 == standardType2
case (DynamicValue.Record(id1, values1), DynamicValue.Record(id2, values2)) =>
id1 == id2 && values1 == values2
case (DynamicValue.Enumeration(id1, value1), DynamicValue.Enumeration(id2, value2)) =>
id1 == id2 && value1 == value2
case (DynamicValue.Sequence(values1), DynamicValue.Sequence(values2)) =>
values1 == values2
case (DynamicValue.Dictionary(entries1), DynamicValue.Dictionary(entries2)) =>
entries1 == entries2
case (DynamicValue.SetValue(values1), DynamicValue.SetValue(values2)) =>
values1 == values2
case (DynamicValue.SomeValue(value1), DynamicValue.SomeValue(value2)) =>
value1 == value2
case (DynamicValue.Tuple(left1, right1), DynamicValue.Tuple(left2, right2)) =>
left1 == left2 && right1 == right2
case (DynamicValue.LeftValue(value1), DynamicValue.LeftValue(value2)) =>
value1 == value2
case (DynamicValue.RightValue(value1), DynamicValue.RightValue(value2)) =>
value1 == value2
case (DynamicValue.BothValue(left1, right1), DynamicValue.BothValue(left2, right2)) =>
left1 == left2 && right1 == right2
case (DynamicValue.DynamicAst(ast1), DynamicValue.DynamicAst(ast2)) =>
ast1 == ast2
case (DynamicValue.Error(message1), DynamicValue.Error(message2)) =>
message1 == message2
case (DynamicValue.Singleton(instance1), DynamicValue.Singleton(instance2)) =>
instance1 == instance2
case _ => false
}
case _ => false
}

}

Expand Down
Loading