Skip to content

Instantly share code, notes, and snippets.

@umurgdk
Last active July 26, 2016 16:34
Show Gist options
  • Select an option

  • Save umurgdk/9d5c1c0ee8deda876c94735548c7b257 to your computer and use it in GitHub Desktop.

Select an option

Save umurgdk/9d5c1c0ee8deda876c94735548c7b257 to your computer and use it in GitHub Desktop.
CSV Serializer
package com.socrata.util
import java.io.StringWriter
import com.github.tototoshi.csv.CSVWriter
trait CSVSerializable[T] {
def getListOfValues(value: T): List[String]
}
object CSVSerializer {
def serialize[T: CSVSerializable](value: T): String = {
val stringWriter = new StringWriter
val writer = CSVWriter.open(stringWriter)
val fields = implicitly[CSVSerializable[T]].getListOfValues(value)
writer.writeRow(fields)
writer.close()
stringWriter.toString
}
def serializeAll[T: CSVSerializable](values: Traversable[T]): String = {
val stringWriter = new StringWriter
val writer = CSVWriter.open(stringWriter)
val serializable = implicitly[CSVSerializable[T]]
values.foreach(x => writer.writeRow(serializable.getListOfValues(x)))
writer.close()
stringWriter.toString
}
}
object DeepGoal {
implicit val deepGoalCodec = AutomaticJsonCodecBuilder[DeepGoal]
def apply(goal:GoalWithId, prevailingMeasure:Option[MeasureWithId]) = {
new DeepGoal(goal.id.id,
goal.model.status,
goal.model.name,
goal.model.agencies,
goal.model.relatedDatasets,
goal.model.isPublic,
goal.model.metadata,
prevailingMeasure,
goal.model.relatedMeasures,
goal.model.baseDashboard,
goal.model.updatedAt,
goal.model.createdAt,
goal.model.version,
goal.model.createdBy)
}
implicit object CSVSerializableDeepGoal extends CSVSerializable[DeepGoal] {
override def getListOfValues(value: DeepGoal): List[String] = {
// TODO: Add required fields
val fields = List(value.status, value.name, value.createdAt, value.updatedAt)
val optionalToString = (x: Option[Any]) => x.map(_.toString).orNull
fields
.map(optionalToString)
.filterNot(_.equals(null))
}
}
}
@msimav
Copy link

msimav commented Jul 26, 2016

object CSVSerializable {
  def apply[T](contentF: T => List[String]): CSVSerializable[T] = new CSVSerializable[T] {
    def getListOfValues(value: DeepGoal): List[String] = contentF(value)
  }
}

implicit val csvDeepGoal = CSVSerialize[DeepGoal] { value =>
  List(value.status, value.name, value.createdAt, value.updatedAt).map(_.fold("")(_.toString))
}

@umurgdk
Copy link
Author

umurgdk commented Jul 26, 2016

final case class CSVSerialize[T](contentF: T => List[String], geaderF: T => Option[List[String]]) extends CSVSerializable[T] {
  def getListOfValues(value: T): List[String] = contentF(value)
  def getHeader(value: T): List[String] = headerF(value)
}

implicit val csvDeepGoal = CSVSerialize[DeepGoal] { 
  value =>  List(value.status, value.name, value.createdAt, value.updatedAt).map(_.fold("")(_.toString)),
  value => List("hebe", "hube")
}

@msimav
Copy link

msimav commented Jul 26, 2016

object CSVSerializable {
  def apply[T](headers: Option[List[String]])(contentF: T => List[String]): CSVSerializable[T] = new CSVSerializable[T] {
    def getListOfValues(value: DeepGoal): List[String] = contentF(value)
  }
}

implicit val csvDeepGoal = CSVSerialize[DeepGoal](Some(List"status", "name", "createdAt", "updatedAt"))) { value =>
  List(value.status, value.name, value.createdAt, value.updatedAt).map(_.fold("")(_.toString))
}

@msimav
Copy link

msimav commented Jul 26, 2016

object CSVSerializable {
  def withHeaders[T](headers: String*)(contentF: T => List[String]): CSVSerializable[T] = new CSVSerializable[T] {
    def getListOfValues(value: DeepGoal): List[String] = contentF(value)
    val getHeaders: Option[List[String]] = Some(headers.toList)
  }
}

implicit val csvDeepGoal = CSVSerialize.withHeaders[DeepGoal]("status", "name", "createdAt", "updatedAt") { value =>
  List(value.status, value.name, value.createdAt, value.updatedAt).map(_.fold("")(_.toString))
}

@msimav
Copy link

msimav commented Jul 26, 2016

object CSVSerializable {
  def withoutHeaders[T](contentF: T => List[String]): CSVSerializable[T] = new CSVSerializable[T] {
      def getListOfValues(value: DeepGoal): List[String] = contentF(value)
      val getHeaders: Option[List[String]] = None
    }

  def withHeaders[T](headers: String*)(contentF: T => List[String]): CSVSerializable[T] = new CSVSerializable[T] {
    def getListOfValues(value: DeepGoal): List[String] = contentF(value)
    val getHeaders: Option[List[String]] = Some(headers.toList)
  }
}

implicit val csvDeepGoal = CSVSerialize.withHeaders[DeepGoal]("status", "name", "createdAt", "updatedAt") { value =>
  List(value.status, value.name, value.createdAt, value.updatedAt).map(_.fold("")(_.toString))
}

implicit val csvListString = CSVSerialize.withoutHeaders[List[String](identity)

@umurgdk
Copy link
Author

umurgdk commented Jul 26, 2016

def list(format: String)()(requestEnv:Triple[HttpServletRequest,Domain,AuthenticatedUser]) = {
      val (request, domain, user) = requestEnv

      val reader = self.GoalReader(domain)

      val goals = Array(FourByFour("iryy-8ukh"), FourByFour("ua8x-w82w"))
        .map(reader.getDeeply)
        .flatMap(_.right.getOrElse(null))
        .filterNot(_.equals(null))

      val content = format match {
        case "json" => renderJson(goals)
        case "csv"  => CSVSerializer.serializeAll(goals)
      }

      getSuccess(content, None)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment