Skip to content

Commit

Permalink
Add assertion for IndexName
Browse files Browse the repository at this point in the history
  • Loading branch information
markaya committed Dec 1, 2022
1 parent 25db77d commit 9dd4f66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ sealed trait ElasticRequest[+A] { self =>

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

// todo: Error for Routing.make Left
final def routing(value: String): 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: 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 = Routing.make(value).toOption).asInstanceOf[ElasticRequest[A]]
case r: CreateOrUpdate => r.copy(routing = Routing.make(value).toOption).asInstanceOf[ElasticRequest[A]]
case r: Exists => r.copy(routing = Routing.make(value).toOption).asInstanceOf[ElasticRequest[A]]
case r: GetById => r.copy(routing = Routing.make(value).toOption).asInstanceOf[ElasticRequest[A]]
case _ => self
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,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
38 changes: 33 additions & 5 deletions modules/library/src/main/scala/zio/elasticsearch/package.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package zio

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

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

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

object IndexName extends Newtype[String]
object IndexName extends Newtype[String] {
override def assertion: QuotedAssertion[String] = assertCustom { (x: String) =>
if (x.toLowerCase != x) Left(AssertionError.Failure("IndexName must be lower case only."))
else if (x.startsWith("-") || x.startsWith("+") || x.startsWith("_"))
Left(AssertionError.Failure("IndexName cannot start with -, _, +."))
else if (x.exists(char => raw"""\/*?"<>|,#""".contains(char)) || x.contains(' '))
Left(
AssertionError.Failure(
"IndexName cannot include \\, /, *, ?, \", <, >, |, ` ` (space character), ,(comma), #"
)
)
else if (x.contains(':'))
Left(AssertionError.Failure("""IndexName cannot include ":"(since 7.0)."""))
else if (x == "." || x == "..")
Left(AssertionError.Failure("""IndexName cannot be . or .."""))
else if (x.getBytes().length > 255)
Left(
AssertionError.Failure(
"""IndexName cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)"""
)
)
else if (x.startsWith(".")) {
// todo: Warning should be that IndexNames starting with . are deprecated?
Right(())
} else
Right(())
}
}
type IndexName = IndexName.Type

}

0 comments on commit 9dd4f66

Please sign in to comment.