diff --git a/build.sbt b/build.sbt index b8610c4f5..004705672 100644 --- a/build.sbt +++ b/build.sbt @@ -28,6 +28,13 @@ lazy val library = project .in(file("modules/library")) .settings(stdSettings("zio-elasticsearch")) + .settings( + libraryDependencies ++= List( + "dev.zio" %% "zio-json" % "0.3.0", + "dev.zio" %% "zio-schema" % "0.3.1", + "dev.zio" %% "zio-schema-json" % "0.3.1" + ) + ) lazy val example = project diff --git a/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala b/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala new file mode 100644 index 000000000..a606cacda --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala @@ -0,0 +1,3 @@ +package zio.elasticsearch + +final case class DocumentId(value: String) extends AnyVal diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala new file mode 100644 index 000000000..f6a84b4ed --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala @@ -0,0 +1,16 @@ +package zio.elasticsearch + +sealed trait ElasticError + +object ElasticError { + + sealed abstract class DocumentRetrievingError + + object DocumentRetrievingError { + + final case object DocumentNotFound extends DocumentRetrievingError + + final case class DecoderError(reason: String) extends DocumentRetrievingError + + } +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala new file mode 100644 index 000000000..33f3a4d8f --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -0,0 +1,33 @@ +package zio.elasticsearch + +import zio.elasticsearch.ElasticError.DocumentRetrievingError._ +import zio.elasticsearch.ElasticError._ +import zio.schema.Schema + +sealed trait ElasticRequest[+A] { self => + final def map[B](f: A => B): ElasticRequest[B] = ElasticRequest.Map(self, f) + +} + +object ElasticRequest { + + private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B) + extends ElasticRequest[B] + + def getById[A: Schema]( + index: IndexName, + id: DocumentId, + routing: Option[Routing] = None + ): ElasticRequest[Either[DocumentRetrievingError, A]] = + GetById(index, id, routing).map { + case Some(document) => document.decode.left.map(err => DecoderError(err.message)) + case None => Left(DocumentNotFound) + } + + private[elasticsearch] final case class GetById( + index: IndexName, + id: DocumentId, + routing: Option[Routing] = None + ) extends ElasticRequest[Option[Document]] + +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala b/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala new file mode 100644 index 000000000..fe43660c2 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/IndexName.scala @@ -0,0 +1,3 @@ +package zio.elasticsearch + +final case class IndexName(name: String) extends AnyVal diff --git a/modules/library/src/main/scala/zio/elasticsearch/Routing.scala b/modules/library/src/main/scala/zio/elasticsearch/Routing.scala new file mode 100644 index 000000000..c0b496050 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/Routing.scala @@ -0,0 +1,3 @@ +package zio.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..c948a18b3 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/package.scala @@ -0,0 +1,11 @@ +package zio + +import zio.schema.Schema +import zio.schema.codec.DecodeError +import zio.schema.codec.JsonCodec.JsonDecoder + +package object elasticsearch { + private[elasticsearch] final case class Document(json: String) { + def decode[A](implicit schema: Schema[A]): Either[DecodeError, A] = JsonDecoder.decode(schema, json) + } +}