All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.kodeworks.kvarg.util.JsonUtil.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.util

import akka.actor.{ActorRef, ActorRefFactory}
import akka.util.Timeout
import no.kodeworks.kvarg.message.{GetAll, GetAllReply}
import io.circe.generic.extras.Configuration
import io.circe.syntax._
import io.circe.{Encoder, Json, Printer}
import shapeless.Poly1

import scala.concurrent.{ExecutionContext, Future}

object JsonUtil {

  implicit val circeCfg: Configuration = Configuration.default.withDefaults
  implicit val circePrinter: Printer = Printer.noSpaces.copy(dropNullValues = true)

  def getAll(servicesEncodes: (ActorRef, Encoder[GetAllReply[_]])*)(implicit ec: ExecutionContext, ac: ActorRefFactory, timeout: Timeout = AtLeastOnceDelivery.defaultTimeout): Future[Json] = {
    import AtLeastOnceDelivery.pattern
    Future.sequence(servicesEncodes.map { case (s, e) =>
      (s ?? GetAll).mapTo[GetAllReply[_]].map(e.apply)
    })
      .map(_.reduce(_ deepMerge _))
  }

  object polyToJson extends Poly1 {
    implicit def default[A: Encoder] = at[A] { x =>
      x.asJson
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy