-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d5700b
commit 5f40fda
Showing
7 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
modules/library/src/main/scala/zio/elasticsearch/DocumentId.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
16 changes: 16 additions & 0 deletions
16
modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
modules/library/src/main/scala/zio/elasticsearch/IndexName.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
modules/library/src/main/scala/zio/elasticsearch/Routing.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
modules/library/src/main/scala/zio/elasticsearch/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |