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

Replace List with Chunk #215

Merged
merged 2 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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(
Expand Down Expand Up @@ -580,7 +580,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
query = range(TestDocument.dateField).gte(LocalDate.now).format("uuuu-MM-dd").boost(1.0)
res <- Executor.execute(ElasticRequest.search(firstSearchIndex, query)).documentAs[TestDocument]
} yield assert(res)(equalTo(List(secondDocumentUpdated, thirdDocumentUpdated)))
} yield assert(res)(equalTo(Chunk(secondDocumentUpdated, thirdDocumentUpdated)))
}
} @@ around(
Executor.execute(ElasticRequest.createIndex(firstSearchIndex)),
Expand Down Expand Up @@ -1122,7 +1122,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(res)(
equalTo(List(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))
equalTo(Chunk(secondDocumentWithFixedIntField, firstDocumentWithFixedIntField))
)
}
} @@ around(
Expand Down Expand Up @@ -1160,7 +1160,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(res)(
equalTo(List(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField))
equalTo(Chunk(firstDocumentWithFixedIntField, secondDocumentWithFixedIntField))
)
}
} @@ around(
Expand Down Expand Up @@ -1196,7 +1196,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestSubDocument]
} yield assert(res)(
equalTo(List(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList))
equalTo(Chunk(firstSubDocumentWithFixedIntList, secondSubDocumentWithFixedIntList))
)
}
} @@ around(
Expand Down Expand Up @@ -1432,7 +1432,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 Expand Up @@ -1525,7 +1525,7 @@ object HttpExecutorSpec extends IntegrationSpec {
doc3 <- Executor.execute(ElasticRequest.getById(index, thirdDocumentId)).documentAs[TestDocument]
} yield assert(res.items.size)(equalTo(7)) &&
assert(res.items.map(_.error.isDefined))(
equalTo(List(false, false, false, false, false, false, true))
equalTo(Chunk(false, false, false, false, false, false, true))
) &&
assert(res.items(6).status)(equalTo(Some(404))) &&
assert(res.items(6).error.map(_.`type`))(equalTo(Some("document_missing_exception"))) &&
Expand Down Expand Up @@ -1676,7 +1676,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
.documentAs[TestDocument]
} yield assert(r1 ++ r2)(
equalTo(List(document, document))
equalTo(Chunk(document, document))
)
}
} @@ after(Executor.execute(ElasticRequest.deleteIndex(geoDistanceIndex)).orDie)
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 @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ object ElasticAggregation {
* an instance of empty [[zio.elasticsearch.aggregation.MultipleAggregations]].
*/
final def multipleAggregations: MultipleAggregations =
Multiple(aggregations = Nil)
Multiple(aggregations = Chunk.empty)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.TermsAggregation]] using the specified
Expand All @@ -95,7 +95,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: Field[_, String]): TermsAggregation =
Terms(name = name, field = field.toString, order = Chunk.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field.toString, order = Chunk.empty, subAggregations = Chunk.empty, size = None)

/**
* Constructs an instance of [[zio.elasticsearch.aggregation.TermsAggregation]] using the specified parameters.
Expand All @@ -109,7 +109,7 @@ object ElasticAggregation {
* performed.
*/
final def termsAggregation(name: String, field: String): TermsAggregation =
Terms(name = name, field = field, order = Chunk.empty, subAggregations = Nil, size = None)
Terms(name = name, field = field, order = Chunk.empty, subAggregations = Chunk.empty, size = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.aggregation.MaxAggregation]] using the specified parameters.
Expand Down
77 changes: 67 additions & 10 deletions modules/library/src/main/scala/zio/elasticsearch/ElasticQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio.elasticsearch

import zio.Chunk
import zio.elasticsearch.ElasticPrimitive.ElasticPrimitive
import zio.elasticsearch.query._
import zio.schema.Schema
Expand Down Expand Up @@ -93,7 +94,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def filter[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = queries.toList, must = Nil, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.fromIterable(queries),
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must satisfy the criteria using
Expand All @@ -106,7 +114,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def filter(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = queries.toList, must = Nil, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.fromIterable(queries),
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.GeoDistanceQuery]] using the specified parameters.
Expand Down Expand Up @@ -365,7 +380,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def must[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = queries.toList, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.fromIterable(queries),
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must satisfy the criteria using
Expand All @@ -378,7 +400,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def must(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = queries.toList, mustNot = Nil, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.fromIterable(queries),
mustNot = Chunk.empty,
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must not satisfy the criteria
Expand All @@ -393,7 +422,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def mustNot[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = Nil, mustNot = queries.toList, should = Nil, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.fromIterable(queries),
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that must not satisfy the criteria
Expand All @@ -406,7 +442,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def mustNot(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = Nil, mustNot = queries.toList, should = Nil, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.fromIterable(queries),
should = Chunk.empty,
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.NestedQuery]] using the specified parameters.
Expand Down Expand Up @@ -479,7 +522,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def should[S: Schema](queries: ElasticQuery[S]*): BoolQuery[S] =
Bool[S](filter = Nil, must = Nil, mustNot = Nil, should = queries.toList, boost = None, minimumShouldMatch = None)
Bool[S](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.fromIterable(queries),
boost = None,
minimumShouldMatch = None
)

/**
* Constructs an instance of [[zio.elasticsearch.query.BoolQuery]] with queries that should satisfy the criteria using
Expand All @@ -492,7 +542,14 @@ object ElasticQuery {
* satisfy the criteria.
*/
final def should(queries: ElasticQuery[Any]*): BoolQuery[Any] =
Bool[Any](filter = Nil, must = Nil, mustNot = Nil, should = queries.toList, boost = None, minimumShouldMatch = None)
Bool[Any](
filter = Chunk.empty,
must = Chunk.empty,
mustNot = Chunk.empty,
should = Chunk.fromIterable(queries),
boost = None,
minimumShouldMatch = None
)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.WildcardQuery]] using the specified parameters.
Expand Down Expand Up @@ -574,7 +631,7 @@ object ElasticQuery {
* an instance of [[zio.elasticsearch.query.TermsQuery]] that represents the term query to be performed.
*/
final def terms[S](field: Field[S, String], values: String*): Terms[S] =
Terms(field = field.toString, values = values.toList, boost = None)
Terms(field = field.toString, values = Chunk.fromIterable(values), boost = None)

/**
* Constructs an instance of [[zio.elasticsearch.query.TermsQuery]] using the specified parameters.
Expand All @@ -590,7 +647,7 @@ object ElasticQuery {
* an instance of [[zio.elasticsearch.query.TermsQuery]] that represents the term query to be performed.
*/
final def terms(field: String, values: String*): Terms[Any] =
Terms(field = field, values = values.toList, boost = None)
Terms(field = field, values = Chunk.fromIterable(values), boost = None)

/**
* Constructs a type-safe instance of [[zio.elasticsearch.query.WildcardQuery]] using the specified parameters.
Expand Down
Loading