-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable deserializer and fix build (#109)
* Add MyConfigurableDeserializer example * Update tpolecat and add warn exclusion for scalaPB * Update README and MyConfigurableDeserializer
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.4.2") | ||
addSbtPlugin("org.typelevel" % "sbt-tpolecat" % "0.5.0") | ||
|
||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1") |
37 changes: 37 additions & 0 deletions
37
src/main/scala/io/example/conduktor/custom/deserializers/MyConfigurableDeserializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.example.conduktor.custom.deserializers | ||
|
||
import org.apache.kafka.common.serialization.Deserializer | ||
|
||
import java.util | ||
import scala.jdk.CollectionConverters.MapHasAsScala | ||
|
||
case object MyConfigurableDeserializerException | ||
extends RuntimeException( | ||
"ConfigurableDeserializer fail when its `::configure` method is called without `output` property" | ||
) | ||
|
||
sealed trait Output | ||
final case class Constant(value: String) extends Output | ||
final case class Config(config: util.Map[String, _]) extends Output | ||
final case object Passthrough extends Output | ||
final case object Unconfigured extends Output | ||
|
||
final class MyConfigurableDeserializer extends Deserializer[Any] { | ||
|
||
var output: Output = Unconfigured | ||
|
||
override def deserialize(topic: String, data: Array[Byte]): Any = output match { | ||
case Constant(value) => value | ||
case Config(config) => config | ||
case Passthrough => data | ||
case Unconfigured => throw MyConfigurableDeserializerException | ||
} | ||
|
||
override def configure(configs: util.Map[String, _], isKey: Boolean): Unit = | ||
configs.asScala.get("output").map(_.asInstanceOf[String]) match { | ||
case Some("config") => output = Config(configs) | ||
case Some("passthrough") => output = Passthrough | ||
case Some(value) => output = Constant(value) | ||
case None => throw MyConfigurableDeserializerException | ||
} | ||
} |