julianpeeters / avrohugger

Generate Scala case class definitions from Avro schemas
Apache License 2.0
201 stars 120 forks source link

Logical types support for `timestamp_ms` and `date` using `java.time.*` #87

Closed raulraja closed 6 years ago

raulraja commented 6 years ago

Support for timestamp_ms and date avro types bases on java.time.LocalDateTime and java.time.LocalDate

@julianpeeters Let me know if this goes in the right direction and I would add the sbt-avrohugger tests for serialization once we are happy with the approach.

something like this idl (avpr and avs also supported)

@namespace("example.idl")

protocol LogicalIDL {

  record LogicalIdl {
    decimal(9, 2) dec = 8888.88;
    union {decimal(9, 2), null} maybeDec = 9999.99;
    timestamp_ms ts = 1526573732000; //ms from the unix epoch
    date dt = 600; //days from the unix epock, no time precission
  }

}

Generates:

/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package example.idl

case class LogicalIdl(dec: BigDecimal = scala.math.BigDecimal("8888.88"), maybeDec: Option[BigDecimal] = Some(scala.math.BigDecimal("9999.99")), ts: java.time.LocalDateTime = java.time.LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(1526573732000L), java.util.TimeZone.getDefault().toZoneId()), dt: java.time.LocalDate = java.time.LocalDate.ofEpochDay(600L))
francescopellegrini commented 6 years ago

41