Skip to content

Commit

Permalink
Introduce Newtype instead of AnyVal (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya authored Dec 2, 2022
1 parent 1366add commit 5221b76
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 30 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ lazy val library =
.settings(scalacOptions += "-language:higherKinds")
.settings(
libraryDependencies ++= List(
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3",
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-prelude" % "1.0.0-RC16",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3"
"org.apache.commons" % "commons-lang3" % "3.12.0"
)
)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ sealed trait ElasticRequest[+A] { self =>

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

final def routing(value: String): ElasticRequest[A] =
final def routing(value: Routing): 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: DeleteById => 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 r: Create => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: CreateOrUpdate => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: DeleteById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: Exists => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case r: GetById => r.copy(routing = Some(value)).asInstanceOf[ElasticRequest[A]]
case _ => self
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,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}".withParam("routing", r.routing.map(Routing.unwrap))
request
.get(uri)
.response(asJson[ElasticGetResponse])
Expand All @@ -40,9 +40,9 @@ 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".withParam("routing", r.routing.map(Routing.unwrap))
case None =>
uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(_.value))
uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap))
}

request
Expand All @@ -52,7 +52,7 @@ 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(_.flatMap(body => DocumentId.make(body.id).toOption))
}

private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] =
Expand All @@ -64,21 +64,20 @@ 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}".withParam("routing", r.routing.map(Routing.unwrap))
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}".withParam("routing", r.routing.map(Routing.unwrap))
request.head(uri).send(client).map(_.code.equals(Ok))
}

private def executeDeleteIndex(r: DeleteIndex): Task[Unit] =
request.delete(uri"$basePath/${r.name}").send(client).unit

private def executeDeleteById(deleteById: DeleteById): Task[Option[Unit]] = {
val uri =
uri"$basePath/${deleteById.index}/$Doc/${deleteById.id}".withParam("routing", deleteById.routing.map(_.value))
private def executeDeleteById(r: DeleteById): Task[Option[Unit]] = {
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))
request
.delete(uri)
.response(asJson[ElasticDeleteResponse])
Expand Down

This file was deleted.

This file was deleted.

45 changes: 45 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,45 @@
package zio

import org.apache.commons.lang3.StringUtils._
import zio.prelude.Assertion._
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

object IndexName extends Newtype[String] {
override def assertion = assertCustom { (name: String) => // scalafix:ok
if (
name.toLowerCase != name ||
startsWithAny(name, "+", "-", "_") ||
containsAny(name, '\\', '/', '*', '?', '"', '/', '<', '>', '|', ' ', ',', '#', ':') ||
equalsAny(name, ".", "..") ||
name.getBytes().length > 255
)
Left(
failure(
"""Index names must meet the following criteria:
| - Must be lower case only
| - Cannot include \\, /, *, ?, ", <, >, |, ` `(space character), `,`(comma), #.
| - Cannot include ":"(since 7.0).
| - Cannot start with -, _, +.
| - Cannot be `.` or `..`.
| - Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster).
| - Names starting with . are deprecated, except for hidden indices and internal indices managed by plugins.
|""".stripMargin
)
)
else
Right(())
}
}
type IndexName = IndexName.Type

}

0 comments on commit 5221b76

Please sign in to comment.