Skip to content

Commit

Permalink
[shared] Add scala 2.12 CanBuildFrom for Set (#942)
Browse files Browse the repository at this point in the history
* [shared] Add scala 2.12 CanBuildFrom for Set

Set could be use as collection in scala 2.13 but not in 2.12 due
to a missing serializable can build from factory.

* FIx protobuf tests

* FIx custom equal/hashcode on test Collections
  • Loading branch information
RustedBones authored Mar 27, 2024
1 parent ee3371c commit dbbb012
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions protobuf/src/test/protobuf/Proto2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message CollectionP2 {
repeated int32 a = 1;
repeated int32 l = 2;
repeated int32 v = 3;
repeated int32 s = 4;
}

message MoreCollectionP2 {
Expand Down
1 change: 1 addition & 0 deletions protobuf/src/test/protobuf/Proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message CollectionP3 {
repeated int32 a = 1;
repeated int32 l = 2;
repeated int32 v = 3;
repeated int32 s = 4;
}

message MoreCollectionP3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ trait SerializableCanBuildFromLowPrio3 extends SerializableCanBuildFromLowPrio4
SerializableCanBuildFrom(Stream.newBuilder[A])
}

trait SerializableCanBuildFromLowPrio4 {
trait SerializableCanBuildFromLowPrio4 extends SerializableCanBuildFromLowPrio5 {
implicit def lazyListCBF[A]: SerializableCanBuildFrom[Any, A, LazyList[A]] =
SerializableCanBuildFrom(LazyList.newBuilder[A])
}

trait SerializableCanBuildFromLowPrio5 {
implicit def setCBF[A]: SerializableCanBuildFrom[Any, A, Set[A]] =
SerializableCanBuildFrom(Set.newBuilder[A])
}
6 changes: 4 additions & 2 deletions test/src/test/scala/magnolify/test/Simple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,23 @@ object Simple {
o: Option[Required],
l: List[Required]
)
case class Collections(a: Array[Int], l: List[Int], v: Vector[Int]) {
case class Collections(a: Array[Int], l: List[Int], v: Vector[Int], s: Set[Int]) {

override def hashCode(): Int = {
var hash = 1
hash = 31 * hash + util.Arrays.hashCode(a)
hash = 31 * hash + Objects.hashCode(l)
hash = 31 * hash + Objects.hashCode(v)
hash = 31 * hash + Objects.hashCode(s)
hash
}

override def equals(obj: Any): Boolean = obj match {
case that: Collections =>
Objects.deepEquals(this.a, that.a) &&
Objects.equals(this.l, that.l) &&
Objects.equals(this.v, that.v)
Objects.equals(this.v, that.v) &&
Objects.equals(this.s, that.s)
case _ => false
}
}
Expand Down

0 comments on commit dbbb012

Please sign in to comment.