Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed May 19, 2024
1 parent 52acb94 commit fe466d6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.io.{ByteArrayInputStream, File, InputStream}
import java.nio.ByteBuffer
import java.nio.file.{Files, Path, Paths}

import com.sksamuel.avro4s.avroutils.ByteBufferHelper
import org.apache.avro.Schema

import scala.util.Try
Expand Down Expand Up @@ -55,7 +56,8 @@ class AvroInputStreamBuilder[T: Decoder](format: AvroFormat) {
def from(file: File): AvroInputStreamBuilderWithSource[T] = from(file.toPath)
def from(in: InputStream): AvroInputStreamBuilderWithSource[T] = new AvroInputStreamBuilderWithSource(format, in)
def from(bytes: Array[Byte]): AvroInputStreamBuilderWithSource[T] = from(new ByteArrayInputStream(bytes))
def from(buffer: ByteBuffer): AvroInputStreamBuilderWithSource[T] = from(new ByteArrayInputStream(buffer.array))
def from(buffer: ByteBuffer): AvroInputStreamBuilderWithSource[T] = from(
new ByteArrayInputStream(ByteBufferHelper.asArray(buffer)))
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sksamuel.avro4s.decoders

import com.sksamuel.avro4s.avroutils.ByteBufferHelper
import com.sksamuel.avro4s.{Avro4sDecodingException, Decoder}
import org.apache.avro.Schema

Expand All @@ -18,7 +19,7 @@ trait ByteDecoders:
object ArrayByteDecoder extends Decoder[Array[Byte]] :
override def decode(schema: Schema): Any => Array[Byte] = { value =>
value match {
case buffer: ByteBuffer => buffer.array
case buffer: ByteBuffer => ByteBufferHelper.asArray(buffer)
case array: Array[Byte] => array
case fixed: org.apache.avro.generic.GenericFixed => fixed.bytes
case _ => throw new Avro4sDecodingException(s"ArrayByteDecoder cannot decode '$value'", value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.sksamuel.avro4s.encoders

import com.sksamuel.avro4s.{Avro4sConfigurationException, Avro4sEncodingException, Encoder, FieldMapper}
import org.apache.avro.Conversions.UUIDConversion
import org.apache.avro.{Conversions, LogicalTypes, Schema}
import com.sksamuel.avro4s.avroutils.ByteBufferHelper
import com.sksamuel.avro4s.{Avro4sConfigurationException, Avro4sEncodingException, Encoder}
import org.apache.avro.Schema
import org.apache.avro.generic.GenericData
import org.apache.avro.util.Utf8

Expand Down Expand Up @@ -50,3 +50,8 @@ object FixedStringEncoder extends Encoder[String] :
if (bytes.length > schema.getFixedSize)
throw new Avro4sEncodingException(s"Cannot write string with ${bytes.length} bytes to fixed type of size ${schema.getFixedSize}")
GenericData.get.createFixed(null, ByteBuffer.allocate(schema.getFixedSize).put(bytes).array, schema).asInstanceOf[GenericData.Fixed]
val bytes = string.getBytes(StandardCharsets.UTF_8)
if (bytes.length > schema.getFixedSize)
throw new Avro4sEncodingException(s"Cannot write string with ${bytes.length} bytes to fixed type of size ${schema.getFixedSize}")
GenericData.get.createFixed(null,
ByteBufferHelper.asArray(ByteBuffer.allocate(schema.getFixedSize).put(bytes)), schema).asInstanceOf[GenericData.Fixed]
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class ByteArrayDecoderTest extends AnyFunSuite with Matchers {
Decoder[VectorTest].decode(schema).apply(record).z shouldBe Vector[Byte](1, 4, 9)
}

test("decode read-only ByteBuffer to Vector[Byte]") {
val schema = AvroSchema[VectorTest]
val record = new GenericData.Record(schema)
record.put("z", ByteBuffer.wrap(Array[Byte](1, 4, 9)).asReadOnlyBuffer())
Decoder[VectorTest].decode(schema).apply(record).z shouldBe Vector[Byte](1, 4, 9)
}

test("decode Array[Byte] to List[Byte]") {
val schema = AvroSchema[ListTest]
val record = new GenericData.Record(schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ class ByteArrayEncoderTest extends AnyFunSuite with Matchers {
fixed.bytes().length shouldBe 7
}

test("encode byte buffers as FIXED (ReadOnlyBuffer)") {
val schema = SchemaBuilder.fixed("foo").size(7)
val fixed = Encoder[ByteBuffer]
.encode(schema)
.apply(ByteBuffer.wrap("hello".getBytes).asReadOnlyBuffer())
.asInstanceOf[GenericFixed]
fixed.bytes().toList shouldBe Seq(104, 101, 108, 108, 111, 0, 0)
fixed.bytes().length shouldBe 7
}

test("encode top level byte arrays") {
val encoder = Encoder[Array[Byte]]
Expand Down

0 comments on commit fe466d6

Please sign in to comment.