aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/bridgeuploadqueue.scala
blob: 6725a1593252381a1cf5c01d0cc89c8790cc4674 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package xyz.driver.pdsuidomain.formats.json

import java.time.LocalDateTime

import spray.json._
import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue
import xyz.driver.pdsuicommon.concurrent.BridgeUploadQueue.Item

object bridgeuploadqueue {
  import DefaultJsonProtocol._
  import common._

  implicit val queueUploadItemFormat: RootJsonFormat[BridgeUploadQueue.Item] = new RootJsonFormat[Item] {
    override def write(obj: Item) =
      JsObject(
        "kind"        -> obj.kind.toJson,
        "tag"         -> obj.tag.toJson,
        "created"     -> obj.created.toJson,
        "attempts"    -> obj.attempts.toJson,
        "nextAttempt" -> obj.nextAttempt.toJson,
        "completed"   -> obj.completed.toJson
      )

    override def read(json: JsValue): Item = json match {
      case JsObject(fields) =>
        val kind = fields
          .get("kind")
          .map(_.convertTo[String])
          .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `kind` field: $json"))

        val tag = fields
          .get("tag")
          .map(_.convertTo[String])
          .getOrElse(deserializationError(s"BridgeUploadQueue.Item json object does not contain `tag` field: $json"))

        val created = fields
          .get("created")
          .map(_.convertTo[LocalDateTime])
          .getOrElse(
            deserializationError(s"BridgeUploadQueue.Item json object does not contain `created` field: $json"))

        val attempts = fields
          .get("attempts")
          .map(_.convertTo[Int])
          .getOrElse(
            deserializationError(s"BridgeUploadQueue.Item json object does not contain `attempts` field: $json"))

        val nextAttempt = fields
          .get("nextAttempt")
          .map(_.convertTo[LocalDateTime])
          .getOrElse(
            deserializationError(s"BridgeUploadQueue.Item json object does not contain `nextAttempt` field: $json"))

        BridgeUploadQueue.Item(
          kind = kind,
          tag = tag,
          created = created,
          attempts = attempts,
          nextAttempt = nextAttempt,
          completed = true,
          dependencyKind = None,
          dependencyTag = None
        )

      case _ => deserializationError(s"Expected Json Object as BridgeUploadQueue.Item, but got $json")
    }
  }
}