From b0661d2dde558ce9b3342863e5c93a173254f597 Mon Sep 17 00:00:00 2001 From: mvelimir Date: Thu, 11 May 2023 16:36:53 +0200 Subject: [PATCH] Convert remaining Lists to Chunks --- .../example/RepositoriesElasticsearch.scala | 12 ++++++------ .../src/main/scala/example/api/Criteria.scala | 3 ++- .../example/external/github/RepoFetcher.scala | 8 ++++---- .../zio/elasticsearch/HttpExecutorSpec.scala | 10 +++++----- .../zio/elasticsearch/IndexNameValidation.scala | 5 +++-- .../zio/elasticsearch/IndexNameValidator.scala | 5 +++-- .../zio/elasticsearch/result/ElasticResult.scala | 16 ++++++++-------- .../scala/zio/elasticsearch/result/Item.scala | 8 ++++---- .../elasticsearch/HttpElasticExecutorSpec.scala | 4 ++-- 9 files changed, 37 insertions(+), 34 deletions(-) diff --git a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala index 28ab974da..dfc343592 100644 --- a/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala +++ b/modules/example/src/main/scala/example/RepositoriesElasticsearch.scala @@ -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]] = @@ -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( @@ -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] = @@ -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]] = @@ -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] = @@ -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] = diff --git a/modules/example/src/main/scala/example/api/Criteria.scala b/modules/example/src/main/scala/example/api/Criteria.scala index 86a76b5e1..ea0ab65aa 100644 --- a/modules/example/src/main/scala/example/api/Criteria.scala +++ b/modules/example/src/main/scala/example/api/Criteria.scala @@ -16,6 +16,7 @@ package example.api +import zio.Chunk import zio.schema.{DeriveSchema, Schema} import java.time.LocalDateTime @@ -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 diff --git a/modules/example/src/main/scala/example/external/github/RepoFetcher.scala b/modules/example/src/main/scala/example/external/github/RepoFetcher.scala index 0b9a6ee1f..a35d4d046 100644 --- a/modules/example/src/main/scala/example/external/github/RepoFetcher.scala +++ b/modules/example/src/main/scala/example/external/github/RepoFetcher.scala @@ -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) } diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index ce84f5dea..8af7a4446 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -315,7 +315,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( @@ -1111,7 +1111,7 @@ object HttpExecutorSpec extends IntegrationSpec { ) .documentAs[TestDocument] } yield assert(res)( - equalTo(List(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField)) + equalTo(Chunk(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField)) ) } } @@ around( @@ -1149,7 +1149,7 @@ object HttpExecutorSpec extends IntegrationSpec { ) .documentAs[TestDocument] } yield assert(res)( - equalTo(List(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField)) + equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField)) ) } } @@ around( @@ -1185,7 +1185,7 @@ object HttpExecutorSpec extends IntegrationSpec { ) .documentAs[TestSubDocument] } yield assert(res)( - equalTo(List(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList)) + equalTo(Chunk(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList)) ) } } @@ around( @@ -1421,7 +1421,7 @@ object HttpExecutorSpec extends IntegrationSpec { ) .documentAs[TestDocument] } yield assert(res2.map(_.intField))( - equalTo((20 to 29).toList) + equalTo(Chunk.fromIterable(20 to 29)) ) } } @@ around( diff --git a/modules/library/src/main/scala-2/zio/elasticsearch/IndexNameValidation.scala b/modules/library/src/main/scala-2/zio/elasticsearch/IndexNameValidation.scala index bb5a88a66..83871ab2b 100644 --- a/modules/library/src/main/scala-2/zio/elasticsearch/IndexNameValidation.scala +++ b/modules/library/src/main/scala-2/zio/elasticsearch/IndexNameValidation.scala @@ -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 } diff --git a/modules/library/src/main/scala-3/zio/elasticsearch/IndexNameValidator.scala b/modules/library/src/main/scala-3/zio/elasticsearch/IndexNameValidator.scala index c72f9a927..c1dd7b795 100644 --- a/modules/library/src/main/scala-3/zio/elasticsearch/IndexNameValidator.scala +++ b/modules/library/src/main/scala-3/zio/elasticsearch/IndexNameValidator.scala @@ -18,17 +18,18 @@ package zio.elasticsearch import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.StringUtils.{equalsAny, startsWithAny} +import zio.Chunk 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 diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala index 885f56b46..d48edc95a 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/result/ElasticResult.scala @@ -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) @@ -81,7 +81,7 @@ 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)) @@ -89,16 +89,16 @@ final class SearchAndAggregateResult private[elasticsearch] ( 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) diff --git a/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala b/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala index 46d7b8f35..aef0732a9 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/result/Item.scala @@ -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(", ")}") diff --git a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala index 4705fcdfd..ea7884352 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/HttpElasticExecutorSpec.scala @@ -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))))) )