-
Notifications
You must be signed in to change notification settings - Fork 38
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
added new optional param classInstance
to Serializer
and Deserializer
helper classes
#5
Conversation
…lizer` helper classes. These are defaulted to perform the normal behaviour: reflective construction of the specified classes. The param can then be set so that one can specify (provide) logic that defines how an instance of the class should be constructed. The primary use case for this was special serializers that take constructor parameters.
As an example of the usecase using Confluents Avro deserializer: import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient
import io.confluent.kafka.serializers.KafkaAvroDeserializer
val schemaRegistryClient = new CachedSchemaRegistryClient("http://localhost:8081", 42)
implicit val avroDeserializer =
Deserializer[AnyRef](
"io.confluent.kafka.serializers.KafkaAvroDeserializer",
classOf[KafkaAvroDeserializer],
(d) => new KafkaAvroDeserializer(schemaRegistryClient)
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I just have this little obsession against verbose naming and if you could change ClassInstanceProvider
to Constructor
, that would be great.
Thanks for the PR.
@@ -27,14 +27,18 @@ import language.existentials | |||
*/ | |||
final case class Serializer[A]( | |||
className: String, | |||
classType: Class[_ <: KafkaSerializer[A]]) { | |||
classType: Class[_ <: KafkaSerializer[A]], | |||
classInstance: Serializer.ClassInstanceProvider[A] = (s: Serializer[A]) => s.classType.newInstance()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default parameter looks ugly (personal pet peeve). I'm fairly certain that Scala accepts this:
classInstance: Serializer.ClassInstanceProvider[A] = _.classType.newInstance()
While we are at it classInstance: Serializer.ClassInstanceProvider
is a little redundant and verbose. So lets simplify to:
constructor: Serializer.Constructor[A] = _.classType.newInstance()
// ...
object Serializer {
type Constructor[A] = (Serializer[A]) => KafkaSerializer[A]
// ...
Naming, right? :-) I don't care that much, so it's up to you.
@kdrakon can I bother you with also adding a comment for that new type alias and for the params of the |
*/ | ||
final case class Deserializer[A]( | ||
className: String, | ||
classType: Class[_ <: KafkaDeserializer[A]]) { | ||
classType: Class[_ <: KafkaDeserializer[A]], | ||
constructor: Deserializer.Constructor[A] = (d: Deserializer[A]) => d.classType.newInstance()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexandru unfortunately, I couldn't simplify this to _.classType.newInstance()
. There is a
missing parameter type for expanded function
warning, but because -Xfatal-warnings
is enabled, this makes it impossible to compile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than that, the rest of the suggestions have been pushed to the PR.
Thanks @kdrakon, I'm merging it, I hope you tested it :-) |
Ran the tests locally a few times, but tested it more with my localised project 😅 |
These are defaulted to perform the normal behaviour: reflective construction of the specified classes. The param can then be set so that one can specify (provide) logic that defines how an instance of the class should be constructed. The primary use case for this was special serializers that take constructor parameters.
Addresses Issue #4