Skip to content

Commit

Permalink
(dsl): Refactor routing parameter (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 authored Dec 27, 2022
1 parent bee9f3b commit 73c9366
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package zio.elasticsearch

import zio.elasticsearch.ElasticError.DocumentRetrievingError._
import zio.elasticsearch.ElasticError._
import zio.elasticsearch.ElasticRequest._
import zio.elasticsearch.Refresh.WithRefresh
import zio.elasticsearch.Routing.{Routing, WithRouting}
import zio.schema.Schema
import zio.{RIO, ZIO}

Expand All @@ -23,20 +23,13 @@ sealed trait ElasticRequest[+A, ERT <: ElasticRequestType] { self =>
final def refreshTrue(implicit wr: WithRefresh[ERT]): ElasticRequest[A, ERT] =
wr.withRefresh(request = self, value = true)

final def routing(value: Routing): ElasticRequest[A, ERT] = self match {
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: CreateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: CreateOrUpdateRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: DeleteByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: ExistsRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case r: GetByIdRequest => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A, ERT]]
case _ => self
}
final def routing(value: Routing)(implicit wr: WithRouting[ERT]): ElasticRequest[A, ERT] =
wr.withRouting(request = self, routing = value)
}

object ElasticRequest {

import zio.elasticsearch.ElasticRequestType._
import ElasticRequestType._

def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Create] =
CreateRequest(index, Some(id), Document.from(doc)).map(_ => ())
Expand Down Expand Up @@ -127,7 +120,6 @@ object ElasticRequest {
request: ElasticRequest[A, ERT],
mapper: A => B
) extends ElasticRequest[B, ERT]

}

sealed trait ElasticRequestType
Expand Down
58 changes: 58 additions & 0 deletions modules/library/src/main/scala/zio/elasticsearch/Routing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package zio.elasticsearch

import zio.elasticsearch.ElasticRequest._
import zio.elasticsearch.ElasticRequestType.{Create, DeleteById, Exists, GetById, Upsert}
import zio.prelude.Assertion.isEmptyString
import zio.prelude.Newtype

object Routing extends Newtype[String] {
override def assertion = assert(!isEmptyString) // scalafix:ok

type Routing = Routing.Type

trait WithRouting[ERT <: ElasticRequestType] {
def withRouting[A](request: ElasticRequest[A, ERT], routing: Routing): ElasticRequest[A, ERT]
}

object WithRouting {
implicit val createWithRouting: WithRouting[Create] = new WithRouting[Create] {
def withRouting[A](request: ElasticRequest[A, Create], routing: Routing): ElasticRequest[A, Create] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: CreateRequest => r.copy(routing = Some(routing))
}
}

implicit val deleteByIdWithRouting: WithRouting[DeleteById] = new WithRouting[DeleteById] {
def withRouting[A](request: ElasticRequest[A, DeleteById], routing: Routing): ElasticRequest[A, DeleteById] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: DeleteByIdRequest => r.copy(routing = Some(routing))
}
}

implicit val existsWithRouting: WithRouting[Exists] = new WithRouting[Exists] {
def withRouting[A](request: ElasticRequest[A, Exists], routing: Routing): ElasticRequest[A, Exists] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: ExistsRequest => r.copy(routing = Some(routing))
}
}

implicit val getByIdWithRouting: WithRouting[GetById] = new WithRouting[GetById] {
def withRouting[A](request: ElasticRequest[A, GetById], routing: Routing): ElasticRequest[A, GetById] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: GetByIdRequest => r.copy(routing = Some(routing))
}
}

implicit val upsertWithRouting: WithRouting[Upsert] = new WithRouting[Upsert] {
def withRouting[A](request: ElasticRequest[A, Upsert], routing: Routing): ElasticRequest[A, Upsert] =
request match {
case Map(r, mapper) => Map(withRouting(r, routing), mapper)
case r: CreateOrUpdateRequest => r.copy(routing = Some(routing))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ package zio

import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.StringUtils._
import zio.prelude.Assertion.isEmptyString
import zio.prelude.AssertionError.failure
import zio.prelude.Newtype

package object elasticsearch {
object Routing extends Newtype[String] {
override def assertion = assert(!isEmptyString) // scalafix:ok
}
type Routing = Routing.Type

object DocumentId extends Newtype[String]
type DocumentId = DocumentId.Type

Expand Down

0 comments on commit 73c9366

Please sign in to comment.