Skip to content

Commit

Permalink
Convert remaining Lists to Chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir committed May 11, 2023
1 parent 504b9b5 commit c90e5f2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import zio.elasticsearch.request.{CreationOutcome, DeletionOutcome}

final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {

def findAll(): Task[List[GitHubRepo]] =
def findAll(): Task[Chunk[GitHubRepo]] =
elasticsearch.execute(ElasticRequest.search(Index, matchAll)).documentAs[GitHubRepo]

def findById(organization: String, id: String): Task[Option[GitHubRepo]] =
Expand All @@ -43,7 +43,7 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {
)
} yield res

def createAll(repositories: List[GitHubRepo]): Task[Unit] =
def createAll(repositories: Chunk[GitHubRepo]): Task[Unit] =
for {
routing <- routingOf(organization)
_ <- elasticsearch.execute(
Expand All @@ -69,7 +69,7 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {
res <- elasticsearch.execute(ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing).refreshFalse)
} yield res

def search(query: ElasticQuery[_], from: Int, size: Int): Task[List[GitHubRepo]] =
def search(query: ElasticQuery[_], from: Int, size: Int): Task[Chunk[GitHubRepo]] =
elasticsearch.execute(ElasticRequest.search(Index, query).from(from).size(size)).documentAs[GitHubRepo]

private def routingOf(value: String): IO[IllegalArgumentException, Routing.Type] =
Expand All @@ -79,7 +79,7 @@ final case class RepositoriesElasticsearch(elasticsearch: Elasticsearch) {

object RepositoriesElasticsearch {

def findAll(): RIO[RepositoriesElasticsearch, List[GitHubRepo]] =
def findAll(): RIO[RepositoriesElasticsearch, Chunk[GitHubRepo]] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.findAll())

def findById(organization: String, id: String): RIO[RepositoriesElasticsearch, Option[GitHubRepo]] =
Expand All @@ -88,7 +88,7 @@ object RepositoriesElasticsearch {
def create(repository: GitHubRepo): RIO[RepositoriesElasticsearch, CreationOutcome] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.create(repository))

def createAll(repositories: List[GitHubRepo]): RIO[RepositoriesElasticsearch, Unit] =
def createAll(repositories: Chunk[GitHubRepo]): RIO[RepositoriesElasticsearch, Unit] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.createAll(repositories))

def upsert(id: String, repository: GitHubRepo): RIO[RepositoriesElasticsearch, Unit] =
Expand All @@ -97,7 +97,7 @@ object RepositoriesElasticsearch {
def remove(organization: String, id: String): RIO[RepositoriesElasticsearch, DeletionOutcome] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.remove(organization, id))

def search(query: ElasticQuery[_], from: Int, size: Int): RIO[RepositoriesElasticsearch, List[GitHubRepo]] =
def search(query: ElasticQuery[_], from: Int, size: Int): RIO[RepositoriesElasticsearch, Chunk[GitHubRepo]] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.search(query, from, size))

lazy val live: URLayer[Elasticsearch, RepositoriesElasticsearch] =
Expand Down
3 changes: 2 additions & 1 deletion modules/example/src/main/scala/example/api/Criteria.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package example.api

import zio.Chunk
import zio.schema.{DeriveSchema, Schema}

import java.time.LocalDateTime
Expand All @@ -26,7 +27,7 @@ object Criteria {
implicit val schema: Schema[Criteria] = DeriveSchema.gen[Criteria]
}

final case class CompoundCriteria(operator: CompoundOperator, filters: List[Criteria]) extends Criteria
final case class CompoundCriteria(operator: CompoundOperator, filters: Chunk[Criteria]) extends Criteria

final case class DateCriteria(field: DateFilter, operator: FilterOperator, value: LocalDateTime) extends Criteria

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ import example.GitHubRepo
import example.external.github.model.RepoResponse
import sttp.client3.{SttpBackend, UriContext, basicRequest}
import zio.json.DecoderOps
import zio.{RIO, Task, ZIO}
import zio.{Chunk, RIO, Task, ZIO}

