Skip to content

Commit

Permalink
Adds the from value and to value avro decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
fedefernandez committed May 23, 2018
1 parent 20793b3 commit 50ae2bf
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions modules/internal/src/main/scala/encoders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.google.protobuf.{CodedInputStream, CodedOutputStream}
import freestyle.rpc.protocol._
import freestyle.rpc.internal.util.{BigDecimalUtil, JavaTimeUtil}
import io.grpc.MethodDescriptor.Marshaller
import org.apache.avro.{Schema, SchemaBuilder}
import org.apache.avro.Schema

object encoders {

Expand Down Expand Up @@ -57,7 +57,6 @@ object encoders {
implicit object LocalDateTimeWriter extends PBWriter[LocalDateTime] {
override def writeTo(index: Int, value: LocalDateTime, out: CodedOutputStream): Unit =
out.writeByteArray(index, JavaTimeUtil.localDateTimeToByteArray(value))

}

implicit object LocalDateTimeReader extends PBReader[LocalDateTime] {
Expand Down Expand Up @@ -85,9 +84,8 @@ object encoders {
override def stream(value: Empty.type) = new ByteArrayInputStream(Array.empty)
}

implicit object bigDecimalSchemaFor extends SchemaFor[BigDecimal] {
private val schema: Schema = SchemaBuilder.builder().bytesType()
def apply(): Schema = schema
implicit object bigDecimalToSchema extends ToSchema[BigDecimal] {
override val schema: Schema = Schema.create(Schema.Type.BYTES)
}

implicit object bigDecimalFromValue extends FromValue[BigDecimal] {
Expand All @@ -100,6 +98,34 @@ object encoders {
ByteBuffer.wrap(BigDecimalUtil.bigDecimalToByte(value))
}

implicit object localDateToSchema extends ToSchema[LocalDate] {
override val schema: Schema = Schema.create(Schema.Type.INT)
}

implicit object localDateFromValue extends FromValue[LocalDate] {
def apply(value: Any, field: Schema.Field): LocalDate =
JavaTimeUtil.intToLocalDate(value.asInstanceOf[Int])
}

implicit object localDateToValue extends ToValue[LocalDate] {
override def apply(value: LocalDate): Int =
JavaTimeUtil.localDateToInt(value)
}

implicit object localDateTimeToSchema extends ToSchema[LocalDateTime] {
override val schema: Schema = Schema.create(Schema.Type.LONG)
}

implicit object localDateTimeFromValue extends FromValue[LocalDateTime] {
def apply(value: Any, field: Schema.Field): LocalDateTime =
JavaTimeUtil.longToLocalDateTime(value.asInstanceOf[Long])
}

implicit object localDateTimeToValue extends ToValue[LocalDateTime] {
override def apply(value: LocalDateTime): Long =
JavaTimeUtil.localDateTimeToLong(value)
}

implicit val bigDecimalMarshaller: Marshaller[BigDecimal] = new Marshaller[BigDecimal] {
override def stream(value: BigDecimal): InputStream =
new ByteArrayInputStream(BigDecimalUtil.bigDecimalToByte(value))
Expand Down

0 comments on commit 50ae2bf

Please sign in to comment.