Skip to content

Commit

Permalink
(dsl): Expose '.routing' as a separate method (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmarjanovic authored Nov 30, 2022
1 parent f6a0dc0 commit 1ff4c67
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zio.elasticsearch

import zio.elasticsearch.ElasticError.DocumentRetrievingError._
import zio.elasticsearch.ElasticError._
import zio.elasticsearch.ElasticRequest._
import zio.schema.Schema
import zio.{RIO, ZIO}

Expand All @@ -10,51 +11,43 @@ sealed trait ElasticRequest[+A] { self =>
ZIO.serviceWithZIO[ElasticExecutor](_.execute(self))

final def map[B](f: A => B): ElasticRequest[B] = ElasticRequest.Map(self, f)

final def routing(value: String): ElasticRequest[A] =
self match {
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: Create => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: CreateOrUpdate => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: Exists => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: GetById => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case _ => self
}
}

object ElasticRequest {

def create[A: Schema](
index: IndexName,
id: DocumentId,
doc: A,
routing: Option[Routing] = None
): ElasticRequest[Unit] =
Create(index = index, id = Some(id), document = Document.from(doc), routing = routing).map(_ => ())
def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
Create(index, Some(id), Document.from(doc)).map(_ => ())

def create[A: Schema](
index: IndexName,
doc: A,
routing: Option[Routing]
): ElasticRequest[Option[DocumentId]] =
Create(index = index, id = None, document = Document.from(doc), routing = routing)
def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] =
Create(index, None, Document.from(doc))

def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] = Exists(index = index, id = id)
def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] =
Exists(index, id)

def getById[A: Schema](
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
): ElasticRequest[Either[DocumentRetrievingError, A]] =
GetById(index, id, routing).map {
def getById[A: Schema](index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentRetrievingError, A]] =
GetById(index, id).map {
case Some(document) => document.decode.left.map(err => DecoderError(err.message))
case None => Left(DocumentNotFound)
}

def upsert[A: Schema](
index: IndexName,
id: DocumentId,
doc: A,
routing: Option[Routing] = None
): ElasticRequest[Unit] =
CreateOrUpdate(index = index, id = id, document = Document.from(doc), routing = routing)
def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[Unit] =
CreateIndex(name, definition)

def createIndex(
name: IndexName,
definition: Option[String]
): ElasticRequest[Unit] = CreateIndex(name, definition)
def deleteIndex(name: IndexName): ElasticRequest[Unit] =
DeleteIndex(name)

def deleteIndex(name: IndexName): ElasticRequest[Unit] = DeleteIndex(name)
def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit] =
CreateOrUpdate(index, id, Document.from(doc))

private[elasticsearch] final case class Create(
index: IndexName,
Expand All @@ -63,13 +56,20 @@ object ElasticRequest {
routing: Option[Routing] = None
) extends ElasticRequest[Option[DocumentId]]

private[elasticsearch] final case class CreateIndex(
name: IndexName,
definition: Option[String]
) extends ElasticRequest[Unit]

private[elasticsearch] final case class CreateOrUpdate(
index: IndexName,
id: DocumentId,
document: Document,
routing: Option[Routing] = None
) extends ElasticRequest[Unit]

private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Unit]

private[elasticsearch] final case class Exists(
index: IndexName,
id: DocumentId,
Expand All @@ -82,11 +82,6 @@ object ElasticRequest {
routing: Option[Routing] = None
) extends ElasticRequest[Option[Document]]

private[elasticsearch] final case class CreateIndex(name: IndexName, definition: Option[String])
extends ElasticRequest[Unit]

private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Unit]

private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B)
extends ElasticRequest[B]
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,13 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
.unit

private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = {
val u = uri"$basePath/${r.index}/$Doc/${r.id}"
.withParam("routing", r.routing.map(_.value))

request
.put(u)
.contentType(ApplicationJson)
.body(r.document.json)
.send(client)
.unit
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(_.value))
request.put(uri).contentType(ApplicationJson).body(r.document.json).send(client).unit
}

private def executeExists(r: Exists): Task[Boolean] = {
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(_.value))
request
.head(uri)
.send(client)
.map(_.code.equals(Ok))
request.head(uri).send(client).map(_.code.equals(Ok))
}

private def executeDeleteIndex(r: DeleteIndex): Task[Unit] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package zio.elasticsearch

final case class Routing(value: String) extends AnyVal
private[elasticsearch] final case class Routing(value: String) extends AnyVal

0 comments on commit 1ff4c67

Please sign in to comment.