Encode/Decode some regex

import java.util.UUID
import com.github.gekomad.ittocsv.core.Types.implicits._
implicit val csvFormat = com.github.gekomad.ittocsv.parser.IttoCSVFormat.default

case class Bar(a: String, b: SHA1, c: SHA256, d: MD5, e: UUID, f: Email, g: IP6, h: BitcoinAdd, i: URL)

val sha1       = SHA1("1c18da5dbf74e3fc1820469cf1f54355b7eec92d")
val uuid       = UUID.fromString("1CC3CCBB-C749-3078-E050-1AACBE064651")
val md5        = MD5("23f8e84c1f4e7c8814634267bd456194")
val sha256     = SHA256("000020f89134d831f48541b2d8ec39397bc99fccf4cc86a3861257dbe6d819d1")
val email      = Email("daigoro@itto.com")
val ip         = IP6("2001:db8:a0b:12f0::1")
val bitcoinAdd = BitcoinAdd("3Nxwenay9Z8Lc9JBiywExpnEFiLp6Afp8v")
val url        = URL("http://www.aaa.cdd.com")

val bar = Bar("abc", sha1, sha256, md5, uuid, email, ip, bitcoinAdd, url)

val csvString =
  "abc,1c18da5dbf74e3fc1820469cf1f54355b7eec92d,000020f89134d831f48541b2d8ec39397bc99fccf4cc86a3861257dbe6d819d1,23f8e84c1f4e7c8814634267bd456194,1cc3ccbb-c749-3078-e050-1aacbe064651,daigoro@itto.com,2001:db8:a0b:12f0::1,3Nxwenay9Z8Lc9JBiywExpnEFiLp6Afp8v,http://www.aaa.cdd.com"

//encode
import com.github.gekomad.ittocsv.core.ToCsv._
assert(toCsv(bar) == csvString)

//decode
import com.github.gekomad.ittocsv.core.FromCsv._
assert(fromCsv[Bar](csvString) == List(Right(bar)))

Encode/Decode your own regex

Using encode for MyType:

implicit def _e(implicit csvFormat: IttoCSVFormat): CsvStringEncoder[MyType] = ???

Example encoding N:Int to “[N]”

import com.github.gekomad.ittocsv.core.CsvStringEncoder
import com.github.gekomad.ittocsv.parser.IttoCSVFormat
implicit val csvFormat: IttoCSVFormat = IttoCSVFormat.default

case class MyType(a: Int)
case class Foo(a: MyType, b: Int)

import com.github.gekomad.ittocsv.core.ToCsv._

implicit def _e(implicit csvFormat: IttoCSVFormat): CsvStringEncoder[MyType] = createEncoder { node =>
  csvConverter.stringToCsvField(s"[${node.a}]")
}

assert(toCsv(Foo(MyType(42),99)) == "[42],99")

Using decode for MyType:

implicit def _d(implicit csvFormat: IttoCSVFormat): String => Either[ParseFailure, MyType] = (str: String) => ???

Example decoding “[N]” to N:Int

import com.github.gekomad.ittocsv.parser.IttoCSVFormat
import scala.util.Try
implicit val csvFormat: IttoCSVFormat = IttoCSVFormat.default
import com.github.gekomad.ittocsv.core.ParseFailure
import cats.data.NonEmptyList

case class MyType(a: Int)
case class Foo(a: MyType, b: Int)

import com.github.gekomad.ittocsv.core.FromCsv._

implicit def _d(implicit csvFormat: IttoCSVFormat): String => Either[ParseFailure, MyType] = { str: String =>
  if (str.startsWith("[") && str.endsWith("]"))
    Try(str.substring(1, str.length - 1).toInt)
      .map(f => Right(MyType(f)))
      .getOrElse(Left(ParseFailure(s"Not a MyType $str")))
  else Left(ParseFailure(s"Wrong format $str"))

}

assert(fromCsv[Foo]("[42],99") == List(Right(Foo(MyType(42),99))))
assert(fromCsv[Foo]("[x],99") == List(Left(NonEmptyList(ParseFailure("Not a MyType [x]"), Nil))))
assert(fromCsv[Foo]("42,99") == List(Left(NonEmptyList(ParseFailure("Wrong format 42"), Nil))))

The TreeTest.scala shows how to encode/decode a Tree[Int]

Defined regex

Email

Ciphers

  • UUID (1CC3CCBB-C749-3078-E050-1AACBE064651)
  • MD5 (23f8e84c1f4e7c8814634267bd456194)
  • SHA1 (1c18da5dbf74e3fc1820469cf1f54355b7eec92d)
  • SHA256 (000020f89134d831f48541b2d8ec39397bc99fccf4cc86a3861257dbe6d819d1)

URL, IP, MAC Address

  • IP (10.192.168.1)
  • IP_6 (2001:db8:a0b:12f0::1)
  • URLs (http://</span>abc.def</span>.com)
  • Youtube (https://</span>www</span>.youtube</span>.com/watch?v=9bZkp7q19f0)
  • Facebook (https://</span>www</span>.facebook.</span>com/thesimpsons - https://</span>www</span>.facebook.</span>com/pages/)
  • Twitter (https://</span>twitter</span>.com/rtpharry)
  • MAC Address (fE:dC:bA:98:76:54)

HEX

  • HEX (#F0F0F0 - 0xF0F0F0)

Bitcoin

Phone numbers

Date time

Crontab

Codes

Concurrency

Strings

Logs

  • Apache error ([Fri Dec 16 02:25:55 2005] [error] [client 1.2.3.4] Client sent malformed Host header)

Numbers

Coordinates

  • Coordinate (N90.00.00 E180.00.00)
  • Coordinate1 (45°23’36.0” N 10°33’48.0” E)
  • Coordinate2 (12:12:12.223546”N - 15:17:6”S - 12°30’23.256547”S)

Programming