Skip to content

Commit

Permalink
csv genrator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Chimirev committed Aug 13, 2019
1 parent b646e53 commit 40403a0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
10 changes: 8 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ lazy val lib = project

lazy val generator = project
.in(file("generator"))
.dependsOn(scalaGenerator, rubyGenerator, javaGenerator, goGenerator, androidGenerator, kotlinGenerator, javaAwsLambdaPojos, postmanGenerator)
.aggregate(scalaGenerator, rubyGenerator, javaGenerator, goGenerator, androidGenerator, kotlinGenerator, javaAwsLambdaPojos, postmanGenerator)
.dependsOn(scalaGenerator, rubyGenerator, javaGenerator, goGenerator, androidGenerator, kotlinGenerator, javaAwsLambdaPojos, postmanGenerator, csvGenerator)
.aggregate(scalaGenerator, rubyGenerator, javaGenerator, goGenerator, androidGenerator, kotlinGenerator, javaAwsLambdaPojos, postmanGenerator, csvGenerator)
.enablePlugins(PlayScala)
.settings(commonSettings: _*)
.settings(
Expand Down Expand Up @@ -124,6 +124,12 @@ lazy val kotlinGenerator = project
)
.settings(Seq(ScoverageKeys.coverageMinimum := 92.49, ScoverageKeys.coverageFailOnMinimum := true))

lazy val csvGenerator = project
.in(file("csv-generator"))
.dependsOn(lib, lib % "test->test")
.settings(commonSettings: _*)
.settings(Seq(ScoverageKeys.coverageMinimum := 82.00))

lazy val postmanGenerator = project
.in(file("postman-generator"))
.dependsOn(lib, lib % "test->test")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package models.generator.csv

import io.apibuilder.generator.v0.models.{File, InvocationForm}
import io.apibuilder.spec.v0.models.{Operation, Resource, Service}
import lib.generator.CodeGenerator

class CsvGenerator extends CodeGenerator {

final val SEPARATOR = ","
final val NEWLINE = "\n"
final val EMPTY = ""

override def invoke(form: InvocationForm): Either[Seq[String], Seq[File]] = {
Right(generateSourceFiles(form.service))
}

def generateSourceFiles(service: Service): Seq[File] = {
Seq(generateResourcesFile(service.resources))
}

def generateResourcesFile(resources: Seq[Resource]): File = {

val HEADER = s"method${SEPARATOR}path${SEPARATOR}description${NEWLINE}"
val RESOURCES_FILE_NAME = "resources.csv"

val lines: Seq[Seq[String]] = for {
resource <- resources
operation <- resource.operations
} yield makeLine(resource, operation)

File(
name = RESOURCES_FILE_NAME,
contents = HEADER + lines.map(_.mkString(SEPARATOR)).mkString(NEWLINE)
)
}

def makeLine(resource: Resource, operation: Operation): Seq[String] = {

val method = wrapInQuotes(operation.method.toString)
val path = wrapInQuotes(resource.path.getOrElse(EMPTY) + operation.path)
val description = wrapInQuotes(operation.description.getOrElse(EMPTY))

Seq(method, path, description)
}

def wrapInQuotes(input: String): String = {
if (input.contains(SEPARATOR))
"\"" + input + "\""
else
input
}
}

object CsvGenerator extends CsvGenerator
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package models.generator.csv

import io.apibuilder.generator.v0.models.File
import io.apibuilder.spec.v0.models._
import org.scalatest.{FunSpec, Matchers}

class CSvGeneratorTest extends FunSpec with Matchers {

val generator = new CsvGenerator

val operation = Operation(
method = Method.Get,
path = "/wheels",
description = Some("get wheels")
)

val resource = Resource(
`type` = "car",
plural = "cars",
path = Some("/cars"),
description = Some("cars description"),
operations = Seq(operation)
)

describe("wrapQuotes") {
it("does ot wrap if not needed") {
assert(generator.wrapInQuotes("this does not have comma") === "this does not have comma")
}
it("wraps in quotes if needed") {
assert(generator.wrapInQuotes("this does, indeed, have a comma") === "\"this does, indeed, have a comma\"")
}
}

describe("makeLine") {
it("generates regular line") {
assert(generator.makeLine(resource, operation) === Seq("GET", "/cars/wheels", "get wheels"))
}
it("generates line with comma in a field") {
assert(generator.makeLine(resource, operation.copy(description = Some("get wheels, and don't forget lug nuts"))) === Seq("GET", "/cars/wheels", "\"get wheels, and don't forget lug nuts\""))
}
}

describe("generateResourcesFile") {
it("generates the file") {
assert(generator.generateResourcesFile(Seq(resource)) === File(
name = "resources.csv",
contents = "method,path,description\nGET,/cars/wheels,get wheels")
)
}
}

}
10 changes: 10 additions & 0 deletions generator/app/controllers/Generators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ object Generators {
status = lib.generator.Status.InDevelopment,
codeGenerator = Some(models.generator.kotlin.KotlinRxClasses)
),
CodeGenTarget(
metaData = Generator(
key = "csv_client",
name = "CSV Generator",
description = Some("Information about API in useful CSV format, for example list of endpoints"),
language = Some("test/csv")
),
status = lib.generator.Status.InDevelopment,
codeGenerator = Some(models.generator.csv.CsvGenerator)
),
CodeGenTarget(
metaData = Generator(
key = "swagger_json",
Expand Down

0 comments on commit 40403a0

Please sign in to comment.