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 scala enumeration implicit coder #5213

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ package com.spotify.scio.coders.instances
import com.spotify.scio.coders.{Coder, CoderDerivation, CoderGrammar}
import org.apache.beam.sdk.coders.Coder.NonDeterministicException
import org.apache.beam.sdk.coders.{Coder => BCoder, IterableCoder => BIterableCoder, _}
import org.apache.beam.sdk.util.{BufferedElementCountingOutputStream, CoderUtils, VarInt}
import org.apache.beam.sdk.util.common.ElementByteSizeObserver
import org.apache.beam.sdk.util.{BufferedElementCountingOutputStream, CoderUtils, VarInt}

import java.io.{InputStream, OutputStream}
import java.lang.{Iterable => JIterable}
import java.util.{Collections, List => JList}
import scala.collection.compat._
import scala.collection.{mutable => m, AbstractIterable, BitSet, SortedSet}
import scala.jdk.CollectionConverters._
import scala.reflect.ClassTag
import scala.reflect.{classTag, ClassTag}
import scala.util.{Failure, Success, Try}

private[coders] object UnitCoder extends AtomicCoder[Unit] {
Expand Down Expand Up @@ -452,6 +452,14 @@ trait ScalaCoders extends CoderGrammar with CoderDerivation {
implicit lazy val bigDecimalCoder: Coder[BigDecimal] =
xmap(beam(BigDecimalCoder.of()))(BigDecimal.apply, _.bigDecimal)
implicit lazy val bitSetCoder: Coder[BitSet] = beam(new BitSetCoder)

implicit def enumerationCoder[E <: Enumeration: ClassTag]: Coder[E#Value] = {
import scala.reflect.runtime.{currentMirror => m}
val sym = m.moduleSymbol(classTag[E].runtimeClass)
val e = m.reflectModule(sym).instance.asInstanceOf[E]
xmap(Coder[Int])(e.apply, _.id)
}

implicit def throwableCoder[T <: Throwable: ClassTag]: Coder[T] = kryo[T]
implicit def seqCoder[T: Coder]: Coder[Seq[T]] =
transform(Coder[T])(bc => beam(new SeqCoder[T](bc)))
Expand Down
12 changes: 12 additions & 0 deletions scio-test/src/test/scala/com/spotify/scio/coders/CoderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ final class CoderTest extends AnyFlatSpec with Matchers {
beFullyCompliantNotConsistentWithEquals()
}

it should "support Scala enumerations" in {
ScalaColor.Red coderShould roundtrip() and
beOfType[Transform[_, _]] and
materializeToTransformOf[beam.VarIntCoder] and
beFullyCompliant()
}

it should "support Scala option" in {
Option(1) coderShould roundtrip() and
beOfType[Disjunction[_, _]] and
Expand Down Expand Up @@ -852,6 +859,11 @@ final class CoderTest extends AnyFlatSpec with Matchers {
}
}

// enumeration
object ScalaColor extends Enumeration {
val Red, Green, Blue = Value
}

// record
final case class UserId(bytes: Seq[Byte])
final case class User(id: UserId, username: String, email: String)
Expand Down
Loading