-
Notifications
You must be signed in to change notification settings - Fork 97
/
Serializer.scala
266 lines (226 loc) · 9.35 KB
/
Serializer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2018-2023 OVO Energy Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
package fs2.kafka
import cats.Contravariant
import cats.effect.{Resource, Sync}
import cats.syntax.all._
import java.nio.charset.{Charset, StandardCharsets}
import java.util.UUID
sealed abstract class GenericSerializer[-T <: KeyOrValue, F[_], A] {
/**
* Attempts to serialize the specified value of type `A` into
* bytes. The Kafka topic name, to which the serialized bytes
* are going to be sent, and record headers are available.
*/
def serialize(topic: String, headers: Headers, a: A): F[Array[Byte]]
/**
* Creates a new [[Serializer]] which applies the specified
* function `f` on a value of type `B`, and then serializes
* the result with this [[Serializer]].
*/
def contramap[B](f: B => A): GenericSerializer[T, F, B]
/**
* Creates a new [[Serializer]] which applies the specified
* function `f` on the output bytes of this [[Serializer]].
*/
def mapBytes(f: Array[Byte] => Array[Byte]): GenericSerializer[T, F, A]
/**
* Creates a new [[Serializer]] which serializes `Some` values
* using this [[Serializer]], and serializes `None` as `null`.
*/
def option: GenericSerializer[T, F, Option[A]]
/**
* Creates a new [[Serializer]] which suspends serialization,
* capturing any impure behaviours of this [[Serializer]].
*/
def suspend: Serializer[F, A]
}
/**
* Functional composable Kafka key- and record serializer with
* support for effect types.
*/
object GenericSerializer {
def apply[F[_], A](implicit serializer: Serializer[F, A]): Serializer[F, A] = serializer
/** Alias for [[Serializer#identity]]. */
def apply[F[_]](implicit F: Sync[F]): Serializer[F, Array[Byte]] = identity
/**
* Creates a new [[Serializer]] which serializes
* all values of type `A` as `null`.
*/
def asNull[F[_], A](implicit F: Sync[F]): Serializer[F, A] =
Serializer.const(null)
/**
* Creates a new [[Serializer]] which serializes
* all values of type `A` to the specified `bytes`.
*/
def const[F[_], A](bytes: Array[Byte])(implicit F: Sync[F]): Serializer[F, A] =
Serializer.lift(_ => F.pure(bytes))
/**
* Creates a new [[Serializer]] which delegates serialization
* to the specified Kafka `Serializer`. Note the `close` and
* `configure` functions won't be called for the delegate.<br>
* <br>
* It is assumed the delegate `serialize` function is pure.
* If it's not pure, then use `suspend` after `delegate`,
* so the impure behaviours can be captured properly.
*/
def delegate[F[_], A](serializer: KafkaSerializer[A])(
implicit F: Sync[F]
): Serializer[F, A] =
Serializer.instance[F, A] { (topic, headers, a) =>
F.pure(serializer.serialize(topic, headers.asJava, a))
}
/**
* Creates a new [[Serializer]] which always fails
* serialization with the specified exception `e`.
*/
def fail[F[_], A](e: Throwable)(implicit F: Sync[F]): Serializer[F, A] =
Serializer.lift(_ => F.raiseError(e))
/**
* Creates a new [[Serializer]] which always fails
* serialization with a [[SerializationException]]
* using the specified message.
*/
def failWith[F[_], A](message: String)(implicit F: Sync[F]): Serializer[F, A] =
Serializer.fail(SerializationException(message))
/**
* Creates a new [[Serializer]] which serializes all
* values of type `A` as the empty `Array[Byte]`.
*/
def empty[F[_], A](implicit F: Sync[F]): Serializer[F, A] =
Serializer.const(Array.emptyByteArray)
/**
* Creates a new [[Serializer]] which can use different
* [[Serializer]]s depending on the record headers.
*/
def headers[F[_], A](f: Headers => Serializer[F, A])(implicit F: Sync[F]): Serializer[F, A] =
Serializer.instance { (topic, headers, a) =>
f(headers).serialize(topic, headers, a)
}
/**
* Creates a new [[Serializer]] from the specified function.
* Use [[lift]] instead if the serializer doesn't need
* access to the Kafka topic name or record headers.
*/
def instance[F[_], A](
f: (String, Headers, A) => F[Array[Byte]]
)(implicit F: Sync[F]): Serializer[F, A] =
new Serializer[F, A] {
override def serialize(topic: String, headers: Headers, a: A): F[Array[Byte]] =
f(topic, headers, a)
override def contramap[B](f: B => A): Serializer[F, B] =
Serializer.instance { (topic, headers, b) =>
serialize(topic, headers, f(b))
}
override def mapBytes(f: Array[Byte] => Array[Byte]): Serializer[F, A] =
Serializer.instance { (topic, headers, a) =>
serialize(topic, headers, a).map(f)
}
override def option: Serializer[F, Option[A]] =
Serializer.instance {
case (topic, headers, Some(a)) => serialize(topic, headers, a)
case (_, _, None) => F.pure(null)
}
override def suspend: Serializer[F, A] =
Serializer.instance { (topic, headers, a) =>
F.defer(serialize(topic, headers, a))
}
override def toString: String =
"Serializer$" + System.identityHashCode(this)
}
/**
* Creates a new [[Serializer]] from the specified function,
* ignoring to which Kafka topic the bytes are going to be
* sent and any record headers. Use [[instance]] instead
* if the serializer needs access to the Kafka topic
* name or the record headers.
*/
def lift[F[_], A](f: A => F[Array[Byte]])(implicit F: Sync[F]): Serializer[F, A] =
Serializer.instance((_, _, a) => f(a))
private[this] def unexpectedTopic[F[_], A](implicit F: Sync[F]): String => Serializer[F, A] =
topic => Serializer.fail(UnexpectedTopicException(topic))
/**
* Creates a new [[Serializer]] which can use different
* [[Serializer]]s depending on the Kafka topic name to
* which the bytes are going to be sent.
*/
def topic[T <: KeyOrValue, F[_], A](
f: PartialFunction[String, GenericSerializer[T, F, A]]
)(implicit F: Sync[F]): GenericSerializer[T, F, A] =
Serializer.instance[F, A] { (topic, headers, a) =>
f.applyOrElse(topic, unexpectedTopic)
.serialize(topic, headers, a)
}
/**
* Creates a new [[Serializer]] which serializes `String`
* values using the specified `Charset`. Note that the
* default `String` serializer uses `UTF-8`.
*/
def string[F[_]](charset: Charset)(implicit F: Sync[F]): Serializer[F, String] =
Serializer.lift(s => F.pure(s.getBytes(charset)))
/**
* Creates a new [[Serializer]] which serializes `UUID` values
* as `String`s with the specified `Charset`. Note that the
* default `UUID` serializer uses `UTF-8.`
*/
def uuid[F[_]](charset: Charset)(implicit F: Sync[F]): Serializer[F, UUID] =
Serializer.string[F](charset).contramap(_.toString)
/**
* The identity [[Serializer]], which does not perform any kind
* of serialization, simply using the input bytes as the output.
*/
implicit def identity[F[_]](implicit F: Sync[F]): Serializer[F, Array[Byte]] =
Serializer.lift(bytes => F.pure(bytes))
/**
* The option [[Serializer]] serializes `None` as `null`, and
* serializes `Some` values using the serializer for type `A`.
*/
implicit def option[T <: KeyOrValue, F[_], A](
implicit serializer: GenericSerializer[T, F, A]
): GenericSerializer[T, F, Option[A]] =
serializer.option
implicit def contravariant[T <: KeyOrValue, F[_]]: Contravariant[GenericSerializer[T, F, *]] =
new Contravariant[GenericSerializer[T, F, *]] {
override def contramap[A, B](
serializer: GenericSerializer[T, F, A]
)(f: B => A): GenericSerializer[T, F, B] =
serializer.contramap(f)
}
implicit def double[F[_]](implicit F: Sync[F]): Serializer[F, Double] =
Serializer.delegate {
(new org.apache.kafka.common.serialization.DoubleSerializer)
.asInstanceOf[org.apache.kafka.common.serialization.Serializer[Double]]
}
implicit def float[F[_]](implicit F: Sync[F]): Serializer[F, Float] =
Serializer.delegate {
(new org.apache.kafka.common.serialization.FloatSerializer)
.asInstanceOf[org.apache.kafka.common.serialization.Serializer[Float]]
}
implicit def int[F[_]](implicit F: Sync[F]): Serializer[F, Int] =
Serializer.delegate {
(new org.apache.kafka.common.serialization.IntegerSerializer)
.asInstanceOf[org.apache.kafka.common.serialization.Serializer[Int]]
}
implicit def long[F[_]](implicit F: Sync[F]): Serializer[F, Long] =
Serializer.delegate {
(new org.apache.kafka.common.serialization.LongSerializer)
.asInstanceOf[org.apache.kafka.common.serialization.Serializer[Long]]
}
implicit def short[F[_]](implicit F: Sync[F]): Serializer[F, Short] =
Serializer.delegate {
(new org.apache.kafka.common.serialization.ShortSerializer)
.asInstanceOf[org.apache.kafka.common.serialization.Serializer[Short]]
}
implicit def string[F[_]](implicit F: Sync[F]): Serializer[F, String] =
Serializer.string(StandardCharsets.UTF_8)
implicit def unit[F[_]](implicit F: Sync[F]): Serializer[F, Unit] =
Serializer.const(null)
implicit def uuid[F[_]](implicit F: Sync[F]): Serializer[F, UUID] =
Serializer.string[F].contramap(_.toString)
implicit def resource[T <: KeyOrValue, F[_], A](
implicit ser: GenericSerializer[T, F, A]
): Resource[F, GenericSerializer[T, F, A]] = Resource.pure(ser)
}