Skip to content

Commit

Permalink
Introduce Newtype
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya committed Dec 1, 2022
1 parent cd0e66a commit cb47584
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ lazy val library =
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"dev.zio" %% "zio-prelude" % "1.0.0-RC16",
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3"
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import sttp.model.StatusCode.Ok
import sttp.model.Uri
import zio.Task
import zio.elasticsearch.ElasticRequest._
import zio.prelude.ZValidation

private[elasticsearch] final class HttpElasticExecutor private (config: ElasticConfig, client: SttpBackend[Task, Any])
extends ElasticExecutor {
Expand All @@ -27,7 +28,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
}

private def executeGetById(r: GetById): Task[Option[Document]] = {
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(_.value))
val uri = uri"$basePath/${r.index}/$Doc/${r.id}"
request
.get(uri)
.response(asJson[ElasticGetResponse])
Expand All @@ -39,19 +40,25 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
private def executeCreate(r: Create): Task[Option[DocumentId]] = {
val uri = r.id match {
case Some(documentId) =>
uri"$basePath/${r.index}/$Create/$documentId".withParam("routing", r.routing.map(_.value))
uri"$basePath/${r.index}/$Create/$documentId"
case None =>
uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(_.value))
uri"$basePath/${r.index}/$Doc"
}

request
.post(uri)
.contentType(ApplicationJson)
.response(asJson[ElasticCreateResponse])
.body(r.document.json)
.send(client)
.map(_.body.toOption)
.map(_.flatMap(body => Some(DocumentId(body.id))))
.map(a =>
a.flatMap { body =>
DocumentId.make(body.id) match {
case ZValidation.Failure(_, _) => None // todo how should we handle things like this?
case ZValidation.Success(_, value) => Some(value)
}
}
)
}

private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] =
Expand All @@ -63,12 +70,12 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
.unit

private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = {
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(_.value))
val uri = uri"$basePath/${r.index}/$Doc/${r.id}"
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))
val uri = uri"$basePath/${r.index}/$Doc/${r.id}"
request.head(uri).send(client).map(_.code.equals(Ok))
}

Expand Down

This file was deleted.

This file was deleted.

20 changes: 20 additions & 0 deletions modules/library/src/main/scala/zio/elasticsearch/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package zio

import zio.prelude.Assertion._
import zio.prelude.{Newtype, Subtype}

package object elasticsearch {
object Routing extends Subtype[String] {
override def assertion = assert {
!isEmptyString
}
}
type Routing = Routing.Type

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

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

}

0 comments on commit cb47584

Please sign in to comment.