Skip to content
imranr edited this page Jan 6, 2014 · 2 revisions

Sumac supports many types out of the box, but we haven't added support for everything out there. You can easily add support for any types you like. You need to

  1. Create a class that implements the Parser interface. You might be able to start from the SimpleParser trait in some instances.
  2. Register that class with a call to registerParser

eg.

//some arbitrary class, from a 3rd party lib, or you define yourself
case class MyCoolClass(val name: String, val x: Int)

//define a parser which can convert from a String to a MyCoolClass
object MyCoolClassParser extends Parser[MyCoolClass] {
  def canParse(tpe:Type) = {
    ParseHelper.checkType(tpe, classOf[MyCoolClass])
  }
  def parse(s: String, tpe: Type, currentVal: AnyRef) = {
    val parts = s.split(":")
    CustomType(parts(0), parts(1).toInt)
  }
  override def valueAsString(currentVal: AnyRef) = {
    val ct = currentVal.asInstanceOf[MyCoolClass]
    ct.name + ":" + ct.x
  }
}

//then you can parse fields with that type:
class ArgsWithCustomType extends FieldArgs {
  registerParser(MyCoolClassParser)
  var x: Int = _
  var y: MyCoolClass = _
  var z: String = _
}

Note that the Parser api may change in future releases. In particular:

  1. Parsers will no longer be registered globally -- each Args class will register its own parsers.
  2. Parsers will change from Java Reflection to Scala Reflection. However, this will not happen in the near future -- not at least until scala 2.11.

Think you've created a parser that is useful to others also? Please submit it! We want to keep the core project free of external dependencies, but for now the "sumac-ext" project is a dumping ground for anything that integrates with 3rd party libs. Just add your parser in there, and submit a pull request.

Clone this wiki locally