-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mike Chimirev
committed
Aug 13, 2019
1 parent
b646e53
commit 40403a0
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
54 changes: 54 additions & 0 deletions
54
csv-generator/src/main/scala/models/generator/csv/CsvGenerator.scala
This file contains 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
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 |
52 changes: 52 additions & 0 deletions
52
csv-generator/src/test/scala/models/generator/csv/CSvGeneratorTest.scala
This file contains 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
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") | ||
) | ||
} | ||
} | ||
|
||
} |
This file contains 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