Skip to content

Commit

Permalink
follow code-review suggestion & use case class default values instead of
Browse files Browse the repository at this point in the history
hand-crufted formatters
  • Loading branch information
Denys Fakhritdinov committed Nov 12, 2024
1 parent 0c0ce0b commit 5ccc229
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,76 +18,10 @@ object ActionHeader {

implicit val formatOptActionHeader: OFormat[Option[ActionHeader]] = {

val appendFormat = {
val format = Json.format[Append]
val reads = format orElse new Reads[Append] {
def reads(json: JsValue): JsResult[Append] = {

def metadata = {
(json \ "metadata").validate[JsObject] match {
case JsSuccess(a, _) => a.validate[HeaderMetadata]
case _: JsError => HeaderMetadata.empty.pure[JsResult]
}
}

for {
range <- (json \ "range").validate[SeqRange]
origin <- (json \ "origin").validateOpt[Origin]
version <- (json \ "version").validateOpt[Version]
payloadType <- (json \ "payloadType").validate[PayloadType.BinaryOrJson]
metadata <- metadata
} yield {
Append(range, origin, version.getOrElse(Version.obsolete), payloadType, metadata)
}
}
}
OFormat(reads, format)
}
val deleteFormat = {
val format = Json.format[Delete]
val reads = format orElse new Reads[Delete] {
def reads(json: JsValue): JsResult[Delete] = {
for {
to <- (json \ "to").validate[SeqNr]
origin <- (json \ "origin").validateOpt[Origin]
version <- (json \ "version").validateOpt[Version]
} yield {
Delete(to.toDeleteTo, origin, version.getOrElse(Version.obsolete))
}
}
}
OFormat(reads, format)
}
val purgeFormat = {

val format = Json.format[Purge]
val reads = format orElse new Reads[Purge] {
def reads(json: JsValue): JsResult[Purge] = {
for {
origin <- (json \ "origin").validateOpt[Origin]
version <- (json \ "version").validateOpt[Version]
} yield {
Purge(origin, version.getOrElse(Version.obsolete))
}
}
}
OFormat(reads, format)
}
val readFormat = {
val format = Json.format[Mark]
val reads = format orElse new Reads[Mark] {
def reads(json: JsValue): JsResult[Mark] = {
for {
id <- (json \ "id").validate[String]
origin <- (json \ "origin").validateOpt[Origin]
version <- (json \ "version").validateOpt[Version]
} yield {
Mark(id, origin, version.getOrElse(Version.obsolete))
}
}
}
OFormat(reads, format)
}
val appendFormat = Json.using[Json.WithDefaultValues].format[Append]
val deleteFormat = Json.using[Json.WithDefaultValues].format[Delete]
val purgeFormat = Json.using[Json.WithDefaultValues].format[Purge]
val readFormat = Json.using[Json.WithDefaultValues].format[Mark]

new OFormat[Option[ActionHeader]] {

Expand Down Expand Up @@ -138,25 +72,25 @@ object ActionHeader {
final case class Append(
range: SeqRange,
origin: Option[Origin],
version: Version,
version: Version = Version.obsolete,
payloadType: PayloadType.BinaryOrJson,
metadata: HeaderMetadata,
metadata: HeaderMetadata = HeaderMetadata.empty,
) extends AppendOrDelete

final case class Delete(
to: DeleteTo,
origin: Option[Origin],
version: Version,
version: Version = Version.obsolete,
) extends AppendOrDelete

final case class Purge(
origin: Option[Origin],
version: Version,
version: Version = Version.obsolete,
) extends AppendOrDelete

final case class Mark(
id: String,
origin: Option[Origin],
version: Version,
version: Version = Version.obsolete,
) extends ActionHeader
}
49 changes: 0 additions & 49 deletions notes.md

This file was deleted.

0 comments on commit 5ccc229

Please sign in to comment.