Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil committed Oct 24, 2023
1 parent 426177b commit 3ab3e84
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions zio-http/src/main/scala/zio/http/endpoint/openapi/JsonSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,44 +104,6 @@ private[openapi] object TypeOrTypes {
}
}

@noDiscriminator
sealed trait EnumValue { self =>
def toJson: Json = self match {
case EnumValue.Bool(value) => Json.Bool(value)
case EnumValue.Str(value) => Json.Str(value)
case EnumValue.Num(value) => Json.Num(value)
case EnumValue.Null => Json.Null
case EnumValue.SchemaValue(value) =>
Json.decoder
.decodeJson(value.toJson)
.getOrElse(throw new IllegalArgumentException(s"Can't convert $self"))
}
}

object EnumValue {

def fromJson(json: Json): EnumValue =
json match {
case Json.Str(value) => Str(value)
case Json.Num(value) => Num(value)
case Json.Bool(value) => Bool(value)
case Json.Null => Null
case other =>
SchemaValue(
JsonSchema.codec
.decode(Chunk.fromArray(other.toString().getBytes))
.getOrElse(throw new IllegalArgumentException(s"Can't convert $json")),
)
}

final case class SchemaValue(value: JsonSchema) extends EnumValue
final case class Bool(value: Boolean) extends EnumValue
final case class Str(value: String) extends EnumValue
final case class Num(value: BigDecimal) extends EnumValue
case object Null extends EnumValue

}

sealed trait JsonSchema extends Product with Serializable { self =>

lazy val toJsonBytes: Chunk[Byte] = JsonCodec.schemaBasedBinaryCodec[JsonSchema].encode(self)
Expand Down Expand Up @@ -701,6 +663,44 @@ object JsonSchema {
)
}

@noDiscriminator
sealed trait EnumValue { self =>
def toJson: Json = self match {
case EnumValue.Bool(value) => Json.Bool(value)
case EnumValue.Str(value) => Json.Str(value)
case EnumValue.Num(value) => Json.Num(value)
case EnumValue.Null => Json.Null
case EnumValue.SchemaValue(value) =>
Json.decoder
.decodeJson(value.toJson)
.getOrElse(throw new IllegalArgumentException(s"Can't convert $self"))
}
}

object EnumValue {

def fromJson(json: Json): EnumValue =
json match {
case Json.Str(value) => Str(value)
case Json.Num(value) => Num(value)
case Json.Bool(value) => Bool(value)
case Json.Null => Null
case other =>
SchemaValue(
JsonSchema.codec
.decode(Chunk.fromArray(other.toString().getBytes))
.getOrElse(throw new IllegalArgumentException(s"Can't convert $json")),
)
}

final case class SchemaValue(value: JsonSchema) extends EnumValue
final case class Bool(value: Boolean) extends EnumValue
final case class Str(value: String) extends EnumValue
final case class Num(value: BigDecimal) extends EnumValue
case object Null extends EnumValue

}

case object Null extends JsonSchema {
override protected[openapi] def toSerializableSchema: SerializableJsonSchema =
SerializableJsonSchema(
Expand Down

0 comments on commit 3ab3e84

Please sign in to comment.