-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #713 from Banno/avro4s-split
feat!: split Avro4s functionality into its own project
- Loading branch information
Showing
24 changed files
with
562 additions
and
321 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2019 Jack Henry & Associates, Inc.® | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.banno.kafka.avro4s | ||
|
||
import cats.* | ||
import cats.effect.* | ||
import cats.syntax.all.* | ||
import com.banno.kafka.consumer.* | ||
import com.sksamuel.avro4s.FromRecord | ||
import org.apache.avro.generic.GenericRecord | ||
|
||
object Avro4sConsumer { | ||
def apply[F[_]: Functor, K, V]( | ||
c: ConsumerApi[F, GenericRecord, GenericRecord] | ||
)(implicit | ||
kfr: FromRecord[K], | ||
vfr: FromRecord[V], | ||
): ConsumerApi[F, K, V] = | ||
c.bimap(kfr.from, vfr.from) | ||
|
||
def resource[F[_]: Async, K: FromRecord, V: FromRecord]( | ||
configs: (String, AnyRef)* | ||
): Resource[F, ConsumerApi[F, K, V]] = | ||
ConsumerApi.Avro.Generic.resource[F](configs: _*).map(Avro4sConsumer(_)) | ||
} |
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 @@ | ||
/* | ||
* Copyright 2019 Jack Henry & Associates, Inc.® | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.banno.kafka.avro4s | ||
|
||
import cats.effect.* | ||
import com.banno.kafka.producer.* | ||
import com.sksamuel.avro4s.ToRecord | ||
import org.apache.avro.generic.GenericRecord | ||
|
||
object Avro4sProducer { | ||
def apply[F[_], K, V]( | ||
p: ProducerApi[F, GenericRecord, GenericRecord] | ||
)(implicit | ||
ktr: ToRecord[K], | ||
vtr: ToRecord[V], | ||
): ProducerApi[F, K, V] = | ||
p.contrabimap(ktr.to, vtr.to) | ||
|
||
def resource[F[_]: Async, K: ToRecord, V: ToRecord]( | ||
configs: (String, AnyRef)* | ||
): Resource[F, ProducerApi[F, K, V]] = | ||
ProducerApi.Avro.Generic.resource[F](configs: _*).map(Avro4sProducer(_)) | ||
} |
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,45 @@ | ||
/* | ||
* Copyright 2019 Jack Henry & Associates, Inc.® | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.banno.kafka | ||
package avro4s | ||
|
||
import com.sksamuel.avro4s.* | ||
import org.apache.avro.{Schema as JSchema} | ||
import org.apache.avro.generic.GenericRecord | ||
import scala.util.* | ||
|
||
object SchemaObjectAvro4sOps { | ||
private def fromGeneric[A]( | ||
gr: GenericRecord | ||
)(implicit FR: FromRecord[A]): Try[A] = | ||
Try(FR.from(gr)) | ||
|
||
private def toGeneric[A]( | ||
x: A | ||
)(implicit TR: ToRecord[A]): GenericRecord = | ||
TR.to(x) | ||
|
||
private def schema[A](implicit SF: SchemaFor[A]): JSchema = | ||
SF.schema(DefaultFieldMapper) | ||
|
||
def apply[A: FromRecord: ToRecord: SchemaFor]: Schema[A] = | ||
Schema( | ||
schema, | ||
fromGeneric(_), | ||
toGeneric(_), | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2019 Jack Henry & Associates, Inc.® | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.banno.kafka | ||
package avro4s | ||
|
||
import com.sksamuel.avro4s.* | ||
|
||
object TopicObjectAvro4sOps { | ||
def apply[ | ||
K: FromRecord: ToRecord: SchemaFor, | ||
V: FromRecord: ToRecord: SchemaFor, | ||
]( | ||
topic: String, | ||
topicPurpose: TopicPurpose, | ||
): Topic[K, V] = | ||
Topic( | ||
topic, | ||
topicPurpose, | ||
Schema.avro4s[K], | ||
Schema.avro4s[V], | ||
) | ||
|
||
def builder[ | ||
K: FromRecord: ToRecord: SchemaFor, | ||
V: FromRecord: ToRecord: SchemaFor, | ||
]( | ||
topic: String, | ||
topicPurpose: TopicPurpose, | ||
): Topic.Builder[K, V] = | ||
Topic.builder( | ||
topic, | ||
topicPurpose, | ||
Schema.avro4s[K], | ||
Schema.avro4s[V], | ||
) | ||
} |
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,83 @@ | ||
/* | ||
* Copyright 2019 Jack Henry & Associates, Inc.® | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.banno.kafka | ||
|
||
import com.banno.kafka.producer.* | ||
import com.banno.kafka.schemaregistry.* | ||
import com.sksamuel.avro4s.* | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.kafka.clients.consumer.ConsumerRecord | ||
import org.apache.kafka.clients.producer.ProducerRecord | ||
|
||
package object avro4s { | ||
implicit final class SchemaRegistryAvro4sOpsOps[F[_]]( | ||
private val r: SchemaRegistryApi[F] | ||
) extends AnyVal { | ||
def avro4s: SchemaRegistryAvro4sOps[F] = | ||
new SchemaRegistryAvro4sOps(r) | ||
} | ||
|
||
implicit final class SchemaRegistryObjectAvro4sOpsOps( | ||
private val x: SchemaRegistryApi.type | ||
) extends AnyVal { | ||
def avro4s = SchemaRegistryApiObjectAvro4sOps | ||
} | ||
|
||
implicit final class TopicObjectAvro4sOpsOps( | ||
private val x: Topic.type | ||
) extends AnyVal { | ||
def avro4s = TopicObjectAvro4sOps | ||
} | ||
|
||
implicit final class SchemaObjectAvro4sOpsOps( | ||
private val x: Schema.type | ||
) extends AnyVal { | ||
def avro4s = SchemaObjectAvro4sOps | ||
} | ||
|
||
implicit final class GenericProducerAvro4sOps[F[_]]( | ||
private val producer: ProducerApi[F, GenericRecord, GenericRecord] | ||
) extends AnyVal { | ||
def toAvro4s[K: ToRecord, V: ToRecord]: ProducerApi[F, K, V] = | ||
Avro4sProducer[F, K, V](producer) | ||
} | ||
|
||
implicit final class GenericConsumerRecordAvro4sOps( | ||
private val cr: ConsumerRecord[GenericRecord, GenericRecord] | ||
) extends AnyVal { | ||
def maybeKeyAs[K](implicit kfr: FromRecord[K]): Option[K] = | ||
cr.maybeKey.map(kfr.from) | ||
def maybeValueAs[V](implicit vfr: FromRecord[V]): Option[V] = | ||
cr.maybeValue.map(vfr.from) | ||
|
||
// note that these will probably throw NPE if key/value is null | ||
def keyAs[K](implicit kfr: FromRecord[K]): K = kfr.from(cr.key) | ||
def valueAs[V](implicit vfr: FromRecord[V]): V = vfr.from(cr.value) | ||
} | ||
|
||
implicit final class ProducerRecordAvro4sOps[K, V]( | ||
private val pr: ProducerRecord[K, V] | ||
) extends AnyVal { | ||
|
||
/** This only works when both key and value are non-null. */ | ||
def toGenericRecord(implicit | ||
ktr: ToRecord[K], | ||
vtr: ToRecord[V], | ||
): ProducerRecord[GenericRecord, GenericRecord] = | ||
pr.bimap(ktr.to, vtr.to) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.