From cb47584aba59eb79e3fbb8fe22a5ffe722b37fa8 Mon Sep 17 00:00:00 2001 From: markaya Date: Wed, 30 Nov 2022 10:26:01 +0100 Subject: [PATCH] Introduce Newtype --- build.sbt | 1 + .../scala/zio/elasticsearch/DocumentId.scala | 5 ----- .../elasticsearch/HttpElasticExecutor.scala | 21 ++++++++++++------- .../scala/zio/elasticsearch/IndexName.scala | 5 ----- .../scala/zio/elasticsearch/Routing.scala | 3 --- .../scala/zio/elasticsearch/package.scala | 20 ++++++++++++++++++ 6 files changed, 35 insertions(+), 20 deletions(-) delete mode 100644 modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala delete mode 100644 modules/library/src/main/scala/zio/elasticsearch/IndexName.scala delete mode 100644 modules/library/src/main/scala/zio/elasticsearch/Routing.scala create mode 100644 modules/library/src/main/scala/zio/elasticsearch/package.scala diff --git a/build.sbt b/build.sbt index d36f40cfe..c31516cbb 100644 --- a/build.sbt +++ b/build.sbt @@ -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" ) diff --git a/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala b/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala deleted file mode 100644 index 9f1b246d9..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala +++ /dev/null @@ -1,5 +0,0 @@ -package zio.elasticsearch - -final case class DocumentId(value: String) extends AnyVal { - override def toString: String = value -} diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 788223b83..f48882247 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -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 { @@ -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]) @@ -39,11 +40,10 @@ 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) @@ -51,7 +51,14 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC .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] = @@ -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)) } diff --git a/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala b/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala deleted file mode 100644 index a461fd766..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala +++ /dev/null @@ -1,5 +0,0 @@ -package zio.elasticsearch - -final case class IndexName(name: String) extends AnyVal { - override def toString: String = name -} diff --git a/modules/library/src/main/scala/zio/elasticsearch/Routing.scala b/modules/library/src/main/scala/zio/elasticsearch/Routing.scala deleted file mode 100644 index 8a6ef447e..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/Routing.scala +++ /dev/null @@ -1,3 +0,0 @@ -package zio.elasticsearch - -private[elasticsearch] final case class Routing(value: String) extends AnyVal diff --git a/modules/library/src/main/scala/zio/elasticsearch/package.scala b/modules/library/src/main/scala/zio/elasticsearch/package.scala new file mode 100644 index 000000000..7557bbcec --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/package.scala @@ -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 + +}