Skip to content

Commit

Permalink
Add date/datetime schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Mar 27, 2019
1 parent a6a2a3e commit eb27420
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/src/main/scala/tapir/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import tapir.generic.{FormCodecDerivation, MultipartCodecDerivation}
import tapir.internal.UrlencodedData
import tapir.model.Part

import scala.annotation.implicitNotFound

/**
* A pair of functions, one to encode a value of type `T` to a raw value of type `R`,
* and another one to decode.
Expand All @@ -21,6 +23,13 @@ import tapir.model.Part
* @tparam M The media type of encoded values.
* @tparam R Type of the raw value to which values are encoded.
*/
@implicitNotFound(msg = """Cannot find a codec for type: ${T} and media type: ${M}.
Did you define a codec for: ${T}?
Did you import the codecs for: ${M}?
Is there an implicit schema for: ${T}, and all of its components?
(codecs are looked up as implicit values of type Codec[${T}, ${M}, _];
schemas are looked up as implicit values of type SchemaFor[${T}])
""")
trait Codec[T, M <: MediaType, R] { outer =>
def encode(t: T): R
def decode(s: R): DecodeResult[T]
Expand Down Expand Up @@ -151,6 +160,13 @@ object Codec extends FormCodecDerivation with MultipartCodecDerivation {
*
* Should be used for inputs/outputs which allow optional values.
*/
@implicitNotFound(msg = """Cannot find a codec for type: ${T} and media type: ${M}.
Did you define a codec for: ${T}?
Did you import the codecs for: ${M}?
Is there an implicit schema for: ${T}, and all of its components?
(codecs are looked up as implicit values of type Codec[${T}, ${M}, _];
schemas are looked up as implicit values of type SchemaFor[${T}])
""")
trait CodecForOptional[T, M <: MediaType, R] { outer =>
def encode(t: T): Option[R]
def decode(s: Option[R]): DecodeResult[T]
Expand Down Expand Up @@ -196,6 +212,13 @@ object CodecForOptional {
*
* Should be used for inputs/outputs which allow multiple values.
*/
@implicitNotFound(msg = """Cannot find a codec for type: ${T} and media type: ${M}.
Did you define a codec for: ${T}?
Did you import the codecs for: ${M}?
Is there an implicit schema for: ${T}, and all of its components?
(codecs are looked up as implicit values of type Codec[${T}, ${M}, _];
schemas are looked up as implicit values of type SchemaFor[${T}])
""")
trait CodecForMany[T, M <: MediaType, R] { outer =>
def encode(t: T): Seq[R]
def decode(s: Seq[R]): DecodeResult[T]
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/tapir/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ object Schema {
case object SBinary extends Schema {
def show: String = "binary"
}
case object SDate extends Schema {
def show: String = "date"
}
case object SDateTime extends Schema {
def show: String = "date-time"
}

case class SRef(fullName: String) extends Schema {
def show: String = s"ref($fullName)"
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/scala/tapir/SchemaFor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tapir
import java.io.File
import java.nio.ByteBuffer
import java.nio.file.Path
import java.time._
import java.util.Date

import tapir.Schema._
import tapir.generic.SchemaForMagnoliaDerivation
Expand Down Expand Up @@ -47,6 +49,24 @@ object SchemaFor extends SchemaForMagnoliaDerivation {
implicit case object SchemaForByteBuffer extends SchemaFor[ByteBuffer] {
override val schema: Schema = SBinary
}
implicit case object SchemaForInstant extends SchemaFor[Instant] {
override val schema: Schema = SDateTime
}
implicit case object SchemaForZonedDateTime extends SchemaFor[ZonedDateTime] {
override val schema: Schema = SDateTime
}
implicit case object SchemaForOffsetDateTime extends SchemaFor[OffsetDateTime] {
override val schema: Schema = SDateTime
}
implicit case object SchemaForDate extends SchemaFor[Date] {
override val schema: Schema = SDateTime
}
implicit case object SchemaForLocalDateTime extends SchemaFor[LocalDateTime] {
override val schema: Schema = SDateTime
}
implicit case object SchemaForLocalDate extends SchemaFor[LocalDate] {
override val schema: Schema = SDate
}
implicit def schemaForOption[T: SchemaFor]: SchemaFor[Option[T]] = new SchemaFor[Option[T]] {
override def schema: Schema = implicitly[SchemaFor[T]].schema
override def isOptional: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ private[schema] class TSchemaToOSchema(fullNameToKey: Map[String, SchemaKey]) {
))
case TSchema.SBinary =>
Right(OSchema(SchemaType.String).copy(format = Some(SchemaFormat.Binary)))
case TSchema.SDate =>
Right(OSchema(SchemaType.String).copy(format = Some(SchemaFormat.Date)))
case TSchema.SDateTime =>
Right(OSchema(SchemaType.String).copy(format = Some(SchemaFormat.DateTime)))
case SRef(fullName) =>
Left(Reference("#/components/schemas/" + fullNameToKey(fullName)))
}
Expand Down

0 comments on commit eb27420

Please sign in to comment.