object RepoFetcher {

def fetchAllByOrganization(
organization: String,
limit: Int = 100
): RIO[SttpBackend[Task, Any], List[GitHubRepo]] =
): RIO[SttpBackend[Task, Any], Chunk[GitHubRepo]] =
for {
client <- ZIO.service[SttpBackend[Task, Any]]
res <- basicRequest.get(uri"https://api.github.com/orgs/$organization/repos?per_page=$limit").send(client)
} yield res.body.toOption
.map(_.fromJson[List[RepoResponse]].fold(_ => Nil, _.map(GitHubRepo.fromResponse).toList))
.getOrElse(Nil)
.map(_.fromJson[Chunk[RepoResponse]].fold(_ => Chunk.empty, _.map(GitHubRepo.fromResponse)))
.getOrElse(Chunk.empty)
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
docs <- res.documentAs[TestDocument]
aggs <- res.aggregations
} yield assert(docs)(equalTo(List(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))) &&
} yield assert(docs)(equalTo(Chunk(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))) &&
assert(aggs)(isNonEmpty)
}
} @@ around(
Expand Down Expand Up @@ -1068,7 +1068,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(res)(
equalTo(List(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))
equalTo(Chunk(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))
)
}
} @@ around(
Expand Down Expand Up @@ -1106,7 +1106,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(res)(
equalTo(List(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField))
equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField))
)
}
} @@ around(
Expand Down Expand Up @@ -1142,7 +1142,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestSubDocument]
} yield assert(res)(
equalTo(List(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList))
equalTo(Chunk(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList))
)
}
} @@ around(
Expand Down Expand Up @@ -1378,7 +1378,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(res2.map(_.intField))(
equalTo((20 to 29).toList)
equalTo(Chunk.fromIterable(20 to 29))
)
}
} @@ around(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package zio.elasticsearch

import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.StringUtils.{equalsAny, startsWithAny}
import zio.Chunk

object IndexNameValidation {
def isValid(name: String): Boolean = {
def containsAny(string: String, params: List[String]): Boolean =
def containsAny(string: String, params: Chunk[String]): Boolean =
params.exists(StringUtils.contains(string, _))

name.toLowerCase == name &&
!startsWithAny(name, "+", "-", "_") &&
!containsAny(string = name, params = List("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!containsAny(string = name, params = Chunk("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!equalsAny(name, ".", "..") &&
name.getBytes().length <= 255
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import zio.prelude.{AssertionError, Validator}

object IndexNameValidator
extends Validator[String](name => {
def containsAny(string: String, params: List[String]): Boolean =
def containsAny(string: String, params: Chunk[String]): Boolean =
params.exists(StringUtils.contains(string, _))

def isValid(name: String): Boolean =
name.toLowerCase == name &&
!startsWithAny(name, "+", "-", "_") &&
!containsAny(string = name, params = List("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!containsAny(string = name, params = Chunk("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!equalsAny(name, ".", "..") &&
name.getBytes().length <= 255

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ final class GetResult private[elasticsearch] (private val doc: Option[Item]) ext
final class SearchResult private[elasticsearch] (
private val hits: Chunk[Item],
private val fullResponse: SearchWithAggregationsResponse
) extends DocumentResult[List] {
def documentAs[A: Schema]: IO[DecodingException, List[A]] =
) extends DocumentResult[Chunk] {
def documentAs[A: Schema]: IO[DecodingException, Chunk[A]] =
ZIO.fromEither {
ZValidation.validateAll(hits.map(item => ZValidation.fromEither(item.documentAs)).toList).toEitherWith { errors =>
ZValidation.validateAll(hits.map(item => ZValidation.fromEither(item.documentAs))).toEitherWith { errors =>
DecodingException(s"Could not parse all documents successfully: ${errors.map(_.message).mkString(", ")}")
}
}

lazy val items: UIO[List[Item]] = ZIO.succeed(hits.toList)
lazy val items: UIO[Chunk[Item]] = ZIO.succeed(hits)

lazy val lastSortValue: UIO[Option[Json]] = ZIO.succeed(fullResponse.lastSortField)

Expand All @@ -81,24 +81,24 @@ final class SearchAndAggregateResult private[elasticsearch] (
private val hits: Chunk[Item],
private val aggs: Map[String, AggregationResponse],
private val fullResponse: SearchWithAggregationsResponse
) extends DocumentResult[List]
) extends DocumentResult[Chunk]
with AggregationsResult {
def aggregation(name: String): Task[Option[AggregationResponse]] =
ZIO.succeed(aggs.get(name))

def aggregations: Task[Map[String, AggregationResponse]] =
ZIO.succeed(aggs)

def documentAs[A: Schema]: Task[List[A]] =
def documentAs[A: Schema]: Task[Chunk[A]] =
ZIO.fromEither {
ZValidation.validateAll(hits.map(item => ZValidation.fromEither(item.documentAs)).toList).toEitherWith { errors =>
ZValidation.validateAll(hits.map(item => ZValidation.fromEither(item.documentAs))).toEitherWith { errors =>
DecodingException(
s"Could not parse all documents successfully: ${errors.map(_.message).mkString(",")})"
)
}
}

lazy val items: UIO[List[Item]] = ZIO.succeed(hits.toList)
lazy val items: UIO[Chunk[Item]] = ZIO.succeed(hits)

lazy val lastSortValue: UIO[Option[Json]] = ZIO.succeed(fullResponse.lastSortField)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ final case class Item(
def highlight(field: Field[_, _]): Option[Chunk[String]] =
highlight(field.toString)

def innerHitAs[A](name: String)(implicit schema: Schema[A]): Either[DecodingException, List[A]] =
def innerHitAs[A](name: String)(implicit schema: Schema[A]): Either[DecodingException, Chunk[A]] =
for {
innerHitsJson <- innerHits.get(name).toRight(DecodingException(s"Could not find inner hits with name $name"))
innerHits <-
Validation
.validateAll(
innerHitsJson
.map(json => Validation.fromEither(JsonDecoder.decode(schema, json.toString)).mapError(_.message))
.toList
innerHitsJson.map(json =>
Validation.fromEither(JsonDecoder.decode(schema, json.toString)).mapError(_.message)
)
)
.toEitherWith(errors =>
DecodingException(s"Could not parse all documents successfully: ${errors.mkString(", ")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ object HttpElasticExecutorSpec extends SttpBackendStubSpec {
Executor
.execute(ElasticRequest.search(index = index, query = matchAll))
.documentAs[TestDocument]
)(equalTo(List(doc)))
)(equalTo(Chunk(doc)))
},
test("search with aggregation request") {
val terms = termsAggregation(name = "aggregation1", field = "name")
val req = Executor
.execute(ElasticRequest.search(index = index, query = matchAll, terms))
assertZIO(req.documentAs[TestDocument])(equalTo(List(doc))) &&
assertZIO(req.documentAs[TestDocument])(equalTo(Chunk(doc))) &&
assertZIO(req.aggregations)(
equalTo(Map("aggregation1" -> TermsAggregationResponse(0, 0, Chunk(TermsAggregationBucket("name", 5, None)))))
)
Expand Down

0 comments on commit c90e5f2

Please sign in to comment.