Skip to content

Instantly share code, notes, and snippets.

@ntngel1
Created July 19, 2024 20:24
Show Gist options
  • Save ntngel1/61ecc1787441f3e0b2a76af47a44d240 to your computer and use it in GitHub Desktop.
Save ntngel1/61ecc1787441f3e0b2a76af47a44d240 to your computer and use it in GitHub Desktop.
package routing
import Readers._
import cats.implicits._
import errors.internal.base.AppError
import play.api.libs.json.{Json, OWrites}
sealed trait PathSegment
object PathSegment {
final case class Literal(value: String) extends PathSegment
final case object Trailing extends PathSegment
final case class Parameter[T : PathParameter](
name: String,
description: Option[String]
) extends PathSegment
}
trait PathParameter[T] {
def read(value: String): Either[AppError, T]
}
object Readers {
implicit val stringReader: PathParameter[String] = (value: String) => value.asRight[AppError]
}
case class Endpoint[I, O](
path: Seq[PathSegment],
queryParams: Seq[QueryParameter[_]],
)
final case class QueryParameter[T](
name: String,
description: Option[String],
defaultValue: Option[T]
)
object Main {
def main(args: Array[String]): Unit = {
val endpoint = Endpoint(
path = Seq(PathSegment.Literal("api"), PathSegment.Literal("cinema"), PathSegment.Parameter[String]("id", description = None), PathSegment.Trailing),
queryParams = Seq.empty
)
println(OpenAPI(version = "3.1.0", info = OpenAPI.Info("Hello World,")))
}
}
case class OpenAPI(
version: String,
info: OpenAPI.Info,
)
trait OpenAPIJson {
implicit val writes: OWrites[OpenAPI] = Json.writes[OpenAPI]
}
object OpenAPI {
case class Info(
title: String,
summary: Option[String],
description: Option[String],
termsOfServiceUrl: Option[String],
version: String
)
trait InfoJson {
implicit val writes: OWrites[Info] = Json.writes[Info]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment