-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not return unwanted empty buckets (#674)
* remove AnyFunSpec from base class * remove AnyFunSpec from base class * commit missed file * Do not return unwanted empty buckets * Do not return unwanted empty buckets * extend no-empties rule to images * avoid naming collision * better naming, as per review
- Loading branch information
1 parent
4fbbbb0
commit d4f62ab
Showing
19 changed files
with
905 additions
and
65 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
search/src/main/scala/weco/api/search/models/DocumentFilter.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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
search/src/main/scala/weco/api/search/models/display/FilterBucketMatcher.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,87 @@ | ||
package weco.api.search.models.display | ||
|
||
import io.circe.Json | ||
import weco.api.search.models.{ | ||
AvailabilitiesFilter, | ||
ContributorsFilter, | ||
DocumentFilter, | ||
FormatFilter, | ||
GenreFilter, | ||
LanguagesFilter, | ||
LicenseFilter, | ||
SubjectLabelFilter | ||
} | ||
|
||
trait FilterAggregationMatcher { | ||
def matchBucket(bucketData: Json): Boolean | ||
} | ||
|
||
case class AggregationDataLabelInFilter(labels: Seq[String]) | ||
extends FilterAggregationMatcher { | ||
def matchBucket(bucketData: Json): Boolean = | ||
bucketData.hcursor.get[String]("label") match { | ||
case Right(value) => labels.contains(value) | ||
case _ => false | ||
} | ||
} | ||
|
||
case class AggregationDataIdInFilter(labels: Seq[String]) | ||
extends FilterAggregationMatcher { | ||
def matchBucket(bucketData: Json): Boolean = | ||
bucketData.hcursor.get[String]("id") match { | ||
case Right(value) => labels.contains(value) | ||
case _ => false | ||
} | ||
} | ||
|
||
case class NeverAggregationMatcher() extends FilterAggregationMatcher { | ||
def matchBucket(bucketData: Json): Boolean = | ||
false | ||
} | ||
|
||
sealed trait FilterWithMatchingAggregation | ||
case object FormatFilterAgg extends FilterWithMatchingAggregation | ||
case object LanguagesFilterAgg extends FilterWithMatchingAggregation | ||
case object GenreFilterAgg extends FilterWithMatchingAggregation | ||
case object SubjectLabelFilterAgg extends FilterWithMatchingAggregation | ||
case object ContributorsFilterAgg extends FilterWithMatchingAggregation | ||
case object LicenseFilterAgg extends FilterWithMatchingAggregation | ||
case object AvailabilitiesFilterAgg extends FilterWithMatchingAggregation | ||
|
||
class FilterBucketMatcher( | ||
filters: Map[FilterWithMatchingAggregation, FilterAggregationMatcher] | ||
) { | ||
|
||
def matchBucket( | ||
aggregationType: FilterWithMatchingAggregation | ||
)(bucketData: Json): Boolean = | ||
filters | ||
.getOrElse(aggregationType, NeverAggregationMatcher()) | ||
.matchBucket(bucketData) | ||
} | ||
|
||
object FilterBucketMatcher { | ||
def apply(filters: Seq[DocumentFilter]) = | ||
new FilterBucketMatcher( | ||
filters collect { | ||
case FormatFilter(ids) => | ||
FormatFilterAgg -> AggregationDataIdInFilter(ids) | ||
case LanguagesFilter(ids) => | ||
LanguagesFilterAgg -> AggregationDataIdInFilter(ids) | ||
case GenreFilter(labels) => | ||
GenreFilterAgg -> AggregationDataLabelInFilter(labels) | ||
case SubjectLabelFilter(labels) => | ||
SubjectLabelFilterAgg -> | ||
AggregationDataLabelInFilter(labels) | ||
case ContributorsFilter(labels) => | ||
ContributorsFilterAgg -> | ||
AggregationDataLabelInFilter(labels) | ||
case LicenseFilter(ids) => | ||
LicenseFilterAgg -> AggregationDataIdInFilter(ids) | ||
case AvailabilitiesFilter(availabilityIds) => | ||
AvailabilitiesFilterAgg -> | ||
AggregationDataIdInFilter(availabilityIds) | ||
} toMap | ||
) | ||
|
||
} |
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
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
Oops, something went wrong.