Last active
July 26, 2016 16:34
-
-
Save umurgdk/9d5c1c0ee8deda876c94735548c7b257 to your computer and use it in GitHub Desktop.
CSV Serializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| } | |
| } | |
| } |
Author
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
Uh oh!
There was an error while loading. Please reload this page.