/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1-dev * User agent: apibuilder app.apibuilder.io/poole-solutions/questlog-api/0.0.1-dev/http4s_0_20 */ package solutions.poole.questlog.api.v0.models { /** * @param guid primary key */ trait DatabaseRecord { def guid: _root_.java.util.UUID } sealed trait Person extends _root_.scala.Product with _root_.scala.Serializable { def personDiscriminator: PersonDiscriminator def email: _root_.scala.Option[String] def name: _root_.scala.Option[String] } /** * Defines the valid discriminator values for the type Person */ sealed trait PersonDiscriminator extends _root_.scala.Product with _root_.scala.Serializable object PersonDiscriminator { case object Guest extends PersonDiscriminator { override def toString = "guest" } case object User extends PersonDiscriminator { override def toString = "user" } final case class UNDEFINED(override val toString: String) extends PersonDiscriminator val all: scala.List[PersonDiscriminator] = scala.List(Guest, User) private val byName: Map[String, PersonDiscriminator] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): PersonDiscriminator = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[PersonDiscriminator] = byName.get(value.toLowerCase) } /** * Represents an adventure. */ final case class Adventure( override val guid: _root_.java.util.UUID, name: String, description: _root_.scala.Option[String] = None, location: _root_.scala.Option[String] = None, participants: _root_.scala.Option[Seq[solutions.poole.questlog.api.v0.models.Participant]] = None, packLists: _root_.scala.Option[Seq[solutions.poole.questlog.api.v0.models.Item]] = None ) extends DatabaseRecord /** * Represents a validation error from the application. */ final case class Error( code: String, message: String ) final case class Guest( override val email: _root_.scala.Option[String] = None, override val name: _root_.scala.Option[String] = None ) extends Person { override val personDiscriminator: PersonDiscriminator = PersonDiscriminator.Guest } final case class Item( name: String, size: _root_.scala.Option[Double] = None, description: _root_.scala.Option[String] = None, `type`: _root_.scala.Option[solutions.poole.questlog.api.v0.models.ItemType] = None ) /** * Represents a adventure participant and what items they will bring. */ final case class Participant( person: solutions.poole.questlog.api.v0.models.Person, items: _root_.scala.Option[Seq[solutions.poole.questlog.api.v0.models.Item]] = None ) final case class User( override val guid: _root_.java.util.UUID, override val email: _root_.scala.Option[String] = None, override val name: _root_.scala.Option[String] = None ) extends DatabaseRecord with Person { override val personDiscriminator: PersonDiscriminator = PersonDiscriminator.User } /** * Provides future compatibility in clients - in the future, when a type is added * to the union Person, it will need to be handled in the client code. This * implementation will deserialize these future types as an instance of this class. * * @param description Information about the type that we received that is undefined in this version of * the client. */ final case class PersonUndefinedType( description: String ) extends Person { override val personDiscriminator: PersonDiscriminator = PersonDiscriminator.UNDEFINED(description) override def email: _root_.scala.Option[String] = ??? override def name: _root_.scala.Option[String] = ??? } sealed trait ItemType extends _root_.scala.Product with _root_.scala.Serializable object ItemType { /** * Required to keep going */ case object Food extends ItemType { override def toString = "Food" } /** * Required to fend of nature */ case object Clothes extends ItemType { override def toString = "Clothes" } /** * Required for sleep */ case object Camp extends ItemType { override def toString = "Camp" } /** * Required for eating */ case object Utensils extends ItemType { override def toString = "Utensils" } /** * Required to perform the given activity */ case object Gear extends ItemType { override def toString = "Gear" } /** * UNDEFINED captures values that are sent either in error or * that were added by the server after this library was * generated. We want to make it easy and obvious to users of * this library to handle this case gracefully. * * We use all CAPS for the variable name to avoid collisions * with the camel cased values above. */ final case class UNDEFINED(override val toString: String) extends ItemType /** * all returns a list of all the valid, known values. We use * lower case to avoid collisions with the camel cased values * above. */ val all: scala.List[ItemType] = scala.List(Food, Clothes, Camp, Utensils, Gear) private val byName: Map[String, ItemType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ItemType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ItemType] = byName.get(value.toLowerCase) } } package solutions.poole.questlog.api.v0.models { package object json { import io.circe.Decoder._ import io.circe.Encoder._ import scala.language.implicitConversions // See below - Make Scala 2.11 Either monadic import scala.util.Try import io.circe.{Json, JsonObject, Encoder, Decoder, DecodingFailure} import io.circe.syntax._ import solutions.poole.questlog.api.v0.models.json._ // Make Scala 2.11 Either monadic private[v0] implicit def eitherOps[A,B](e: Either[A,B]) = cats.implicits.catsSyntaxEither(e) private[v0] implicit val decodeUUID: Decoder[_root_.java.util.UUID] = Decoder.decodeString.emapTry(str => Try(_root_.java.util.UUID.fromString(str))) private[v0] implicit val encodeUUID: Encoder[_root_.java.util.UUID] = Encoder.encodeString.contramap[_root_.java.util.UUID](uuid => uuid.toString) private[v0] implicit val decodeInstant: Decoder[_root_.java.time.Instant] = Decoder.decodeString.emapTry(str => Try(_root_.java.time.OffsetDateTime.parse(str).toInstant)) private[v0] implicit val encodeInstant: Encoder[_root_.java.time.Instant] = Encoder.encodeString.contramap[_root_.java.time.Instant](_.toString) private[v0] implicit val decodeLocalDate: Decoder[_root_.java.time.LocalDate] = Decoder.decodeString.emapTry(str => Try(_root_.java.time.LocalDate.parse(str))) private[v0] implicit val encodeLocalDate: Encoder[_root_.java.time.LocalDate] = Encoder.encodeString.contramap[_root_.java.time.LocalDate](_.toString) implicit val jsonDecoderQuestlogApiItemType: Decoder[solutions.poole.questlog.api.v0.models.ItemType] = Decoder.decodeString.map(solutions.poole.questlog.api.v0.models.ItemType(_)) implicit val jsonEncoderQuestlogApiItemType: Encoder[solutions.poole.questlog.api.v0.models.ItemType] = Encoder.encodeString.contramap[solutions.poole.questlog.api.v0.models.ItemType](_.toString) implicit def decodeQuestlogApiAdventure: Decoder[Adventure] = Decoder.instance { c => for { guid <- c.downField("guid").as[_root_.java.util.UUID] name <- c.downField("name").as[String] description <- c.downField("description").as[Option[String]] location <- c.downField("location").as[Option[String]] participants <- c.downField("participants").as[Option[Seq[solutions.poole.questlog.api.v0.models.Participant]]] packLists <- c.downField("pack_lists").as[Option[Seq[solutions.poole.questlog.api.v0.models.Item]]] } yield { Adventure( guid = guid, name = name, description = description, location = location, participants = participants, packLists = packLists ) } } implicit def encodeQuestlogApiAdventure: Encoder[Adventure] = Encoder.instance { t => Json.fromFields(Seq( Some("guid" -> t.guid.asJson), Some("name" -> t.name.asJson), t.description.map(t => "description" -> t.asJson), t.location.map(t => "location" -> t.asJson), t.participants.map(t => "participants" -> t.asJson), t.packLists.map(t => "pack_lists" -> t.asJson) ).flatten) } implicit def decodeQuestlogApiError: Decoder[Error] = Decoder.instance { c => for { code <- c.downField("code").as[String] message <- c.downField("message").as[String] } yield { Error( code = code, message = message ) } } implicit def encodeQuestlogApiError: Encoder[Error] = Encoder.instance { t => Json.fromFields(Seq( Some("code" -> t.code.asJson), Some("message" -> t.message.asJson) ).flatten) } implicit def decodeQuestlogApiGuest: Decoder[Guest] = Decoder.instance { c => for { email <- c.downField("email").as[Option[String]] name <- c.downField("name").as[Option[String]] } yield { Guest( email = email, name = name ) } } implicit def encodeQuestlogApiGuest: Encoder[Guest] = Encoder.instance { t => Json.fromFields(Seq( t.email.map(t => "email" -> t.asJson), t.name.map(t => "name" -> t.asJson) ).flatten) } implicit def decodeQuestlogApiItem: Decoder[Item] = Decoder.instance { c => for { name <- c.downField("name").as[String] size <- c.downField("size").as[Option[Double]] description <- c.downField("description").as[Option[String]] __type__ <- c.downField("type").as[Option[solutions.poole.questlog.api.v0.models.ItemType]] } yield { Item( name = name, size = size, description = description, `type` = __type__ ) } } implicit def encodeQuestlogApiItem: Encoder[Item] = Encoder.instance { t => Json.fromFields(Seq( Some("name" -> t.name.asJson), t.size.map(t => "size" -> t.asJson), t.description.map(t => "description" -> t.asJson), t.`type`.map(t => "type" -> t.asJson) ).flatten) } implicit def decodeQuestlogApiParticipant: Decoder[Participant] = Decoder.instance { c => for { person <- c.downField("person").as[solutions.poole.questlog.api.v0.models.Person] items <- c.downField("items").as[Option[Seq[solutions.poole.questlog.api.v0.models.Item]]] } yield { Participant( person = person, items = items ) } } implicit def encodeQuestlogApiParticipant: Encoder[Participant] = Encoder.instance { t => Json.fromFields(Seq( Some("person" -> t.person.asJson), t.items.map(t => "items" -> t.asJson) ).flatten) } implicit def decodeQuestlogApiUser: Decoder[User] = Decoder.instance { c => for { guid <- c.downField("guid").as[_root_.java.util.UUID] email <- c.downField("email").as[Option[String]] name <- c.downField("name").as[Option[String]] } yield { User( guid = guid, email = email, name = name ) } } implicit def encodeQuestlogApiUser: Encoder[User] = Encoder.instance { t => Json.fromFields(Seq( Some("guid" -> t.guid.asJson), t.email.map(t => "email" -> t.asJson), t.name.map(t => "name" -> t.asJson) ).flatten) } implicit def decodeQuestlogApiPerson: Decoder[Person] = Decoder.instance { c => c.get[Option[String]]("discriminator") match { case Right(Some(s)) if s == "guest" => c.as[solutions.poole.questlog.api.v0.models.Guest] case Right(Some(s)) if s == "user" => c.as[solutions.poole.questlog.api.v0.models.User] case Right(Some(s)) => Right(solutions.poole.questlog.api.v0.models.PersonUndefinedType(s)) case _ => Left(DecodingFailure("Union[Person] requires a discriminator named 'discriminator' - this field was not found in the Json", c.history)) } } implicit def encodeQuestlogApiPerson: Encoder[Person] = Encoder.instance { case t: solutions.poole.questlog.api.v0.models.Guest => t.asJson.mapObject(obj => ("discriminator", Json.fromString("guest")) +: obj) case t: solutions.poole.questlog.api.v0.models.User => t.asJson.mapObject(obj => ("discriminator", Json.fromString("user")) +: obj) case other => sys.error(s"The type[${other.getClass.getName}] has no JSON encoder") } } }