Skip to content

Commit

Permalink
(dsl): Support GetById request (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmarjanovic authored Nov 28, 2022
1 parent 8d5700b commit 5f40fda
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package zio.elasticsearch

final case class DocumentId(value: String) extends AnyVal
Original file line number Diff line number Diff line change
@@ -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

}
}
Original file line number Diff line number Diff line change
@@ -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]]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package zio.elasticsearch

final case class IndexName(name: String) extends AnyVal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package zio.elasticsearch

final case class Routing(value: String) extends AnyVal
11 changes: 11 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,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)
}
}

0 comments on commit 5f40fda

Please sign in to comment.