-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
9 changed files
with
211 additions
and
12 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
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
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
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,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration> | ||
<Loggers> | ||
<Logger name="io.buildo.tapiro" level="error" /> | ||
</Loggers> | ||
</Configuration> |
58 changes: 58 additions & 0 deletions
58
tapiro/core/src/test/scala/io/buildo/tapiro/FileLayout.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,58 @@ | ||
package io.buildo.tapiro | ||
|
||
import java.nio.charset.Charset | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
import java.nio.file.StandardOpenOption | ||
import scala.meta.io.AbsolutePath | ||
|
||
//TODO(gabro): add Metals notice | ||
object FileLayout { | ||
|
||
def mapFromString(layout: String): Map[String, String] = { | ||
if (!layout.trim.isEmpty) { | ||
val lines = layout.replaceAllLiterally("\r\n", "\n") | ||
lines | ||
.split("(?=\n/[^/])") | ||
.map { row => | ||
row.stripPrefix("\n").split("\n", 2).toList match { | ||
case path :: contents :: Nil => | ||
path.stripPrefix("/") -> contents | ||
case els => | ||
throw new IllegalArgumentException( | ||
s"Unable to split argument info path/contents! \n$els", | ||
) | ||
} | ||
} | ||
.toMap | ||
} else { | ||
Map.empty | ||
} | ||
} | ||
|
||
def fromString( | ||
layout: String, | ||
root: AbsolutePath = AbsolutePath(Files.createTempDirectory("tapiro")), | ||
charset: Charset = StandardCharsets.UTF_8, | ||
): AbsolutePath = { | ||
if (!layout.trim.isEmpty) { | ||
mapFromString(layout).foreach { | ||
case (path, contents) => | ||
val file = | ||
path.split("/").foldLeft(root)(_.resolve(_)) | ||
val parent = file.toNIO.getParent | ||
if (!Files.exists(parent)) { // cannot create directories when parent is a symlink | ||
Files.createDirectories(parent) | ||
} | ||
Files.deleteIfExists(file.toNIO) | ||
Files.write( | ||
file.toNIO, | ||
contents.getBytes(charset), | ||
StandardOpenOption.WRITE, | ||
StandardOpenOption.CREATE, | ||
) | ||
} | ||
} | ||
root | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
tapiro/core/src/test/scala/io/buildo/tapiro/TapiroSuite.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,135 @@ | ||
package io.buildo.tapiro | ||
|
||
import java.nio.file.Files | ||
|
||
class TapiroSuite extends munit.FunSuite { | ||
|
||
check( | ||
"http4s", | ||
Server.Http4s, | ||
"src/main/scala/schools/endpoints", | ||
""" | ||
|/src/main/scala/schools/SchoolController.scala | ||
|package schools | ||
| | ||
|case class School(id: Long, name: String) | ||
| | ||
|sealed trait SchoolReadError | ||
|object SchoolReadError { | ||
| case object NotFound extends SchoolReadError | ||
|} | ||
| | ||
|trait SchoolController[F[_], T] { | ||
| @query | ||
| def read(id: Long): F[Either[SchoolReadError, School]] | ||
|} | ||
|""".stripMargin, | ||
""" | ||
|/src/main/scala/schools/endpoints/SchoolControllerTapirEndpoints.scala | ||
|//---------------------------------------------------------- | ||
|// This code was generated by tapiro. | ||
|// Changes to this file may cause incorrect behavior | ||
|// and will be lost if the code is regenerated. | ||
|//---------------------------------------------------------- | ||
| | ||
|package endpoints | ||
|import schools._ | ||
|import sttp.tapir._ | ||
|import sttp.tapir.Codec.{JsonCodec, PlainCodec} | ||
|import sttp.model.StatusCode | ||
| | ||
|trait SchoolControllerTapirEndpoints[AuthToken] { | ||
| val read: Endpoint[Long, SchoolReadError, School, Nothing] | ||
|} | ||
| | ||
|object SchoolControllerTapirEndpoints { | ||
| | ||
| def create[AuthToken](statusCodes: String => StatusCode)( | ||
| implicit codec0: JsonCodec[School], | ||
| codec1: JsonCodec[SchoolReadError.NotFound.type], | ||
| codec2: PlainCodec[Long] | ||
| ) = new SchoolControllerTapirEndpoints[AuthToken] { | ||
| override val read: Endpoint[Long, SchoolReadError, School, Nothing] = | ||
| endpoint.get | ||
| .in("read") | ||
| .in(query[Long]("id")) | ||
| .errorOut( | ||
| oneOf[SchoolReadError]( | ||
| statusMapping( | ||
| statusCodes("NotFound"), | ||
| jsonBody[SchoolReadError.NotFound.type] | ||
| ) | ||
| ) | ||
| ) | ||
| .out(jsonBody[School]) | ||
| } | ||
|} | ||
| | ||
|/src/main/scala/schools/endpoints/SchoolControllerHttpEndpoints.scala | ||
|//---------------------------------------------------------- | ||
|// This code was generated by tapiro. | ||
|// Changes to this file may cause incorrect behavior | ||
|// and will be lost if the code is regenerated. | ||
|//---------------------------------------------------------- | ||
| | ||
|package endpoints | ||
|import schools._ | ||
|import cats.effect._ | ||
|import cats.implicits._ | ||
|import cats.data.NonEmptyList | ||
|import org.http4s._ | ||
|import org.http4s.server.Router | ||
|import sttp.tapir.server.http4s._ | ||
|import sttp.tapir.Codec.{JsonCodec, PlainCodec} | ||
|import sttp.model.StatusCode | ||
| | ||
|object SchoolControllerHttpEndpoints { | ||
| | ||
| def routes[F[_]: Sync, AuthToken]( | ||
| controller: SchoolController[F, AuthToken], | ||
| statusCodes: String => StatusCode = _ => StatusCode.UnprocessableEntity | ||
| )( | ||
| implicit codec0: JsonCodec[School], | ||
| codec1: JsonCodec[SchoolReadError.NotFound.type], | ||
| codec2: PlainCodec[Long], | ||
| cs: ContextShift[F] | ||
| ): HttpRoutes[F] = { | ||
| val endpoints = | ||
| SchoolControllerTapirEndpoints.create[AuthToken](statusCodes) | ||
| val read = endpoints.read.toRoutes(controller.read) | ||
| Router("/SchoolController" -> NonEmptyList(read, List()).reduceK) | ||
| } | ||
|} | ||
|""".stripMargin, | ||
) | ||
|
||
def check( | ||
name: String, | ||
server: Server, | ||
endpointDirectory: String, | ||
layout: String, | ||
expectedLayout: String, | ||
`package`: List[String] = List("endpoints"), | ||
)( | ||
implicit loc: munit.Location, | ||
): Unit = | ||
test(name) { | ||
val projectRoot = FileLayout.fromString(layout) | ||
val tapiro = new Util() | ||
tapiro.createFiles( | ||
List(projectRoot.toString), | ||
List(projectRoot.toString), | ||
projectRoot.resolve(endpointDirectory).toString, | ||
`package`, | ||
server, | ||
) | ||
|
||
FileLayout.mapFromString(expectedLayout).map { | ||
case (path, content) => | ||
val filePath = path.split("/").foldLeft(projectRoot)(_.resolve(_)) | ||
assertNoDiff(Files.readString(filePath.toNIO), content) | ||
} | ||
|
||
} | ||
|
||
} |
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
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
3 changes: 0 additions & 3 deletions
3
tapiro/sbt-tapiro/src/sbt-test/sbt-tapiro/simple/src/main/resources/logback.xml
This file was deleted.
Oops, something went wrong.