From fcd519846f2e57761fa2fbb0f6cdefe35e5c3066 Mon Sep 17 00:00:00 2001 From: Alfredo Torre Date: Thu, 14 Dec 2023 13:09:11 +0100 Subject: [PATCH] Scalafmt --- src/main/scala/purecsv/config/Headers.scala | 4 +- src/main/scala/purecsv/config/Trimming.scala | 2 +- .../scala/purecsv/csviterable/package.scala | 34 ++++++---- .../purecsv/safe/converter/Converter.scala | 26 +++---- .../defaults/rawfields/package.scala | 29 ++++---- .../converter/defaults/string/package.scala | 36 +++++----- src/main/scala/purecsv/safe/package.scala | 68 ++++++++++--------- .../scala/purecsv/safe/tryutil/TryUtil.scala | 15 ++-- .../scala/purecsv/unsafe/RecordSplitter.scala | 18 ++--- .../purecsv/unsafe/RecordSplitterImpl.scala | 13 ++-- .../purecsv/unsafe/converter/Converter.scala | 31 +++++---- .../defaults/rawfields/package.scala | 22 +++--- .../converter/defaults/string/package.scala | 39 +++++------ src/main/scala/purecsv/unsafe/package.scala | 51 ++++++++------ src/main/scala/purecsv/util/FileUtil.scala | 6 +- .../scala/purecsv/RecordSplitterSuite.scala | 19 ++++-- .../purecsv/csviterable/CSVRecordTest.scala | 13 ++-- src/test/scala/purecsv/package.scala | 9 ++- .../scala/purecsv/safe/TryUtilSuite.scala | 18 ++--- .../purecsv/safe/absentColumnsSuite.scala | 17 ++--- .../safe/converter/ConverterSuite.scala | 46 ++++++------- .../safe/customerDelimiterSafeSuite.scala | 20 ++++-- .../scala/purecsv/safe/headersSuite.scala | 7 +- src/test/scala/purecsv/safe/safeSuite.scala | 24 ++++--- .../unsafe/converter/ConverterSuite.scala | 48 ++++++------- .../unsafe/customerDelimiterUnsafeSuite.scala | 16 +++-- .../scala/purecsv/unsafe/unsafeSuite.scala | 14 ++-- 27 files changed, 352 insertions(+), 293 deletions(-) diff --git a/src/main/scala/purecsv/config/Headers.scala b/src/main/scala/purecsv/config/Headers.scala index 3a46a9a..7768188 100644 --- a/src/main/scala/purecsv/config/Headers.scala +++ b/src/main/scala/purecsv/config/Headers.scala @@ -4,6 +4,6 @@ sealed trait Headers object Headers { object ReadAndIgnore extends Headers - object None extends Headers - object ParseHeaders extends Headers + object None extends Headers + object ParseHeaders extends Headers } diff --git a/src/main/scala/purecsv/config/Trimming.scala b/src/main/scala/purecsv/config/Trimming.scala index 5202e98..e8cd6d4 100644 --- a/src/main/scala/purecsv/config/Trimming.scala +++ b/src/main/scala/purecsv/config/Trimming.scala @@ -16,4 +16,4 @@ object Trimming { object TrimAll extends Trimming { override def trim(s: String): String = s.trim } -} \ No newline at end of file +} diff --git a/src/main/scala/purecsv/csviterable/package.scala b/src/main/scala/purecsv/csviterable/package.scala index 671c33d..8ba5b78 100644 --- a/src/main/scala/purecsv/csviterable/package.scala +++ b/src/main/scala/purecsv/csviterable/package.scala @@ -19,11 +19,9 @@ import java.io.{File, PrintWriter} import purecsv.unsafe.RecordSplitter.defaultFieldSeparatorStr import purecsv.unsafe.converter.Converter - package object csviterable { - - implicit class CSVRecord[A,R <: Converter[A,Seq[String]]](a: A)(implicit rfc: R) { + implicit class CSVRecord[A, R <: Converter[A, Seq[String]]](a: A)(implicit rfc: R) { def toCSV(sep: String = defaultFieldSeparatorStr): String = rfc.to(a).mkString(sep) } @@ -32,11 +30,14 @@ package object csviterable { * Helper class that adds methods for converting and writing an Iterable of [[A]] into CSV format. * * @param iter - * @param rfc The implicit class that allows converting [[A]] to a [[Seq]] - * @tparam A The type that can be converted to a CSV record - * @tparam R The type of the converter [[A]] <-> [[Seq]] + * @param rfc + * The implicit class that allows converting [[A]] to a [[Seq]] + * @tparam A + * The type that can be converted to a CSV record + * @tparam R + * The type of the converter [[A]] <-> [[Seq]] */ - implicit class CSVIterable[A,R <: Converter[A,Seq[String]]](iter: Iterable[A])(implicit rfc: R) { + implicit class CSVIterable[A, R <: Converter[A, Seq[String]]](iter: Iterable[A])(implicit rfc: R) { /** Convert all the values in [[iter]] into CSV lines */ def toCSVLines(sep: String = defaultFieldSeparatorStr): Iterable[String] = @@ -48,9 +49,12 @@ package object csviterable { /** * Convert [[iter]] to CSV lines and then write them into the [[PrintWriter]] * - * @param writer Where to write the CSV lines - * @param sep The CSV separator - * @param header An optional header. If it is set, then it is printed as first line + * @param writer + * Where to write the CSV lines + * @param sep + * The CSV separator + * @param header + * An optional header. If it is set, then it is printed as first line */ def writeCSVTo(writer: PrintWriter, sep: String, header: Option[Seq[String]]): Unit = { header.foreach(h => writer.println(h.mkString(sep))) @@ -58,7 +62,10 @@ package object csviterable { } /** @see [[writeCSVTo]] */ - def writeCSVToFile(file: File, sep: String = defaultFieldSeparatorStr, header: Option[Seq[String]] = None): Unit = { + def writeCSVToFile(file: File, + sep: String = defaultFieldSeparatorStr, + header: Option[Seq[String]] = None + ): Unit = { val writer = new PrintWriter(file, "utf-8") try { this.writeCSVTo(writer, sep, header) @@ -68,7 +75,10 @@ package object csviterable { } /** @see [[writeCSVToFile(File,String,Option[Seq[String]]):Unit*]] */ - def writeCSVToFileName(fileName: String, sep: String = defaultFieldSeparatorStr, header: Option[Seq[String]] = None): Unit = { + def writeCSVToFileName(fileName: String, + sep: String = defaultFieldSeparatorStr, + header: Option[Seq[String]] = None + ): Unit = { writeCSVToFile(new File(fileName), sep, header) } } diff --git a/src/main/scala/purecsv/safe/converter/Converter.scala b/src/main/scala/purecsv/safe/converter/Converter.scala index f9d471d..6b57a0a 100644 --- a/src/main/scala/purecsv/safe/converter/Converter.scala +++ b/src/main/scala/purecsv/safe/converter/Converter.scala @@ -16,18 +16,18 @@ package purecsv.safe.converter import scala.util.{Failure, Success, Try} - /** - * A version of [[purecsv.unsafe.converter.Converter]] with a special from method - * for safe conversions. The method [[Converter!from()]] is defined starting from - * [[Converter!tryFrom()]] but it throw [[IllegalArgumentException]] when - * the result is [[Failure]] + * A version of [[purecsv.unsafe.converter.Converter]] with a special from method for safe conversions. The method + * [[Converter!from()]] is defined starting from [[Converter!tryFrom()]] but it throw [[IllegalArgumentException]] + * when the result is [[Failure]] */ -trait Converter[A,B] extends purecsv.unsafe.converter.Converter[A,B] { +trait Converter[A, B] extends purecsv.unsafe.converter.Converter[A, B] { + /** - * @param b The starting value from which we try the conversion to [[A]] - * @return A value of type [[A]] wrapped in [[Success]] if the conversion is successful else [[Failure]] with the - * error + * @param b + * The starting value from which we try the conversion to [[A]] + * @return + * A value of type [[A]] wrapped in [[Success]] if the conversion is successful else [[Failure]] with the error */ def tryFrom(b: B): Try[A] final override def from(b: B): A = tryFrom(b) match { @@ -37,7 +37,7 @@ trait Converter[A,B] extends purecsv.unsafe.converter.Converter[A,B] { } /** Converter from/to String */ -trait StringConverter[A] extends Converter[A,String] +trait StringConverter[A] extends Converter[A, String] object StringConverter { def apply[A](implicit conv: StringConverter[A]): StringConverter[A] = conv @@ -46,13 +46,13 @@ object StringConverter { object StringConverterUtils { def mkStringConverter[A](fromF: String => Try[A], toF: A => String) = new StringConverter[A] { def tryFrom(s: String): Try[A] = fromF(s) - def to(a: A): String = toF(a) + def to(a: A): String = toF(a) } } /** Converter from/to raw fields, represented as sequence of strings */ -trait RawFieldsConverter[A] extends Converter[A,Seq[String]] +trait RawFieldsConverter[A] extends Converter[A, Seq[String]] object RawFieldsConverter { def apply[A](implicit conv: RawFieldsConverter[A]): RawFieldsConverter[A] = conv -} \ No newline at end of file +} diff --git a/src/main/scala/purecsv/safe/converter/defaults/rawfields/package.scala b/src/main/scala/purecsv/safe/converter/defaults/rawfields/package.scala index 5b23968..ef66f7f 100644 --- a/src/main/scala/purecsv/safe/converter/defaults/rawfields/package.scala +++ b/src/main/scala/purecsv/safe/converter/defaults/rawfields/package.scala @@ -19,7 +19,6 @@ import shapeless.{::, Generic, HList, HNil} import scala.util.{Failure, Success, Try} - package object rawfields { import purecsv.safe.converter.RawFieldsConverter @@ -31,30 +30,32 @@ package object rawfields { implicit val deriveHNil: RawFieldsConverter[HNil] = new RawFieldsConverter[HNil] { override def tryFrom(s: Seq[String]): Try[HNil] = s match { case Nil => Success(HNil) - case _ => illegalConversion(s.mkString("[",", ","]"), "HNil") + case _ => illegalConversion(s.mkString("[", ", ", "]"), "HNil") } override def to(a: HNil): Seq[String] = Seq.empty } - implicit def deriveHCons[V, T <: HList] - (implicit sc: StringConverter[V], - fto: RawFieldsConverter[T]) - : RawFieldsConverter[V :: T] = new RawFieldsConverter[V :: T] { + implicit def deriveHCons[V, T <: HList](implicit + sc: StringConverter[V], + fto: RawFieldsConverter[T] + ): RawFieldsConverter[V :: T] = new RawFieldsConverter[V :: T] { override def tryFrom(s: Seq[String]): Try[V :: T] = s match { case Nil => illegalConversion("", classOf[V :: T].toString) - case _ => for { - head <- sc.tryFrom(s.head) - tail <- fto.tryFrom(s.tail) - } yield head :: tail + case _ => + for { + head <- sc.tryFrom(s.head) + tail <- fto.tryFrom(s.tail) + } yield head :: tail } override def to(a: ::[V, T]): Seq[String] = sc.to(a.head) +: fto.to(a.tail) } - implicit def deriveClass[A, R](implicit gen: Generic.Aux[A, R], - conv: RawFieldsConverter[R]) - : RawFieldsConverter[A] = new RawFieldsConverter[A] { + implicit def deriveClass[A, R](implicit + gen: Generic.Aux[A, R], + conv: RawFieldsConverter[R] + ): RawFieldsConverter[A] = new RawFieldsConverter[A] { override def tryFrom(s: Seq[String]): Try[A] = conv.tryFrom(s).map(gen.from) - override def to(a: A): Seq[String] = conv.to(gen.to(a)) + override def to(a: A): Seq[String] = conv.to(gen.to(a)) } } diff --git a/src/main/scala/purecsv/safe/converter/defaults/string/package.scala b/src/main/scala/purecsv/safe/converter/defaults/string/package.scala index 62bdc1d..bad69e3 100644 --- a/src/main/scala/purecsv/safe/converter/defaults/string/package.scala +++ b/src/main/scala/purecsv/safe/converter/defaults/string/package.scala @@ -20,31 +20,31 @@ import purecsv.unsafe.converter.StringConverterUtils import scala.util.{Success, Try} - package object string { import purecsv.safe.converter.StringConverter import purecsv.safe.converter.StringConverterUtils.mkStringConverter import purecsv.unsafe.converter.defaults.string.{strToBool, strToChar} - implicit val boolc: StringConverter[Boolean] = mkStringConverter(s => Try(strToBool(s)),_.toString) - implicit val bytec: StringConverter[Byte] = mkStringConverter(s => Try(s.toByte),_.toString) - implicit val charc: StringConverter[Char] = mkStringConverter(s => Try(strToChar(s)),_.toString) - implicit val doublec: StringConverter[Double] = mkStringConverter(s => Try(s.toDouble),_.toString) - implicit val floatc: StringConverter[Float] = mkStringConverter(s => Try(s.toFloat),_.toString) - implicit val intc: StringConverter[Int] = mkStringConverter(s => Try(s.toInt),_.toString) - implicit val longc: StringConverter[Long] = mkStringConverter(s => Try(s.toLong),_.toString) - implicit val shortc: StringConverter[Short] = mkStringConverter(s => Try(s.toShort),_.toString) - implicit val uuidc: StringConverter[UUID] = mkStringConverter(s => Try(UUID.fromString(s)),_.toString) - implicit val stringc: StringConverter[String] = new StringConverter[String] { + implicit val boolc: StringConverter[Boolean] = mkStringConverter(s => Try(strToBool(s)), _.toString) + implicit val bytec: StringConverter[Byte] = mkStringConverter(s => Try(s.toByte), _.toString) + implicit val charc: StringConverter[Char] = mkStringConverter(s => Try(strToChar(s)), _.toString) + implicit val doublec: StringConverter[Double] = mkStringConverter(s => Try(s.toDouble), _.toString) + implicit val floatc: StringConverter[Float] = mkStringConverter(s => Try(s.toFloat), _.toString) + implicit val intc: StringConverter[Int] = mkStringConverter(s => Try(s.toInt), _.toString) + implicit val longc: StringConverter[Long] = mkStringConverter(s => Try(s.toLong), _.toString) + implicit val shortc: StringConverter[Short] = mkStringConverter(s => Try(s.toShort), _.toString) + implicit val uuidc: StringConverter[UUID] = mkStringConverter(s => Try(UUID.fromString(s)), _.toString) + implicit val stringc: StringConverter[String] = new StringConverter[String] { override def tryFrom(s: String): Try[String] = Success(s) - override def to(s: String): String = StringConverterUtils.quoteTextIfNecessary(s) + override def to(s: String): String = StringConverterUtils.quoteTextIfNecessary(s) } - implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = new StringConverter[Option[A]] { - override def tryFrom(s: String): Try[Option[A]] = s match { - case "" => Success(None) - case s => ac.tryFrom(s).map(Some(_)) + implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = + new StringConverter[Option[A]] { + override def tryFrom(s: String): Try[Option[A]] = s match { + case "" => Success(None) + case s => ac.tryFrom(s).map(Some(_)) + } + override def to(v: Option[A]): String = v.map(ac.to).getOrElse("") } - override def to(v: Option[A]): String = v.map(ac.to).getOrElse("") - } } diff --git a/src/main/scala/purecsv/safe/package.scala b/src/main/scala/purecsv/safe/package.scala index 69eb1a4..28d59b0 100644 --- a/src/main/scala/purecsv/safe/package.scala +++ b/src/main/scala/purecsv/safe/package.scala @@ -31,14 +31,14 @@ import scala.util.Try package object safe { // String - implicit val boolc: StringConverter[Boolean] = purecsv.safe.converter.defaults.string.boolc - implicit val bytec: StringConverter[Byte] = purecsv.safe.converter.defaults.string.bytec - implicit val charc: StringConverter[Char] = purecsv.safe.converter.defaults.string.charc + implicit val boolc: StringConverter[Boolean] = purecsv.safe.converter.defaults.string.boolc + implicit val bytec: StringConverter[Byte] = purecsv.safe.converter.defaults.string.bytec + implicit val charc: StringConverter[Char] = purecsv.safe.converter.defaults.string.charc implicit val doublec: StringConverter[Double] = purecsv.safe.converter.defaults.string.doublec - implicit val floatc: StringConverter[Float] = purecsv.safe.converter.defaults.string.floatc - implicit val intc: StringConverter[Int] = purecsv.safe.converter.defaults.string.intc - implicit val longc: StringConverter[Long] = purecsv.safe.converter.defaults.string.longc - implicit val shortc: StringConverter[Short] = purecsv.safe.converter.defaults.string.shortc + implicit val floatc: StringConverter[Float] = purecsv.safe.converter.defaults.string.floatc + implicit val intc: StringConverter[Int] = purecsv.safe.converter.defaults.string.intc + implicit val longc: StringConverter[Long] = purecsv.safe.converter.defaults.string.longc + implicit val shortc: StringConverter[Short] = purecsv.safe.converter.defaults.string.shortc implicit val stringc: StringConverter[String] = purecsv.safe.converter.defaults.string.stringc implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = @@ -47,13 +47,17 @@ package object safe { // Raw Fields implicit val deriveHNil: RawFieldsConverter[HNil] = purecsv.safe.converter.defaults.rawfields.deriveHNil - implicit def deriveHCons[V, T <: HList](implicit sc: StringConverter[V], - fto: RawFieldsConverter[T]): RawFieldsConverter[V :: T] = { + implicit def deriveHCons[V, T <: HList](implicit + sc: StringConverter[V], + fto: RawFieldsConverter[T] + ): RawFieldsConverter[V :: T] = { purecsv.safe.converter.defaults.rawfields.deriveHCons } - implicit def deriveClass[A, R](implicit gen: Generic.Aux[A, R], - conv: RawFieldsConverter[R]): RawFieldsConverter[A] = { + implicit def deriveClass[A, R](implicit + gen: Generic.Aux[A, R], + conv: RawFieldsConverter[R] + ): RawFieldsConverter[A] = { purecsv.safe.converter.defaults.rawfields.deriveClass } @@ -67,28 +71,29 @@ package object safe { def rfc: RawFieldsConverter[A] - def readCSVFromReader( - r: Reader, - delimiter: Char = RecordSplitter.defaultFieldSeparator, - trimming: Trimming = NoAction, - headers: Headers = ParseHeaders, - headerMapping: Map[String, String] = Map.empty)(implicit classTag: ClassTag[A]): Iterator[Try[A]] = { + def readCSVFromReader(r: Reader, + delimiter: Char = RecordSplitter.defaultFieldSeparator, + trimming: Trimming = NoAction, + headers: Headers = ParseHeaders, + headerMapping: Map[String, String] = Map.empty + )(implicit classTag: ClassTag[A]): Iterator[Try[A]] = { RecordSplitterImpl .getRecords(r, caseClassParams[A], delimiter, trimming = trimming, headers = headers, - headerMapping = headerMapping) + headerMapping = headerMapping + ) .map(record => rfc.tryFrom(record.toSeq)) } - def readCSVFromString( - s: String, - delimiter: Char = RecordSplitter.defaultFieldSeparator, - trimming: Trimming = NoAction, - headers: Headers = ParseHeaders, - headerMapping: Map[String, String] = Map.empty)(implicit classTag: ClassTag[A]): List[Try[A]] = { + def readCSVFromString(s: String, + delimiter: Char = RecordSplitter.defaultFieldSeparator, + trimming: Trimming = NoAction, + headers: Headers = ParseHeaders, + headerMapping: Map[String, String] = Map.empty + )(implicit classTag: ClassTag[A]): List[Try[A]] = { val r = new StringReader(s) try { readCSVFromReader(r, delimiter, trimming, headers, headerMapping).toList @@ -101,7 +106,8 @@ package object safe { delimiter: Char = RecordSplitter.defaultFieldSeparator, trimming: Trimming = NoAction, headers: Headers = ParseHeaders, - headerMapping: Map[String, String] = Map.empty)(implicit classTag: ClassTag[A]): List[Try[A]] = { + headerMapping: Map[String, String] = Map.empty + )(implicit classTag: ClassTag[A]): List[Try[A]] = { val r = FileUtil.createReader(f) try { readCSVFromReader(r, delimiter, trimming, headers, headerMapping).toList @@ -110,12 +116,12 @@ package object safe { } } - def readCSVFromFileName( - fileName: String, - delimiter: Char = RecordSplitter.defaultFieldSeparator, - trimming: Trimming = NoAction, - headers: Headers = ParseHeaders, - headerMapping: Map[String, String] = Map.empty)(implicit classTag: ClassTag[A]): List[Try[A]] = { + def readCSVFromFileName(fileName: String, + delimiter: Char = RecordSplitter.defaultFieldSeparator, + trimming: Trimming = NoAction, + headers: Headers = ParseHeaders, + headerMapping: Map[String, String] = Map.empty + )(implicit classTag: ClassTag[A]): List[Try[A]] = { readCSVFromFile(new File(fileName), delimiter, trimming, headers, headerMapping) } diff --git a/src/main/scala/purecsv/safe/tryutil/TryUtil.scala b/src/main/scala/purecsv/safe/tryutil/TryUtil.scala index 7e2188a..2c3fa59 100644 --- a/src/main/scala/purecsv/safe/tryutil/TryUtil.scala +++ b/src/main/scala/purecsv/safe/tryutil/TryUtil.scala @@ -17,20 +17,19 @@ package purecsv.safe import scala.collection.immutable import scala.util.{Failure, Success, Try} - package object tryutil { implicit class IterableOfTry[A](iter: Iterable[Try[A]]) { + /** A tuple composed by the successes and the failures */ - lazy val getSuccessesAndFailures: (immutable.List[(Int,A)], - immutable.List[(Int,Throwable)]) = { - val successes = scala.collection.mutable.Buffer[(Int,A)]() - val failures = scala.collection.mutable.Buffer[(Int,Throwable)]() + lazy val getSuccessesAndFailures: (immutable.List[(Int, A)], immutable.List[(Int, Throwable)]) = { + val successes = scala.collection.mutable.Buffer[(Int, A)]() + val failures = scala.collection.mutable.Buffer[(Int, Throwable)]() iter.zipWithIndex.foreach { - case (Success(a),i) => successes += (i+1 -> a) - case (Failure(f),i) => failures += (i+1 -> f) + case (Success(a), i) => successes += (i + 1 -> a) + case (Failure(f), i) => failures += (i + 1 -> f) } - (successes.toList,failures.toList) + (successes.toList, failures.toList) } } diff --git a/src/main/scala/purecsv/unsafe/RecordSplitter.scala b/src/main/scala/purecsv/unsafe/RecordSplitter.scala index f8a21e6..53e4a6b 100644 --- a/src/main/scala/purecsv/unsafe/RecordSplitter.scala +++ b/src/main/scala/purecsv/unsafe/RecordSplitter.scala @@ -18,10 +18,10 @@ import purecsv.config.Headers.ParseHeaders import purecsv.config.{Headers, Trimming} object RecordSplitter { - val defaultFieldSeparator = ',' + val defaultFieldSeparator = ',' val defaultFieldSeparatorStr = defaultFieldSeparator.toString - val defaultQuoteChar = '"' - val defaultQuoteStr = defaultQuoteChar.toString + val defaultQuoteChar = '"' + val defaultQuoteStr = defaultQuoteChar.toString } trait RecordSplitter[R] { @@ -33,19 +33,21 @@ trait RecordSplitter[R] { headers: Headers, trimming: Trimming, fields: Seq[String], - headerMapping: Map[String, String]): Iterator[Iterable[String]] + headerMapping: Map[String, String] + ): Iterator[Iterable[String]] /** - * Like [[getRecords(R, Char, Char, Int):Iterator[Iterable[String]]*]] but with all parameters except the first set - * to defaults and first line set to 0 - */ + * Like [[getRecords(R, Char, Char, Int):Iterator[Iterable[String]]*]] but with all parameters except the first + * set to defaults and first line set to 0 + */ def getRecords(r: R, fields: Seq[String], fieldSep: Char = RecordSplitter.defaultFieldSeparator, quoteChar: Char = RecordSplitter.defaultQuoteChar, headers: Headers = ParseHeaders, trimming: Trimming = Trimming.NoAction, - headerMapping: Map[String, String] = Map.empty): Iterator[Iterable[String]] = { + headerMapping: Map[String, String] = Map.empty + ): Iterator[Iterable[String]] = { getRecords(r, fieldSep, quoteChar, headers, trimming, fields, headerMapping) } } diff --git a/src/main/scala/purecsv/unsafe/RecordSplitterImpl.scala b/src/main/scala/purecsv/unsafe/RecordSplitterImpl.scala index 01a44c5..8429375 100644 --- a/src/main/scala/purecsv/unsafe/RecordSplitterImpl.scala +++ b/src/main/scala/purecsv/unsafe/RecordSplitterImpl.scala @@ -20,8 +20,8 @@ import com.github.tototoshi.csv.{DefaultCSVFormat, CSVReader => TototoshiCSVRead import purecsv.config.{Headers, Trimming} /** - * A [[purecsv.unsafe.RecordSplitter]] that uses the scala-csv library for extracting records from a [[Reader]] - */ + * A [[purecsv.unsafe.RecordSplitter]] that uses the scala-csv library for extracting records from a [[Reader]] + */ object RecordSplitterImpl extends RecordSplitter[Reader] { private val EmptyString = "" @@ -31,7 +31,8 @@ object RecordSplitterImpl extends RecordSplitter[Reader] { headers: Headers, trimming: Trimming, fields: Seq[String], - headerMapping: Map[String, String]): Iterator[Iterable[String]] = { + headerMapping: Map[String, String] + ): Iterator[Iterable[String]] = { implicit val csvFormat: DefaultCSVFormat = new DefaultCSVFormat { override val delimiter: Char = fieldSep override val quoteChar: Char = quoteCharacter @@ -53,7 +54,8 @@ object RecordSplitterImpl extends RecordSplitter[Reader] { private def toValuesIteratorWithHeadersOrdering(csvReader: TototoshiCSVReader, trimming: Trimming, fields: Seq[String], - headerMapping: Map[String, String]) = + headerMapping: Map[String, String] + ) = csvReader.iteratorWithHeaders .map(line => line.view.mapValues(trimming.trim)) .filter(array => array.size != 1 || array.head._2.trim != EmptyString) @@ -61,7 +63,8 @@ object RecordSplitterImpl extends RecordSplitter[Reader] { private def toValuesIteratorWithoutHeadersOrdering(csvReader: TototoshiCSVReader, trimming: Trimming, - linesToBeDropped: Int) = + linesToBeDropped: Int + ) = csvReader.iterator .drop(linesToBeDropped) .map(line => line.map(trimming.trim)) diff --git a/src/main/scala/purecsv/unsafe/converter/Converter.scala b/src/main/scala/purecsv/unsafe/converter/Converter.scala index 59f51ea..da59395 100644 --- a/src/main/scala/purecsv/unsafe/converter/Converter.scala +++ b/src/main/scala/purecsv/unsafe/converter/Converter.scala @@ -19,28 +19,35 @@ import java.io.StringWriter import com.github.tototoshi.csv.CSVWriter /** Typeclass for Converters of A from/to B */ -trait Converter[A,B] extends Serializable { +trait Converter[A, B] extends Serializable { + /** - * @param b The initial value - * @return b converted to the type [[A]] - * @throws IllegalArgumentException if b cannot be converted to [[A]] + * @param b + * The initial value + * @return + * b converted to the type [[A]] + * @throws IllegalArgumentException + * if b cannot be converted to [[A]] */ def from(b: B): A /** - * @param a The initial value - * @return a converted to the type [[B]] - * @throws IllegalArgumentException if a cannot be converted to [[B]] + * @param a + * The initial value + * @return + * a converted to the type [[B]] + * @throws IllegalArgumentException + * if a cannot be converted to [[B]] */ def to(a: A): B } object Converter { - def apply[A,B](implicit conv: Converter[A,B]): Converter[A,B] = conv + def apply[A, B](implicit conv: Converter[A, B]): Converter[A, B] = conv } /** Converter from/to String */ -trait StringConverter[A] extends Converter[A,String] +trait StringConverter[A] extends Converter[A, String] object StringConverter { def apply[A](implicit conv: StringConverter[A]): StringConverter[A] = conv @@ -49,7 +56,7 @@ object StringConverter { object StringConverterUtils { def mkStringConverter[A](fromF: String => A, toF: A => String) = new StringConverter[A] { def from(s: String): A = fromF(s) - def to(a: A): String = toF(a) + def to(a: A): String = toF(a) } def quoteTextIfNecessary(s: String): String = { val sw = new StringWriter(64) @@ -71,8 +78,8 @@ object StringConverterUtils { } /** Converter from/to raw fields, represented as sequence of strings */ -trait RawFieldsConverter[A] extends Converter[A,Seq[String]] +trait RawFieldsConverter[A] extends Converter[A, Seq[String]] object RawFieldsConverter { def apply[A](implicit conv: RawFieldsConverter[A]): RawFieldsConverter[A] = conv -} \ No newline at end of file +} diff --git a/src/main/scala/purecsv/unsafe/converter/defaults/rawfields/package.scala b/src/main/scala/purecsv/unsafe/converter/defaults/rawfields/package.scala index 9491bd0..96b1ac1 100644 --- a/src/main/scala/purecsv/unsafe/converter/defaults/rawfields/package.scala +++ b/src/main/scala/purecsv/unsafe/converter/defaults/rawfields/package.scala @@ -17,7 +17,6 @@ package purecsv.unsafe.converter.defaults import purecsv.unsafe.converter.StringConverter import shapeless.{::, Generic, HList, HNil} - package object rawfields { import purecsv.unsafe.converter.RawFieldsConverter @@ -25,15 +24,15 @@ package object rawfields { implicit val deriveHNil: RawFieldsConverter[HNil] = new RawFieldsConverter[HNil] { override def from(s: Seq[String]): HNil = s match { case Nil => HNil - case _ => throw new IllegalArgumentException(s"'$s' cannot be converted to HNil") + case _ => throw new IllegalArgumentException(s"'$s' cannot be converted to HNil") } override def to(a: HNil): Seq[String] = Seq.empty } - implicit def deriveHCons[V, T <: HList] - (implicit sc: StringConverter[V], - fto: RawFieldsConverter[T]) - : RawFieldsConverter[V :: T] = new RawFieldsConverter[V :: T] { + implicit def deriveHCons[V, T <: HList](implicit + sc: StringConverter[V], + fto: RawFieldsConverter[T] + ): RawFieldsConverter[V :: T] = new RawFieldsConverter[V :: T] { override def from(s: Seq[String]): ::[V, T] = s match { case Nil => throw new IllegalArgumentException(s"The empty String cannot be converted to HList") case _ => sc.from(s.head) :: fto.from(s.tail) @@ -42,10 +41,11 @@ package object rawfields { override def to(a: ::[V, T]): Seq[String] = sc.to(a.head) +: fto.to(a.tail) } - implicit def deriveClass[A, R](implicit gen: Generic.Aux[A, R], - conv: RawFieldsConverter[R]) - : RawFieldsConverter[A] = new RawFieldsConverter[A] { + implicit def deriveClass[A, R](implicit + gen: Generic.Aux[A, R], + conv: RawFieldsConverter[R] + ): RawFieldsConverter[A] = new RawFieldsConverter[A] { override def from(s: Seq[String]): A = gen.from(conv.from(s)) - override def to(a: A): Seq[String] = conv.to(gen.to(a)) + override def to(a: A): Seq[String] = conv.to(gen.to(a)) } -} \ No newline at end of file +} diff --git a/src/main/scala/purecsv/unsafe/converter/defaults/string/package.scala b/src/main/scala/purecsv/unsafe/converter/defaults/string/package.scala index 0114cce..485808f 100644 --- a/src/main/scala/purecsv/unsafe/converter/defaults/string/package.scala +++ b/src/main/scala/purecsv/unsafe/converter/defaults/string/package.scala @@ -23,7 +23,7 @@ package object string { import purecsv.unsafe.converter.StringConverter def strToBool(s: String): Boolean = s match { - case "1" | "true" | "True" | "TRUE" => true + case "1" | "true" | "True" | "TRUE" => true case "0" | "false" | "False" | "FALSE" => false case _ => throw new IllegalArgumentException(s"'$s' cannot be converter to boolean") } @@ -33,26 +33,27 @@ package object string { else throw new IllegalArgumentException(s"'$s' cannot be converted to char") } - implicit val boolc: StringConverter[Boolean] = mkStringConverter(strToBool,_.toString) - implicit val bytec: StringConverter[Byte] = mkStringConverter(_.toByte,_.toString) - implicit val charc: StringConverter[Char] = mkStringConverter(strToChar,_.toString) - implicit val doublec: StringConverter[Double] = mkStringConverter(_.toDouble,_.toString) - implicit val floatc: StringConverter[Float] = mkStringConverter(_.toFloat,_.toString) - implicit val intc: StringConverter[Int] = mkStringConverter(_.toInt,_.toString) - implicit val longc: StringConverter[Long] = mkStringConverter(_.toLong,_.toString) - implicit val shortc: StringConverter[Short] = mkStringConverter(_.toShort,_.toString) - implicit val uuidc: StringConverter[UUID] = mkStringConverter(UUID.fromString,_.toString) - implicit val stringc: StringConverter[String] = new StringConverter[String] { + implicit val boolc: StringConverter[Boolean] = mkStringConverter(strToBool, _.toString) + implicit val bytec: StringConverter[Byte] = mkStringConverter(_.toByte, _.toString) + implicit val charc: StringConverter[Char] = mkStringConverter(strToChar, _.toString) + implicit val doublec: StringConverter[Double] = mkStringConverter(_.toDouble, _.toString) + implicit val floatc: StringConverter[Float] = mkStringConverter(_.toFloat, _.toString) + implicit val intc: StringConverter[Int] = mkStringConverter(_.toInt, _.toString) + implicit val longc: StringConverter[Long] = mkStringConverter(_.toLong, _.toString) + implicit val shortc: StringConverter[Short] = mkStringConverter(_.toShort, _.toString) + implicit val uuidc: StringConverter[UUID] = mkStringConverter(UUID.fromString, _.toString) + implicit val stringc: StringConverter[String] = new StringConverter[String] { override def from(s: String): String = s - override def to(s: String): String = StringConverterUtils.quoteTextIfNecessary(s) + override def to(s: String): String = StringConverterUtils.quoteTextIfNecessary(s) } - implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = new StringConverter[Option[A]] { - override def from(s: String): Option[A] = s match { - case "" => None - case x => Some(ac.from(x)) + implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = + new StringConverter[Option[A]] { + override def from(s: String): Option[A] = s match { + case "" => None + case x => Some(ac.from(x)) + } + override def to(v: Option[A]): String = v.map(ac.to).getOrElse("") } - override def to(v: Option[A]): String = v.map(ac.to).getOrElse("") - } -} \ No newline at end of file +} diff --git a/src/main/scala/purecsv/unsafe/package.scala b/src/main/scala/purecsv/unsafe/package.scala index 01ef563..3536437 100644 --- a/src/main/scala/purecsv/unsafe/package.scala +++ b/src/main/scala/purecsv/unsafe/package.scala @@ -29,39 +29,41 @@ import scala.reflect.ClassTag package object unsafe { // String - implicit val boolc: StringConverter[Boolean] = purecsv.unsafe.converter.defaults.string.boolc - implicit val bytec: StringConverter[Byte] = purecsv.unsafe.converter.defaults.string.bytec - implicit val charc: StringConverter[Char] = purecsv.unsafe.converter.defaults.string.charc + implicit val boolc: StringConverter[Boolean] = purecsv.unsafe.converter.defaults.string.boolc + implicit val bytec: StringConverter[Byte] = purecsv.unsafe.converter.defaults.string.bytec + implicit val charc: StringConverter[Char] = purecsv.unsafe.converter.defaults.string.charc implicit val doublec: StringConverter[Double] = purecsv.unsafe.converter.defaults.string.doublec - implicit val floatc: StringConverter[Float] = purecsv.unsafe.converter.defaults.string.floatc - implicit val intc: StringConverter[Int] = purecsv.unsafe.converter.defaults.string.intc - implicit val longc: StringConverter[Long] = purecsv.unsafe.converter.defaults.string.longc - implicit val shortc: StringConverter[Short] = purecsv.unsafe.converter.defaults.string.shortc + implicit val floatc: StringConverter[Float] = purecsv.unsafe.converter.defaults.string.floatc + implicit val intc: StringConverter[Int] = purecsv.unsafe.converter.defaults.string.intc + implicit val longc: StringConverter[Long] = purecsv.unsafe.converter.defaults.string.longc + implicit val shortc: StringConverter[Short] = purecsv.unsafe.converter.defaults.string.shortc implicit val stringc: StringConverter[String] = purecsv.unsafe.converter.defaults.string.stringc - implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = purecsv.unsafe.converter.defaults.string.optionc - + implicit def optionc[A](implicit ac: StringConverter[A]): StringConverter[Option[A]] = + purecsv.unsafe.converter.defaults.string.optionc // Raw Fields implicit val deriveHNil: RawFieldsConverter[HNil] = purecsv.unsafe.converter.defaults.rawfields.deriveHNil - implicit def deriveHCons[V, T <: HList](implicit sc: StringConverter[V], - fto: RawFieldsConverter[T]) - : RawFieldsConverter[V :: T] = { + implicit def deriveHCons[V, T <: HList](implicit + sc: StringConverter[V], + fto: RawFieldsConverter[T] + ): RawFieldsConverter[V :: T] = { purecsv.unsafe.converter.defaults.rawfields.deriveHCons } - implicit def deriveClass[A, R](implicit gen: Generic.Aux[A, R], - conv: RawFieldsConverter[R]) - : RawFieldsConverter[A] = { + implicit def deriveClass[A, R](implicit + gen: Generic.Aux[A, R], + conv: RawFieldsConverter[R] + ): RawFieldsConverter[A] = { purecsv.unsafe.converter.defaults.rawfields.deriveClass } implicit class CSVRecord[A](a: A)(implicit rfc: RawFieldsConverter[A]) - extends purecsv.csviterable.CSVRecord[A, RawFieldsConverter[A]](a)(rfc) + extends purecsv.csviterable.CSVRecord[A, RawFieldsConverter[A]](a)(rfc) implicit class CSVIterable[A](iter: Iterable[A])(implicit rfc: RawFieldsConverter[A]) - extends purecsv.csviterable.CSVIterable[A, RawFieldsConverter[A]](iter)(rfc) + extends purecsv.csviterable.CSVIterable[A, RawFieldsConverter[A]](iter)(rfc) trait CSVReader[A] extends Serializable { @@ -70,15 +72,18 @@ package object unsafe { def readCSVFromReader(r: Reader, delimiter: Char = RecordSplitter.defaultFieldSeparator, trimming: Trimming = NoAction, - headers: Headers = ParseHeaders)(implicit classTag: ClassTag[A]): Iterator[A] = { - RecordSplitterImpl.getRecords(r, caseClassParams[A], delimiter, trimming = trimming, headers = headers) + headers: Headers = ParseHeaders + )(implicit classTag: ClassTag[A]): Iterator[A] = { + RecordSplitterImpl + .getRecords(r, caseClassParams[A], delimiter, trimming = trimming, headers = headers) .map(record => rfc.from(record.toSeq)) } def readCSVFromString(s: String, delimiter: Char = RecordSplitter.defaultFieldSeparator, trimming: Trimming = NoAction, - headers: Headers = ParseHeaders)(implicit classTag: ClassTag[A]): List[A] = { + headers: Headers = ParseHeaders + )(implicit classTag: ClassTag[A]): List[A] = { val r = new StringReader(s) try { readCSVFromReader(r, delimiter, trimming, headers).toList @@ -90,7 +95,8 @@ package object unsafe { def readCSVFromFile(f: File, delimiter: Char = RecordSplitter.defaultFieldSeparator, trimming: Trimming = NoAction, - headers: Headers = ParseHeaders)(implicit classTag: ClassTag[A]): List[A] = { + headers: Headers = ParseHeaders + )(implicit classTag: ClassTag[A]): List[A] = { val r = FileUtil.createReader(f) try { readCSVFromReader(r, delimiter, trimming, headers).toList @@ -102,7 +108,8 @@ package object unsafe { def readCSVFromFileName(fileName: String, delimiter: Char = RecordSplitter.defaultFieldSeparator, trimming: Trimming = NoAction, - headers: Headers = ParseHeaders)(implicit classTag: ClassTag[A]): List[A] = { + headers: Headers = ParseHeaders + )(implicit classTag: ClassTag[A]): List[A] = { readCSVFromFile(new File(fileName), delimiter, trimming, headers) } diff --git a/src/main/scala/purecsv/util/FileUtil.scala b/src/main/scala/purecsv/util/FileUtil.scala index 8069f96..ea5178b 100644 --- a/src/main/scala/purecsv/util/FileUtil.scala +++ b/src/main/scala/purecsv/util/FileUtil.scala @@ -4,7 +4,7 @@ import java.io._ import java.nio.charset.StandardCharsets object FileUtil { - def createReader(f: File): Reader = { - new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8)) - } + def createReader(f: File): Reader = { + new BufferedReader(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8)) + } } diff --git a/src/test/scala/purecsv/RecordSplitterSuite.scala b/src/test/scala/purecsv/RecordSplitterSuite.scala index 59105da..871ccb4 100644 --- a/src/test/scala/purecsv/RecordSplitterSuite.scala +++ b/src/test/scala/purecsv/RecordSplitterSuite.scala @@ -22,7 +22,6 @@ import purecsv.unsafe.RecordSplitterImpl import purecsv.config.Headers import purecsv.config.Headers.None - class RecordSplitterSuite extends AnyFunSuite with Matchers { test("RecordSplitterImpl works with no records") { val reader = new CharArrayReader("".toCharArray) @@ -38,25 +37,33 @@ class RecordSplitterSuite extends AnyFunSuite with Matchers { test("RecordSplitterImpl works with custom delimiter") { val reader = new CharArrayReader("foo|bar\nbar|foo".toCharArray) - RecordSplitterImpl.getRecords(reader, Seq.empty, '|', headers = None).toSeq should contain theSameElementsInOrderAs + RecordSplitterImpl + .getRecords(reader, Seq.empty, '|', headers = None) + .toSeq should contain theSameElementsInOrderAs Seq(Array("foo", "bar"), Array("bar", "foo")) } test("RecordSplitterImpl works with custom UTF8 delimiter") { val reader = new CharArrayReader("foo☃bar\nbar☃foo".toCharArray) - RecordSplitterImpl.getRecords(reader, Seq.empty, '☃', headers = None).toSeq should contain theSameElementsInOrderAs + RecordSplitterImpl + .getRecords(reader, Seq.empty, '☃', headers = None) + .toSeq should contain theSameElementsInOrderAs Seq(Array("foo", "bar"), Array("bar", "foo")) } test("RecordSplitterImpl ignores empty lines") { val reader = new CharArrayReader("foo☃bar\n\nbar☃foo\n\n".toCharArray) - RecordSplitterImpl.getRecords(reader, Seq.empty, '☃', headers = None).toSeq should contain theSameElementsInOrderAs + RecordSplitterImpl + .getRecords(reader, Seq.empty, '☃', headers = None) + .toSeq should contain theSameElementsInOrderAs Seq(Array("foo", "bar"), Array("bar", "foo")) } test("RecordSplitterImpl with header ignores empty lines") { val reader = new CharArrayReader("key☃value\nfoo☃bar\n\nbar☃foo\n\n".toCharArray) - RecordSplitterImpl.getRecords(reader, Seq("key", "value"), '☃', headers = Headers.ParseHeaders).toSeq should contain theSameElementsInOrderAs + RecordSplitterImpl + .getRecords(reader, Seq("key", "value"), '☃', headers = Headers.ParseHeaders) + .toSeq should contain theSameElementsInOrderAs Seq(Array("foo", "bar"), Array("bar", "foo")) } -} \ No newline at end of file +} diff --git a/src/test/scala/purecsv/csviterable/CSVRecordTest.scala b/src/test/scala/purecsv/csviterable/CSVRecordTest.scala index 1d2d6b7..baacf6b 100644 --- a/src/test/scala/purecsv/csviterable/CSVRecordTest.scala +++ b/src/test/scala/purecsv/csviterable/CSVRecordTest.scala @@ -28,12 +28,17 @@ class CSVRecordTest extends AnyFunSuite with Matchers { test("CSVRecord output should be parsable by purecsv") { implicit val rfc = RawFieldsConverter[Person] - val csvRecord = CSVRecord(person).toCSV() - CSVReader[Person].readCSVFromString(csvRecord, headers = Headers.None) should contain theSameElementsAs Seq(person) + val csvRecord = CSVRecord(person).toCSV() + CSVReader[Person].readCSVFromString(csvRecord, headers = Headers.None) should contain theSameElementsAs Seq( + person + ) } test("CSVRecord output should be parsable by purecsv (trimming applied)") { implicit val rfc = RawFieldsConverter[Person] - val csvRecord = """Jon,"Snow ""III"" of Winterfell """" - CSVReader[Person].readCSVFromString(csvRecord, trimming = TrimAll, headers = Headers.None) should contain theSameElementsAs Seq(person) + val csvRecord = """Jon,"Snow ""III"" of Winterfell """" + CSVReader[Person].readCSVFromString(csvRecord, + trimming = TrimAll, + headers = Headers.None + ) should contain theSameElementsAs Seq(person) } } diff --git a/src/test/scala/purecsv/package.scala b/src/test/scala/purecsv/package.scala index 0c1e1dd..657aa62 100644 --- a/src/test/scala/purecsv/package.scala +++ b/src/test/scala/purecsv/package.scala @@ -3,15 +3,18 @@ package purecsv import java.io._ package object util { + /** * Utility function to serialize an object and immediately deserialize it back. * - * @param obj an object of type T, should be Serializable - * @return the object of type T after being serialized and deserialized back + * @param obj + * an object of type T, should be Serializable + * @return + * the object of type T after being serialized and deserialized back */ def serializeAndDeserialize[T](obj: T): T = { val baos = new ByteArrayOutputStream() - val oos = new ObjectOutputStream(baos) + val oos = new ObjectOutputStream(baos) oos.writeObject(obj) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray)) diff --git a/src/test/scala/purecsv/safe/TryUtilSuite.scala b/src/test/scala/purecsv/safe/TryUtilSuite.scala index 0ae4b9c..2967ef5 100644 --- a/src/test/scala/purecsv/safe/TryUtilSuite.scala +++ b/src/test/scala/purecsv/safe/TryUtilSuite.scala @@ -21,8 +21,8 @@ import scala.util.{Failure, Success} class MyException(val s: String) extends RuntimeException(s) { override def equals(o: Any): Boolean = o match { - case e:MyException => s.equals(e.s) - case _ => false + case e: MyException => s.equals(e.s) + case _ => false } } @@ -32,12 +32,12 @@ class TryUtilSuite extends AnyFunSuite with Matchers { def failure(s: String): Failure[RuntimeException] = Failure(new MyException(s)) test("getSuccessesAndFailures partition an Iterator[Try[A]] into successes and failures") { - val startingSuccesses = Seq(Success(1),Success(2)) - val startingFailures = Seq(failure("3"),failure("4")) - val expectedSuccesses = Seq(1 -> 1, 2 -> 2) - val expectedFailures = Seq(3 -> new MyException("3"), 4 -> new MyException("4")) - val (resSuccesses,resFailures) = (startingSuccesses ++ startingFailures).getSuccessesAndFailures - resSuccesses should be (expectedSuccesses) - resFailures should be (expectedFailures) + val startingSuccesses = Seq(Success(1), Success(2)) + val startingFailures = Seq(failure("3"), failure("4")) + val expectedSuccesses = Seq(1 -> 1, 2 -> 2) + val expectedFailures = Seq(3 -> new MyException("3"), 4 -> new MyException("4")) + val (resSuccesses, resFailures) = (startingSuccesses ++ startingFailures).getSuccessesAndFailures + resSuccesses should be(expectedSuccesses) + resFailures should be(expectedFailures) } } diff --git a/src/test/scala/purecsv/safe/absentColumnsSuite.scala b/src/test/scala/purecsv/safe/absentColumnsSuite.scala index 92cc918..64b362d 100644 --- a/src/test/scala/purecsv/safe/absentColumnsSuite.scala +++ b/src/test/scala/purecsv/safe/absentColumnsSuite.scala @@ -16,9 +16,8 @@ class absentColumnsSuite extends AnyFunSuite with Matchers with TryValues { |42,joey |55,rachel""".stripMargin - readCsv(csv, NoAction) must contain only ( - Success(TestAccount(42, "joey", None)), - Success(TestAccount(55, "rachel", None))) + readCsv(csv, NoAction) must contain only (Success(TestAccount(42, "joey", None)), + Success(TestAccount(55, "rachel", None))) } test("should set some value when column is present") { @@ -27,10 +26,9 @@ class absentColumnsSuite extends AnyFunSuite with Matchers with TryValues { |55,rachel,green |66,chandler,bing""".stripMargin - readCsv(csv, NoAction) must contain only ( - Success(TestAccount(42, "joey", Some("tribiani"))), - Success(TestAccount(55, "rachel", Some("green"))), - Success(TestAccount(66, "chandler", Some("bing")))) + readCsv(csv, NoAction) must contain only (Success(TestAccount(42, "joey", Some("tribiani"))), + Success(TestAccount(55, "rachel", Some("green"))), + Success(TestAccount(66, "chandler", Some("bing")))) } test("should set empty string for the missing column") { @@ -38,9 +36,8 @@ class absentColumnsSuite extends AnyFunSuite with Matchers with TryValues { |42,tribiani |55,green""".stripMargin - readCsv(csv, NoAction) must contain only ( - Success(TestAccount(42, "", Some("tribiani"))), - Success(TestAccount(55, "", Some("green")))) + readCsv(csv, NoAction) must contain only (Success(TestAccount(42, "", Some("tribiani"))), + Success(TestAccount(55, "", Some("green")))) } test("should fail when column is missing and skipping header is enabled") { diff --git a/src/test/scala/purecsv/safe/converter/ConverterSuite.scala b/src/test/scala/purecsv/safe/converter/ConverterSuite.scala index c906e56..9563b36 100644 --- a/src/test/scala/purecsv/safe/converter/ConverterSuite.scala +++ b/src/test/scala/purecsv/safe/converter/ConverterSuite.scala @@ -30,42 +30,42 @@ case class Event(ts: Long, msg: String, user: Option[Int]) class ConverterSuite extends AnyFunSuite with Matchers with TryValues { test("conversion String -> Try[Boolean] works") { - StringConverter[Boolean].tryFrom("false") should be (Success(false)) - StringConverter[Boolean].tryFrom("1") should be (Success(true)) - StringConverter[Boolean].tryFrom("TRUE") should be (Success(true)) + StringConverter[Boolean].tryFrom("false") should be(Success(false)) + StringConverter[Boolean].tryFrom("1") should be(Success(true)) + StringConverter[Boolean].tryFrom("TRUE") should be(Success(true)) } test("conversion String <-> Try[UUID] works") { val uuid = UUID.randomUUID() - StringConverter[UUID].tryFrom(uuid.toString) should be (Success(uuid)) - StringConverter[UUID].tryFrom(uuid.toString.toLowerCase) should be (Success(uuid)) - StringConverter[UUID].tryFrom(uuid.toString.toUpperCase) should be (Success(uuid)) + StringConverter[UUID].tryFrom(uuid.toString) should be(Success(uuid)) + StringConverter[UUID].tryFrom(uuid.toString.toLowerCase) should be(Success(uuid)) + StringConverter[UUID].tryFrom(uuid.toString.toUpperCase) should be(Success(uuid)) } test("conversion string -> Try[Option[Int]] works") { - StringConverter[Option[Int]].tryFrom("") should be (Success(None)) - StringConverter[Option[Int]].tryFrom("1") should be (Success(Some(1))) + StringConverter[Option[Int]].tryFrom("") should be(Success(None)) + StringConverter[Option[Int]].tryFrom("1") should be(Success(Some(1))) } test("conversion String -> HNil works") { - RawFieldsConverter[HNil].tryFrom(Seq.empty) should be (Success(HNil)) + RawFieldsConverter[HNil].tryFrom(Seq.empty) should be(Success(HNil)) } test("conversion String -> HList works") { val conv = RawFieldsConverter[String :: Int :: HNil] - conv.tryFrom(Seq("foo","2")) should be (Success("foo" :: 2 :: HNil)) + conv.tryFrom(Seq("foo", "2")) should be(Success("foo" :: 2 :: HNil)) } test("conversion String -> case class works") { val conv = RawFieldsConverter[Event] - conv.tryFrom(Seq("2","barfoo","")) should be (Success(Event(2,"barfoo",None))) - conv.tryFrom(Seq("2","barfoo","1")) should be (Success(Event(2,"barfoo",Some(1)))) + conv.tryFrom(Seq("2", "barfoo", "")) should be(Success(Event(2, "barfoo", None))) + conv.tryFrom(Seq("2", "barfoo", "1")) should be(Success(Event(2, "barfoo", Some(1)))) } class Event2(val ts: Long, var msg: String) { override def equals(o: Any): Boolean = o match { - case other:Event2 => (this.ts == other.ts && this.msg == other.msg) - case _ => false + case other: Event2 => (this.ts == other.ts && this.msg == other.msg) + case _ => false } override def toString: String = s"Event($ts, $msg)" } @@ -81,25 +81,25 @@ class ConverterSuite extends AnyFunSuite with Matchers with TryValues { test("conversion String -> class with custom Generic works") { val conv = RawFieldsConverter[Event2] - conv.tryFrom(Seq("2","bar")) should be (Success(new Event2(2,"bar"))) + conv.tryFrom(Seq("2", "bar")) should be(Success(new Event2(2, "bar"))) // Strings are not quoted - val event = new Event2(1,"foo") - conv.tryFrom(conv.to(event)) should be (Success(event)) + val event = new Event2(1, "foo") + conv.tryFrom(conv.to(event)) should be(Success(event)) } test("conversion String -> class with custom Generic works - with quotes") { val conv = RawFieldsConverter[Event2] // Strings are quoted - val event = new Event2(1,"""with "quotes"""") - conv.to(event) should be (Seq("1", "\"with \"\"quotes\"\"\"")) + val event = new Event2(1, """with "quotes"""") + conv.to(event) should be(Seq("1", "\"with \"\"quotes\"\"\"")) } test("serializing a RawFieldsConverter should work") { - val conv = RawFieldsConverter[Event] + val conv = RawFieldsConverter[Event] val convDeserialized = serializeAndDeserialize(conv) - convDeserialized.tryFrom(Seq("2","barfoo","")) should be (Success(Event(2,"barfoo",None))) - convDeserialized.tryFrom(Seq("2","barfoo","1")) should be (Success(Event(2,"barfoo",Some(1)))) + convDeserialized.tryFrom(Seq("2", "barfoo", "")) should be(Success(Event(2, "barfoo", None))) + convDeserialized.tryFrom(Seq("2", "barfoo", "1")) should be(Success(Event(2, "barfoo", Some(1)))) } -} \ No newline at end of file +} diff --git a/src/test/scala/purecsv/safe/customerDelimiterSafeSuite.scala b/src/test/scala/purecsv/safe/customerDelimiterSafeSuite.scala index 8b3bc69..4e5cf42 100644 --- a/src/test/scala/purecsv/safe/customerDelimiterSafeSuite.scala +++ b/src/test/scala/purecsv/safe/customerDelimiterSafeSuite.scala @@ -28,8 +28,8 @@ import scala.util.Success class customerDelimiterSafeSuite extends AnyFunSuite with Matchers { case class Event(ts: Long, msg: String, user: Option[Int]) - private val events = Seq(Event(1,"foo",None),Event(2,"bar",Some(1))) - private val rawEvents = Seq("1|foo|","2|bar|1") + private val events = Seq(Event(1, "foo", None), Event(2, "bar", Some(1))) + private val rawEvents = Seq("1|foo|", "2|bar|1") test("Converting an iterable of events to CSV lines works") { events.toCSVLines("|").toSeq should contain theSameElementsInOrderAs rawEvents @@ -37,20 +37,26 @@ class customerDelimiterSafeSuite extends AnyFunSuite with Matchers { test("Reading events from a String reader works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - CSVReader[Event].readCSVFromReader(reader, '|', NoAction, Headers.None).toSeq should contain theSameElementsInOrderAs events.map(Success(_)) + CSVReader[Event] + .readCSVFromReader(reader, '|', NoAction, Headers.None) + .toSeq should contain theSameElementsInOrderAs events.map(Success(_)) } test("Reading events and get successes and failures works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - val (successes,failures) = CSVReader[Event].readCSVFromReader(reader, '|', NoAction, Headers.None).getSuccessesAndFailures + val (successes, failures) = + CSVReader[Event].readCSVFromReader(reader, '|', NoAction, Headers.None).getSuccessesAndFailures val expectedSuccesses = Seq(1 -> events(0), 2 -> events(1)) successes should contain theSameElementsInOrderAs expectedSuccesses - failures should be (Seq.empty[Event]) + failures should be(Seq.empty[Event]) } test("Can read a file written with writeCSVToFile") { - val file = Files.createTempFile("casecsv",".csv").toFile + val file = Files.createTempFile("casecsv", ".csv").toFile events.writeCSVToFile(file, "☃") - CSVReader[Event].readCSVFromFile(file, '☃', headers = Headers.None) should contain theSameElementsInOrderAs events.map(Success(_)) + CSVReader[Event].readCSVFromFile(file, + '☃', + headers = Headers.None + ) should contain theSameElementsInOrderAs events.map(Success(_)) } } diff --git a/src/test/scala/purecsv/safe/headersSuite.scala b/src/test/scala/purecsv/safe/headersSuite.scala index 7c86609..10025ab 100644 --- a/src/test/scala/purecsv/safe/headersSuite.scala +++ b/src/test/scala/purecsv/safe/headersSuite.scala @@ -26,7 +26,9 @@ class headersSuite extends AnyFunSuite with Matchers { val csv = """id,name,email |42,chandler,chandler@bing.com""".stripMargin - readCsv(csv, Headers.ParseHeaders) must contain only Success(TestUser(42, "chandler", Some("chandler@bing.com"))) + readCsv(csv, Headers.ParseHeaders) must contain only Success( + TestUser(42, "chandler", Some("chandler@bing.com")) + ) } test("should parse csv entry with headers even if fields order doesn't match class's fields order") { @@ -48,7 +50,8 @@ class headersSuite extends AnyFunSuite with Matchers { |ross@geller.com,34,42,ross""".stripMargin readCsv(csv, Headers.ParseHeaders, Map("email address" -> "email")) must contain only Success( - TestUser(42, "ross", Some("ross@geller.com"))) + TestUser(42, "ross", Some("ross@geller.com")) + ) } private def readCsv(csv: String, headers: Headers, headerMapping: Map[String, String] = Map.empty) = diff --git a/src/test/scala/purecsv/safe/safeSuite.scala b/src/test/scala/purecsv/safe/safeSuite.scala index ea2a7ca..96210c4 100644 --- a/src/test/scala/purecsv/safe/safeSuite.scala +++ b/src/test/scala/purecsv/safe/safeSuite.scala @@ -31,7 +31,7 @@ case class Event(ts: Long, msg: String, user: Option[Int]) class safeSuite extends AnyFunSuite with Matchers { - val events = Seq(Event(1,"foo",None),Event(2,"bar",Some(1))) + val events = Seq(Event(1, "foo", None), Event(2, "bar", Some(1))) val rawEvents = Seq("1,foo,", "2,bar,1") test("Converting an iterable of events to CSV lines works") { @@ -40,33 +40,35 @@ class safeSuite extends AnyFunSuite with Matchers { test("Reading events from a String reader works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - CSVReader[Event].readCSVFromReader(reader, ',', NoAction, Headers.None).toSeq should contain theSameElementsInOrderAs events.map(Success(_)) + CSVReader[Event] + .readCSVFromReader(reader, ',', NoAction, Headers.None) + .toSeq should contain theSameElementsInOrderAs events.map(Success(_)) } test("Reading events and get successes and failures works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - val (successes,failures) = CSVReader[Event].readCSVFromReader(reader, ',', NoAction, Headers.None).getSuccessesAndFailures + val (successes, failures) = + CSVReader[Event].readCSVFromReader(reader, ',', NoAction, Headers.None).getSuccessesAndFailures val expectedSuccesses = Seq(1 -> events(0), 2 -> events(1)) successes should contain theSameElementsInOrderAs expectedSuccesses - failures should be (Seq.empty[Event]) + failures should be(Seq.empty[Event]) } test("Can read a file written with writeCSVToFile") { - val file = Files.createTempFile("casecsv",".csv").toFile + val file = Files.createTempFile("casecsv", ".csv").toFile events.writeCSVToFile(file) - CSVReader[Event].readCSVFromFile(file, headers = Headers.None) should contain theSameElementsInOrderAs events.map(Success(_)) + CSVReader[Event].readCSVFromFile(file, headers = Headers.None) should contain theSameElementsInOrderAs events + .map(Success(_)) } test("serializing a CSVReader should work") { - val csvReader = CSVReader[Event] + val csvReader = CSVReader[Event] val csvReaderDeserialized = serializeAndDeserialize(csvReader) val result = csvReaderDeserialized.readCSVFromString("123|bar|\n456|foo|3", '|', NoAction, Headers.None) - result.length should be (2) - result should be (List( - Success(Event(123, "bar", None)), - Success(Event(456, "foo", Some(3))))) + result.length should be(2) + result should be(List(Success(Event(123, "bar", None)), Success(Event(456, "foo", Some(3))))) } } diff --git a/src/test/scala/purecsv/unsafe/converter/ConverterSuite.scala b/src/test/scala/purecsv/unsafe/converter/ConverterSuite.scala index 09cf887..292dd3a 100644 --- a/src/test/scala/purecsv/unsafe/converter/ConverterSuite.scala +++ b/src/test/scala/purecsv/unsafe/converter/ConverterSuite.scala @@ -28,41 +28,41 @@ case class Event(ts: Long, msg: String) class ConverterSuite extends AnyFunSuite with Matchers { test("conversion String <-> Boolean works") { - StringConverter[Boolean].to(true) should be ("true") - StringConverter[Boolean].from("false") should be (false) - StringConverter[Boolean].from("1") should be (true) - StringConverter[Boolean].from("TRUE") should be (true) + StringConverter[Boolean].to(true) should be("true") + StringConverter[Boolean].from("false") should be(false) + StringConverter[Boolean].from("1") should be(true) + StringConverter[Boolean].from("TRUE") should be(true) } test("conversion String <-> UUID works") { val uuid = UUID.randomUUID() - StringConverter[UUID].to(uuid) should be (uuid.toString) - StringConverter[UUID].from(uuid.toString) should be (uuid) - StringConverter[UUID].from(uuid.toString.toLowerCase) should be (uuid) - StringConverter[UUID].from(uuid.toString.toUpperCase) should be (uuid) + StringConverter[UUID].to(uuid) should be(uuid.toString) + StringConverter[UUID].from(uuid.toString) should be(uuid) + StringConverter[UUID].from(uuid.toString.toLowerCase) should be(uuid) + StringConverter[UUID].from(uuid.toString.toUpperCase) should be(uuid) } test("conversion HNil <-> String works") { RawFieldsConverter[HNil].to(HNil) should contain theSameElementsInOrderAs (Seq.empty) - RawFieldsConverter[HNil].from(Seq.empty) should be (HNil) + RawFieldsConverter[HNil].from(Seq.empty) should be(HNil) } test("conversion HList <-> String works") { val conv = RawFieldsConverter[String :: Int :: HNil] - conv.to("test" :: 1 :: HNil) should contain theSameElementsInOrderAs (Seq("test","1")) - conv.from(Seq("foo","2")) should be ("foo" :: 2 :: HNil) + conv.to("test" :: 1 :: HNil) should contain theSameElementsInOrderAs (Seq("test", "1")) + conv.from(Seq("foo", "2")) should be("foo" :: 2 :: HNil) } test("conversion case class <-> String works") { val conv = RawFieldsConverter[Event] - conv.to(Event(1,"foobar")) should contain theSameElementsInOrderAs(Seq("1","foobar")) - conv.from(Seq("2","barfoo")) should be (Event(2,"barfoo")) + conv.to(Event(1, "foobar")) should contain theSameElementsInOrderAs (Seq("1", "foobar")) + conv.from(Seq("2", "barfoo")) should be(Event(2, "barfoo")) } class Event2(val ts: Long, var msg: String) { override def equals(o: Any): Boolean = o match { - case other:Event2 => (this.ts == other.ts && this.msg == other.msg) - case _ => false + case other: Event2 => (this.ts == other.ts && this.msg == other.msg) + case _ => false } override def toString: String = s"Event($ts, $msg)" } @@ -78,27 +78,27 @@ class ConverterSuite extends AnyFunSuite with Matchers { test("conversion class with custom Generic <-> String works") { val conv = RawFieldsConverter[Event2] - conv.to(new Event2(1,"foo")) should contain theSameElementsInOrderAs(Seq("1","foo")) - conv.from(Seq("2","bar")) should be (new Event2(2,"bar")) + conv.to(new Event2(1, "foo")) should contain theSameElementsInOrderAs (Seq("1", "foo")) + conv.from(Seq("2", "bar")) should be(new Event2(2, "bar")) // Strings are quoted - val event = new Event2(1,"foo") - conv.from(conv.to(event)) should be (event) + val event = new Event2(1, "foo") + conv.from(conv.to(event)) should be(event) } test("conversion String -> class with custom Generic works - with quotes") { val conv = RawFieldsConverter[Event2] // Strings are quoted - val event = new Event2(1,"""with "quotes"""") - conv.to(event) should be (Seq("1", "\"with \"\"quotes\"\"\"")) + val event = new Event2(1, """with "quotes"""") + conv.to(event) should be(Seq("1", "\"with \"\"quotes\"\"\"")) } test("serializing a RawFieldsConverter should work") { - val conv = RawFieldsConverter[Event] + val conv = RawFieldsConverter[Event] val convDeserialized = serializeAndDeserialize(conv) - convDeserialized.to(Event(1,"foobar")) should contain theSameElementsInOrderAs(Seq("1","foobar")) - convDeserialized.from(Seq("2","barfoo")) should be (Event(2,"barfoo")) + convDeserialized.to(Event(1, "foobar")) should contain theSameElementsInOrderAs (Seq("1", "foobar")) + convDeserialized.from(Seq("2", "barfoo")) should be(Event(2, "barfoo")) } test("quoteTextIfNecessary should work") { diff --git a/src/test/scala/purecsv/unsafe/customerDelimiterUnsafeSuite.scala b/src/test/scala/purecsv/unsafe/customerDelimiterUnsafeSuite.scala index a633ba2..80a790b 100644 --- a/src/test/scala/purecsv/unsafe/customerDelimiterUnsafeSuite.scala +++ b/src/test/scala/purecsv/unsafe/customerDelimiterUnsafeSuite.scala @@ -23,12 +23,11 @@ import purecsv.unsafe._ import purecsv.config.Headers import purecsv.config.Trimming.NoAction - class customerDelimiterUnsafeSuite extends AnyFunSuite with Matchers { case class Event(ts: Long, msg: String, user: Option[Int]) - private val events = Seq(Event(1,"foo",None),Event(2,"bar",Some(1))) - private val rawEvents = Seq("1|foo|","2|bar|1") + private val events = Seq(Event(1, "foo", None), Event(2, "bar", Some(1))) + private val rawEvents = Seq("1|foo|", "2|bar|1") test("Converting an iterable of events to CSV lines works") { events.toCSVLines("|").toSeq should contain theSameElementsInOrderAs rawEvents @@ -36,13 +35,16 @@ class customerDelimiterUnsafeSuite extends AnyFunSuite with Matchers { test("Reading events from a String reader works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - CSVReader[Event].readCSVFromReader(reader, '|', NoAction, Headers.None).toSeq should be (events) + CSVReader[Event].readCSVFromReader(reader, '|', NoAction, Headers.None).toSeq should be(events) } test("Can read a file written with writeCSVToFile") { - val file = Files.createTempFile("casecsv",".csv").toFile + val file = Files.createTempFile("casecsv", ".csv").toFile file.deleteOnExit() events.writeCSVToFile(file, "☃") - CSVReader[Event].readCSVFromFile(file, '☃', headers = Headers.None) should contain theSameElementsInOrderAs events + CSVReader[Event].readCSVFromFile(file, + '☃', + headers = Headers.None + ) should contain theSameElementsInOrderAs events } -} \ No newline at end of file +} diff --git a/src/test/scala/purecsv/unsafe/unsafeSuite.scala b/src/test/scala/purecsv/unsafe/unsafeSuite.scala index 494f353..1f8e42f 100644 --- a/src/test/scala/purecsv/unsafe/unsafeSuite.scala +++ b/src/test/scala/purecsv/unsafe/unsafeSuite.scala @@ -26,7 +26,7 @@ case class Event(ts: Long, msg: String, user: Option[Int]) class unsafeSuite extends AnyFunSuite with Matchers { - private val events = Seq(Event(1,"foo",None),Event(2,"bar",Some(1))) + private val events = Seq(Event(1, "foo", None), Event(2, "bar", Some(1))) private val rawEvents = Seq("1,foo,", "2,bar,1") test("Converting an iterable of events to CSV lines works") { @@ -35,25 +35,23 @@ class unsafeSuite extends AnyFunSuite with Matchers { test("Reading events from a String reader works") { val reader = new CharArrayReader(rawEvents.mkString(System.lineSeparator()).toCharArray) - CSVReader[Event].readCSVFromReader(reader, headers = Headers.None).toSeq should be (events) + CSVReader[Event].readCSVFromReader(reader, headers = Headers.None).toSeq should be(events) } test("Can read a file written with writeCSVToFile") { - val file = Files.createTempFile("casecsv",".csv").toFile + val file = Files.createTempFile("casecsv", ".csv").toFile file.deleteOnExit() events.writeCSVToFile(file) CSVReader[Event].readCSVFromFile(file, headers = Headers.None) should contain theSameElementsInOrderAs events } test("serializing a CSVReader should work") { - val csvReader = CSVReader[Event] + val csvReader = CSVReader[Event] val csvReaderDeserialized = serializeAndDeserialize(csvReader) val result = csvReaderDeserialized.readCSVFromString("123|bar|\n456|foo|3", '|', headers = Headers.None) - result.length should be (2) - result should be (List( - Event(123, "bar", None), - Event(456, "foo", Some(3)))) + result.length should be(2) + result should be(List(Event(123, "bar", None), Event(456, "foo", Some(3)))) } }