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

(dsl): Support GetById request #5

Merged
merged 13 commits into from
Nov 28, 2022
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,14 @@
package zio.elasticsearch

object ElasticError {

sealed abstract class DocumentGettingError
Copy link
Member Author

Choose a reason for hiding this comment

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

What do you think about having only sealed trait ElasticError at the moment?

final case object DocumentNotFound extends ElasticError

final case class DecoderError(reason: String) extends ElasticError

We should consider this because we don't have a lot of errors, and maybe it's too early to introduce several layers of error types.


object DocumentGettingError {

case object DocumentNotFound extends DocumentGettingError

case class JsonDecoderError(errorMsg: String) extends DocumentGettingError
Copy link
Member Author

Choose a reason for hiding this comment

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

  • I prefer final case class.
  • Can you use message or reason instead?


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

import zio.elasticsearch.ElasticError._
import zio.elasticsearch.ElasticError.DocumentGettingError._
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)
arnoldlacko marked this conversation as resolved.
Show resolved Hide resolved
extends ElasticRequest[B]

def getById[A: Schema](
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
): ElasticRequest[Either[DocumentGettingError, A]] =
GetById(index, id, routing).map {
case Some(document) => document.decode.left.map(err => JsonDecoderError(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)
}
}