Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Newtype instead of AnyVal #14

Merged
merged 11 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer sorting those by alphabet. So, you can order all of those that way.

)
)

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(())
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

type IndexName = IndexName.Type

}