String Mapper

This library automates the task of mapping Strings to Things. A typical use-case is parsing CSV into a case class.

Build Status Maven Central

Usage

import be.icteam.stringmapper._

case class Item(title: String, name: String, id: Int, last: Option[String])

val conformInput = "sir,mike,3,x"
val malformedLeadingSpaceInId = "sir,mike, 3,x"
val malformedMissingColumn = "sir,mike,3"
val malformedAdditionalColumn = "sir,mike,3,x,BAD"

val itemCsvParser = CsvParser[Item]
assert(itemCsvParser.parse(conformInput) == Right(Item("sir", "mike", 3, Some("x"))))
assert(itemCsvParser.parse(malformedLeadingSpaceInId) == Left(List("java.lang.NumberFormatException: For input string: \" 3\"")))
assert(itemCsvParser.parse(malformedMissingColumn) == Left(List("unexpected end of line, still need to parse columns..")))
assert(itemCsvParser.parse(malformedAdditionalColumn) == Left(List("expected end of line, but still have 'BAD'")))

Customizing StringToThing:

val stringToItem: StringsToThing[Item] = {

    implicit def customIntFieldParser[K <: Symbol]: StringToThing[FieldType[K, Int]] = {
        // in this example we trim eventual whitespace
        StringToThing.stringTofieldType[K, Int](StringToThing[Int](_.trim.toInt))
    }

    implicit def customStringFieldParser[K <: Symbol](implicit witness: Witness.Aux[K]): StringToThing[FieldType[K, String]] = {
        // in this example we UPPERCASE the value of the 'name' field
        val nameParser = StringToThing[String](_.toUpperCase())
        val hParserToUse =
            if(witness.value == Symbol("name")) nameParser
            else implicitly[StringToThing[String]]
        StringToThing.stringTofieldType[K, String](hParserToUse)
    }

    implicitly[StringsToThing[Item]]
}

implicit val customStringToItem = stringToItem
val customItemCsvParser = CsvParser[Item]
// the whitespace in ' 3' is ignored and name is in uppercase
assert(customItemCsvParser.parse(malformedLeadingSpaceInId) == Right(Item("sir", "MIKE", 3, Some("x"))))

Development

Compile and test:

sbt +clean; +cleanFiles; +compile; +test

Install a snapshot in your local maven repository:

sbt +publishM2

Release

Set the following environment variables:

  • PGP_PASSPHRASE
  • PGP_SECRET
  • SONATYPE_USERNAME
  • SONATYPE_PASSWORD
export SONATYPE_USERNAME=timvw
export SONATYPE_PASSWORD=XXX
export PGP_PASSPHRASE=XXX
export PGP_SECRET=$(gpg --armor --export-secret-keys 0x6E4CD7D2EAC83E19 | base64)

Leveraging the ci-release plugin:

sbt ci-release # release a SNAPSHOT version
CI_COMMIT_TAG=v0.0.2 sbt 'set version := "0.0.2"' ci-release # release 0.0.2

Find the most recent release:

git ls-remote --tags | \
  awk -F"/" '{print $3}' | \
  grep '^v[0-9]*\.[0-9]*\.[0-9]*' | \
  grep -v {} | \
  sort --version-sort | \
  tail -n1

Push a new tag to trigger a release via travis-ci:

v=v1.0.5
git tag -a $v  -m $v
git push origin $v

License

Code is provided under the Apache 2.0 license available at http://opensource.org/licenses/Apache-2.0, as well as in the LICENSE file. This is the same license used as Spark and Frameless.