All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lenses.jdbc4.avroExtensions.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package io.lenses.jdbc4

import org.apache.avro.LogicalTypes
import org.apache.avro.Schema

fun Schema.isNullable(): Boolean {
  return this.type == Schema.Type.UNION &&
      this.types.firstOrNull { it -> it.type == Schema.Type.NULL } != null
}

fun Schema.isNumber(): Boolean {
  return when (this.type) {
    Schema.Type.FLOAT, Schema.Type.INT, Schema.Type.DOUBLE, Schema.Type.LONG -> true
    else -> false
  }
}

fun Schema.scale(): Int {
  val logicalType = this.logicalType
  return when (logicalType) {
    is LogicalTypes.Decimal -> logicalType.scale
    else -> 0
  }
}

fun Schema.fromUnion(): Schema {
  return when (this.type) {
    Schema.Type.UNION -> {
      val schemaTypes = this.types
      return when {
        schemaTypes.size == 1 -> types[0]
        schemaTypes.size == 2 -> schemaTypes.first { it -> it.type != Schema.Type.NULL }
        else -> throw IllegalArgumentException("Not a Union schema")
      }
    }
    else -> this
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy