/** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1 * User agent: apibuilder app.apibuilder.io/nccloud/registry-api/0.0.1/http4s_0_18 */ package de.nccloud.registry.api.v0.models { sealed trait ServiceEvent extends _root_.scala.Product with _root_.scala.Serializable { def serviceEventType: ServiceEventType } /** * Defines the valid type values for the type ServiceEvent */ sealed trait ServiceEventType extends _root_.scala.Product with _root_.scala.Serializable object ServiceEventType { case object ServiceUpsertedEvent extends ServiceEventType { override def toString = "service_upserted_event" } case object ServiceDeletedEvent extends ServiceEventType { override def toString = "service_deleted_event" } final case class UNDEFINED(override val toString: String) extends ServiceEventType val all: scala.List[ServiceEventType] = scala.List(ServiceUpsertedEvent, ServiceDeletedEvent) private val byName: Map[String, ServiceEventType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ServiceEventType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ServiceEventType] = byName.get(value.toLowerCase) } /** * A single available service. * * @param uri URI under which this service is deployed. * @param title A human-readable title of the service. * @param description A human-readable description of the service. */ final case class Service( name: String, version: String, state: de.nccloud.registry.api.v0.models.ServiceState, `type`: de.nccloud.registry.api.v0.models.ServiceType = de.nccloud.registry.api.v0.models.ServiceType.Public, uri: String, title: _root_.scala.Option[String] = None, description: _root_.scala.Option[String] = None ) /** * A service has been undeployed is not reachable anymore. * * @param name Name of the service that has been undeployed. */ final case class ServiceDeletedEvent( name: String, timestamp: _root_.java.time.Instant ) extends ServiceEvent { override val serviceEventType: ServiceEventType = ServiceEventType.ServiceDeletedEvent } /** * A service has been newly deployed or updated. */ final case class ServiceUpsertedEvent( service: de.nccloud.registry.api.v0.models.Service, timestamp: _root_.java.time.Instant ) extends ServiceEvent { override val serviceEventType: ServiceEventType = ServiceEventType.ServiceUpsertedEvent } /** * Provides future compatibility in clients - in the future, when a type is added * to the union ServiceEvent, 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 ServiceEventUndefinedType( description: String ) extends ServiceEvent { override val serviceEventType: ServiceEventType = ServiceEventType.UNDEFINED(description) } /** * The current state of a single service. */ sealed trait ServiceState extends _root_.scala.Product with _root_.scala.Serializable object ServiceState { /** * This service is currently not deployed. */ case object Unavailable extends ServiceState { override def toString = "unavailable" } /** * This service is deployed and reachable. */ case object Available extends ServiceState { override def toString = "available" } /** * This service is deployed but not reachable due to an error. */ case object Error extends ServiceState { override def toString = "error" } /** * 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 ServiceState /** * 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[ServiceState] = scala.List(Unavailable, Available, Error) private val byName: Map[String, ServiceState] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ServiceState = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ServiceState] = byName.get(value.toLowerCase) } /** * Indicates where a service is reachable. */ sealed trait ServiceType extends _root_.scala.Product with _root_.scala.Serializable object ServiceType { /** * This service is publicly reachable. */ case object Public extends ServiceType { override def toString = "public" } /** * This service only reachable internally in the cluster. */ case object Internal extends ServiceType { override def toString = "internal" } /** * 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 ServiceType /** * 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[ServiceType] = scala.List(Public, Internal) private val byName: Map[String, ServiceType] = all.map(x => x.toString.toLowerCase -> x).toMap def apply(value: String): ServiceType = fromString(value).getOrElse(UNDEFINED(value)) def fromString(value: String): _root_.scala.Option[ServiceType] = byName.get(value.toLowerCase) } } package de.nccloud.registry.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 de.nccloud.common.v0.models.json._ import de.nccloud.registry.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 jsonDecoderRegistryApiServiceState: Decoder[de.nccloud.registry.api.v0.models.ServiceState] = Decoder.decodeString.map(de.nccloud.registry.api.v0.models.ServiceState(_)) implicit val jsonEncoderRegistryApiServiceState: Encoder[de.nccloud.registry.api.v0.models.ServiceState] = Encoder.encodeString.contramap[de.nccloud.registry.api.v0.models.ServiceState](_.toString) implicit val jsonDecoderRegistryApiServiceType: Decoder[de.nccloud.registry.api.v0.models.ServiceType] = Decoder.decodeString.map(de.nccloud.registry.api.v0.models.ServiceType(_)) implicit val jsonEncoderRegistryApiServiceType: Encoder[de.nccloud.registry.api.v0.models.ServiceType] = Encoder.encodeString.contramap[de.nccloud.registry.api.v0.models.ServiceType](_.toString) implicit def decodeRegistryApiService: Decoder[Service] = Decoder.instance { c => for { name <- c.downField("name").as[String] version <- c.downField("version").as[String] state <- c.downField("state").as[de.nccloud.registry.api.v0.models.ServiceState] __type__ <- c.downField("type").as[de.nccloud.registry.api.v0.models.ServiceType] uri <- c.downField("uri").as[String] title <- c.downField("title").as[Option[String]] description <- c.downField("description").as[Option[String]] } yield { Service( name = name, version = version, state = state, `type` = __type__, uri = uri, title = title, description = description ) } } implicit def encodeRegistryApiService: Encoder[Service] = Encoder.instance { t => Json.fromFields(Seq( Some("name" -> t.name.asJson), Some("version" -> t.version.asJson), Some("state" -> t.state.asJson), Some("type" -> t.`type`.asJson), Some("uri" -> t.uri.asJson), t.title.map(t => "title" -> t.asJson), t.description.map(t => "description" -> t.asJson) ).flatten) } implicit def decodeRegistryApiServiceDeletedEvent: Decoder[ServiceDeletedEvent] = Decoder.instance { c => for { name <- c.downField("name").as[String] timestamp <- c.downField("timestamp").as[_root_.java.time.Instant] } yield { ServiceDeletedEvent( name = name, timestamp = timestamp ) } } implicit def encodeRegistryApiServiceDeletedEvent: Encoder[ServiceDeletedEvent] = Encoder.instance { t => Json.fromFields(Seq( Some("name" -> t.name.asJson), Some("timestamp" -> t.timestamp.asJson) ).flatten) } implicit def decodeRegistryApiServiceUpsertedEvent: Decoder[ServiceUpsertedEvent] = Decoder.instance { c => for { service <- c.downField("service").as[de.nccloud.registry.api.v0.models.Service] timestamp <- c.downField("timestamp").as[_root_.java.time.Instant] } yield { ServiceUpsertedEvent( service = service, timestamp = timestamp ) } } implicit def encodeRegistryApiServiceUpsertedEvent: Encoder[ServiceUpsertedEvent] = Encoder.instance { t => Json.fromFields(Seq( Some("service" -> t.service.asJson), Some("timestamp" -> t.timestamp.asJson) ).flatten) } implicit def decodeRegistryApiServiceEvent: Decoder[ServiceEvent] = Decoder.instance { c => c.get[Option[String]]("type") match { case Right(Some(s)) if s == "service_upserted_event" => c.as[de.nccloud.registry.api.v0.models.ServiceUpsertedEvent] case Right(Some(s)) if s == "service_deleted_event" => c.as[de.nccloud.registry.api.v0.models.ServiceDeletedEvent] case Right(Some(s)) => Right(de.nccloud.registry.api.v0.models.ServiceEventUndefinedType(s)) case _ => Left(DecodingFailure("Union[ServiceEvent] requires a discriminator named 'type' - this field was not found in the Json", c.history)) } } implicit def encodeRegistryApiServiceEvent: Encoder[ServiceEvent] = Encoder.instance { case t: de.nccloud.registry.api.v0.models.ServiceUpsertedEvent => t.asJson.mapObject(obj => ("type", Json.fromString("service_upserted_event")) +: obj) case t: de.nccloud.registry.api.v0.models.ServiceDeletedEvent => t.asJson.mapObject(obj => ("type", Json.fromString("service_deleted_event")) +: obj) case other => sys.error(s"The type[${other.getClass.getName}] has no JSON encoder") } } }