Skip to content

Commit

Permalink
chore | add support for multi filter arguments (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-bansal authored Nov 27, 2023
1 parent a4d5d5d commit fd15f6e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ protected void configure() {
new TypeLiteral<
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter>>() {}))
.to(FilterConverter.class);
bind(Key.get(
new TypeLiteral<
Converter<
Collection<Collection<AttributeAssociation<FilterArgument>>>, Filter>>() {}))
.to(MultiFilterConverter.class);

bind(Key.get(new TypeLiteral<Converter<AttributeModel, ColumnIdentifier>>() {}))
.to(ColumnIdentifierConverter.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.hypertrace.core.graphql.utils.gateway;

import static io.reactivex.rxjava3.core.Single.zip;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.hypertrace.core.graphql.common.request.AttributeAssociation;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.core.graphql.common.utils.Converter;
import org.hypertrace.gateway.service.v1.common.Filter;
import org.hypertrace.gateway.service.v1.common.Operator;

public class MultiFilterConverter
implements Converter<Collection<Collection<AttributeAssociation<FilterArgument>>>, Filter> {

private final AttributeExpressionConverter attributeExpressionConverter;
private final OperatorConverter operatorConverter;
private final LiteralConstantExpressionConverter literalConstantExpressionConverter;

@Inject
MultiFilterConverter(
AttributeExpressionConverter attributeExpressionConverter,
OperatorConverter operatorConverter,
LiteralConstantExpressionConverter literalConstantExpressionConverter) {
this.attributeExpressionConverter = attributeExpressionConverter;
this.operatorConverter = operatorConverter;
this.literalConstantExpressionConverter = literalConstantExpressionConverter;
}

@Override
public Single<Filter> convert(
Collection<Collection<AttributeAssociation<FilterArgument>>> filters) {
if (filters.isEmpty()) {
return Single.just(Filter.getDefaultInstance());
}

return Observable.fromIterable(filters)
.flatMapSingle(this::buildAndFilterOperations)
.collect(Collectors.toUnmodifiableList())
.map(
filterList ->
Filter.newBuilder().setOperator(Operator.OR).addAllChildFilter(filterList).build());
}

private Single<Filter> buildAndFilterOperations(
Collection<AttributeAssociation<FilterArgument>> andFilters) {
return Observable.fromIterable(andFilters)
.flatMapSingle(this::buildFilter)
.collect(Collectors.toUnmodifiableList())
.map(
filterList ->
Filter.newBuilder()
.setOperator(Operator.AND)
.addAllChildFilter(filterList)
.build());
}

private Single<Filter> buildFilter(AttributeAssociation<FilterArgument> filter) {
return zip(
this.attributeExpressionConverter.convert(
AttributeAssociation.of(filter.attribute(), filter.value().keyExpression())),
this.operatorConverter.convert(filter.value().operator()),
this.literalConstantExpressionConverter.convert(filter.value().value()),
(key, operator, value) ->
Filter.newBuilder().setLhs(key).setOperator(operator).setRhs(value).build());
}
}

0 comments on commit fd15f6e

Please sign in to comment.