简体   繁体   中英

how to generate a java array from a scala case class

I'm writing a small data access library to help me use Cassandra prepared statements in a Scala program (its not open source but maybe one day). What I'd like to do is automatically generate a Java Array for the bind statement from the case class

com.datastax.driver.core
PreparedStatement...
  public BoundStatement bind(Object... values);

So currently I have

case class Entity(foo:String, optionalBar:Option[String]) 

object Entity { 
  def toJArray(e:Entity) = { Array(e.foo, e.optionalBar.getOrElse(null)) } 
}

val e1 = Entity("fred", Option("bill"))
val e2 = Entity("fred", None)

Entity.toJArray(e1)
res5: Array[String] = Array(fred, bill)
Entity.toJArray(e2)
res6: Array[String] = Array(fred, null)

The toJArray returns an Array I can use in the bind statement. The boiler plate code gets worse if there is a date or double or a java enum

new java.util.Date(createdOn)
scala.Double.box(price)
priceType.name

Is there a way of automatically generating the Array in Scala assuming the bind parameters have the same order as the case class fields?

EDIT Thanks to @srgfed01 Here's what I came up with (not complete) but allows me to do something like

val customer1 = Customer( "email", "name", None, Option(new Date), OrdStatus.New)
session.execute(populate(customer1, insert))
val customer2 = Customer( "email2", "name2", Option(22), Option(new Date), OrdStatus.Rejected)
session.execute(populate(customer2, insert))

using this function

def populate(state:Product, statement:PreparedStatement): BoundStatement = {
  def set(bnd:BoundStatement, i:Int, aval:Any): Unit = {
    aval match {
      case v:Date => bnd.setDate(i, v)
      case v:Int => bnd.setInt(i, v)
      case v:Long => bnd.setLong(i, v)
      case v:Double => bnd.setDouble(i, v)
      case v:String => bnd.setString(i, v)
      case null => bnd.setToNull(i)
      case _ => bnd.setString(i, aval.toString)
    }
  }

  val bnd = statement.bind
  for(i <- 0 until state.productArity) {
    state.productElement(i) match {
      case op: Option[_]  => set(bnd, i, op.getOrElse(null))
      case v => set(bnd, i, v)
    }
  }
  bnd
}

You can use productIterator call for your case class object:

case class Entity(foo: String, optionalBar: Option[String])

val e1 = Entity("fred", Option("bill"))
val e2 = Entity("fred", None)

def run(e: Entity): Array[Any] = e.productIterator
  .map {
    case op: Option[_] => op.getOrElse(null)
    case v             => v
  }
  .toArray

println(run(e1).mkString(" ")) // fred bill
println(run(e2).mkString(" ")) // fred null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM