Skip to content

Commit

Permalink
Scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfredo Torre committed Dec 14, 2023
1 parent 9646c85 commit fcd5198
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 293 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/purecsv/config/Headers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/main/scala/purecsv/config/Trimming.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ object Trimming {
object TrimAll extends Trimming {
override def trim(s: String): String = s.trim
}
}
}
34 changes: 22 additions & 12 deletions src/main/scala/purecsv/csviterable/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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] =
Expand All @@ -48,17 +49,23 @@ 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)))
toCSVLines(sep).foreach(writer.println)
}

/** @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)
Expand All @@ -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)
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/scala/purecsv/safe/converter/Converter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import shapeless.{::, Generic, HList, HNil}

import scala.util.{Failure, Success, Try}


package object rawfields {

import purecsv.safe.converter.RawFieldsConverter
Expand All @@ -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))
}
}
36 changes: 18 additions & 18 deletions src/main/scala/purecsv/safe/converter/defaults/string/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
}
}
68 changes: 37 additions & 31 deletions src/main/scala/purecsv/safe/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]] =
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/scala/purecsv/safe/tryutil/TryUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Loading

0 comments on commit fcd5198

Please sign in to comment.