Skip to content

Commit

Permalink
Replace EntityEncoderFilter with per-entity object read filter #501
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
andrus committed Nov 28, 2021
1 parent 824cdfb commit 7dcf992
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.agrest.cayenne.compiler;

import io.agrest.compiler.AnnotationsAgEntityBuilder;
import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.meta.AgAttribute;
import io.agrest.meta.AgDataMap;
import io.agrest.meta.AgEntity;
Expand Down Expand Up @@ -205,7 +205,7 @@ protected AgEntity<T> buildEntity() {
rootDataResolver != null ? rootDataResolver : ThrowingRootDataResolver.getInstance(),

// TODO: support Exp filters via annotations?
ObjectFilter.allowsAllFilter());
SelectFilter.allowsAllFilter());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.agrest.cayenne.cayenne.main.E4;
import io.agrest.cayenne.unit.AgCayenneTester;
import io.agrest.cayenne.unit.DbTest;
import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.meta.AgEntity;
import io.bootique.junit5.BQTestTool;
import org.apache.cayenne.Cayenne;
Expand All @@ -21,7 +21,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

public class GET_ObjectFilterIT extends DbTest {
public class GET_SelectFilterIT extends DbTest {

@BQTestTool
static final AgCayenneTester tester = tester(Resource.class)
Expand All @@ -33,11 +33,11 @@ public class GET_ObjectFilterIT extends DbTest {
)
.build();

static <T extends DataObject> ObjectFilter<T> evenFilter() {
static <T extends DataObject> SelectFilter<T> evenFilter() {
return o -> Cayenne.intPKForObject(o) % 2 == 0;
}

static <T extends DataObject> ObjectFilter<T> oddFilter() {
static <T extends DataObject> SelectFilter<T> oddFilter() {
return o -> Cayenne.intPKForObject(o) % 2 != 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GET_ObjectFilter_EntityEncoderFilterIT extends DbTest {
public class GET_SelectFilter_EntityEncoderFilterIT extends DbTest {

@BQTestTool
static final AgCayenneTester tester = tester(Resource.class)
Expand All @@ -36,8 +36,8 @@ public class GET_ObjectFilter_EntityEncoderFilterIT extends DbTest {
static EntityEncoderFilter evenFilter() {
return EntityEncoderFilter
.forEntity(E4.class)
.objectCondition(GET_ObjectFilter_EntityEncoderFilterIT::willEncodeEven)
.encoder(GET_ObjectFilter_EntityEncoderFilterIT::encodeEven)
.objectCondition(GET_SelectFilter_EntityEncoderFilterIT::willEncodeEven)
.encoder(GET_SelectFilter_EntityEncoderFilterIT::encodeEven)
.build();
}

Expand Down
3 changes: 2 additions & 1 deletion agrest-engine/src/main/java/io/agrest/ResourceEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.agrest.base.protocol.Exp;
import io.agrest.base.protocol.Sort;
import io.agrest.encoder.EntityEncoderFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.meta.AgAttribute;
import io.agrest.meta.AgEntity;

Expand Down Expand Up @@ -233,7 +234,7 @@ public boolean isFiltered() {

/**
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
public List<EntityEncoderFilter> getEntityEncoderFilters() {
Expand Down
6 changes: 3 additions & 3 deletions agrest-engine/src/main/java/io/agrest/SelectBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.agrest.constraints.Constraint;
import io.agrest.encoder.Encoder;
import io.agrest.encoder.EntityEncoderFilter;
import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.PropertyFilter;
import io.agrest.meta.AgEntity;
import io.agrest.meta.AgEntityOverlay;
Expand Down Expand Up @@ -46,7 +46,7 @@ public interface SelectBuilder<T> {
* @return this builder instance
* @see AgBuilder#entityEncoderFilter(EntityEncoderFilter)
* @since 3.4
* @deprecated since 4.8 in favor of {@link #filter(Class, ObjectFilter)}.
* @deprecated since 4.8 in favor of {@link #filter(Class, SelectFilter)}.
*/
@Deprecated
SelectBuilder<T> entityEncoderFilter(EntityEncoderFilter filter);
Expand Down Expand Up @@ -117,7 +117,7 @@ default <A> SelectBuilder<T> propFilter(Class<A> entityType, PropertyFilter filt
* @return this builder instance
* @since 4.8
*/
default <A> SelectBuilder<T> filter(Class<A> entityType, ObjectFilter<A> filter) {
default <A> SelectBuilder<T> filter(Class<A> entityType, SelectFilter<A> filter) {
return entityOverlay(AgEntity.overlay(entityType).selectFilter(filter));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.agrest.annotation.AgRelationship;
import io.agrest.base.reflect.BeanAnalyzer;
import io.agrest.base.reflect.PropertyGetter;
import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.meta.*;
import io.agrest.resolver.ReaderBasedResolver;
import io.agrest.resolver.RootDataResolver;
Expand Down Expand Up @@ -209,7 +209,7 @@ protected AgEntity<T> buildEntity() {
rootDataResolver != null ? rootDataResolver : ThrowingRootDataResolver.getInstance(),

// TODO: support Exp filters via annotations?
ObjectFilter.allowsAllFilter());
SelectFilter.allowsAllFilter());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;

import java.io.IOException;

/**
* Encoder filter that delegates individual encoder methods to custom functions.
*
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
class CompositeEntityEncoderFilter implements EntityEncoderFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.agrest.encoder;

import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;

/**
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.agrest.encoder;

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.filter.SelectFilter;

import java.io.IOException;

/**
* @param <T>
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.agrest.encoder;

import io.agrest.filter.SelectFilter;

/**
* @param <T>
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.ResourceEntity;
import io.agrest.filter.SelectFilter;

import java.io.IOException;

/**
* An interceptor for custom encoding of specific entities. An application may define a filter that suppresses
* certain objects based on security constraints, or may provide a custom encoder for a given object, etc.
*
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
public interface EntityEncoderFilter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.agrest.encoder;

import io.agrest.filter.SelectFilter;

import java.util.Objects;

/**
* A helper class to build EntityEncoderFilters. Usually created via static methods on {@link EntityEncoderFilter}.
*
* @since 3.4
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
public class EntityEncoderFilterBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.agrest.encoder;

import com.fasterxml.jackson.core.JsonGenerator;
import io.agrest.filter.SelectFilter;

import java.io.IOException;
import java.util.List;
Expand All @@ -9,7 +10,7 @@
* An encoder that passes encoding request through a chain of {@link EntityEncoderFilter} filters before it gets to the real
* Encoder.
*
* @deprecated since 4.8 in favor of {@link io.agrest.filter.ObjectFilter}.
* @deprecated since 4.8 in favor of {@link SelectFilter}.
*/
@Deprecated
public class FilterChainEncoder implements Encoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/**
* @since 4.8
*/
final class AllowAllObjectFilter<T> implements ObjectFilter<T> {
final class AllowAllSelectFilter<T> implements SelectFilter<T> {

static final AllowAllObjectFilter instance = new AllowAllObjectFilter<>();
static final AllowAllSelectFilter instance = new AllowAllSelectFilter<>();

private AllowAllObjectFilter() {
private AllowAllSelectFilter() {
}

@Override
Expand All @@ -21,7 +21,7 @@ public boolean allowsAll() {
}

@Override
public ObjectFilter<T> andThen(ObjectFilter<T> another) {
public SelectFilter<T> andThen(SelectFilter<T> another) {
return another;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* @since 4.8
*/
@FunctionalInterface
public interface ObjectFilter<T> {
public interface SelectFilter<T> {

static <T> ObjectFilter<T> allowsAllFilter() {
return AllowAllObjectFilter.instance;
static <T> SelectFilter<T> allowsAllFilter() {
return AllowAllSelectFilter.instance;
}

boolean isAllowed(T object);
Expand All @@ -21,7 +21,7 @@ default boolean allowsAll() {
return false;
}

default ObjectFilter<T> andThen(ObjectFilter<T> another) {
default SelectFilter<T> andThen(SelectFilter<T> another) {
if (another.allowsAll()) {
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions agrest-engine/src/main/java/io/agrest/meta/AgEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.property.PropertyReader;
import io.agrest.resolver.RootDataResolver;

Expand Down Expand Up @@ -80,5 +80,5 @@ default PropertyReader getIdReader() {
*
* @since 4.8
*/
ObjectFilter<T> getSelectFilter();
SelectFilter<T> getSelectFilter();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.filter.PropertyFilteringRulesBuilder;
import io.agrest.filter.PropertyFilter;
import io.agrest.property.PropertyReader;
Expand Down Expand Up @@ -34,13 +34,13 @@ public class AgEntityOverlay<T> {
private PropertyFilter readablePropFilter;
private PropertyFilter writablePropFilter;

private ObjectFilter<T> selectFilter;
private SelectFilter<T> selectFilter;

public AgEntityOverlay(Class<T> type) {
this.type = type;
this.attributes = new HashMap<>();
this.relationships = new HashMap<>();
this.selectFilter = ObjectFilter.allowsAllFilter();
this.selectFilter = SelectFilter.allowsAllFilter();
}

private static PropertyReader fromFunction(Function<?, ?> f) {
Expand Down Expand Up @@ -197,7 +197,7 @@ public AgEntityOverlay<T> writablePropFilter(PropertyFilter filter) {
/**
* @since 4.8
*/
public AgEntityOverlay<T> selectFilter(ObjectFilter<T> filter) {
public AgEntityOverlay<T> selectFilter(SelectFilter<T> filter) {
this.selectFilter = this.selectFilter.andThen(filter);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.resolver.RootDataResolver;

import java.util.Collection;
Expand All @@ -14,7 +14,7 @@ public class DefaultAgEntity<T> implements AgEntity<T> {
private final String name;
private final Class<T> type;
private final RootDataResolver<T> dataResolver;
private final ObjectFilter<T> selectFilter;
private final SelectFilter<T> selectFilter;

// TODO: ensure name uniqueness between all types of properties
private final Map<String, AgIdPart> ids;
Expand All @@ -28,7 +28,7 @@ public DefaultAgEntity(
Map<String, AgAttribute> attributes,
Map<String, AgRelationship> relationships,
RootDataResolver<T> dataResolver,
ObjectFilter<T> selectFilter) {
SelectFilter<T> selectFilter) {

this.name = name;
this.type = type;
Expand Down Expand Up @@ -85,7 +85,7 @@ public RootDataResolver<T> getDataResolver() {
}

@Override
public ObjectFilter<T> getSelectFilter() {
public SelectFilter<T> getSelectFilter() {
return selectFilter;
}

Expand Down
4 changes: 2 additions & 2 deletions agrest-engine/src/main/java/io/agrest/meta/LazyAgEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.agrest.meta;

import io.agrest.filter.ObjectFilter;
import io.agrest.filter.SelectFilter;
import io.agrest.resolver.RootDataResolver;

import java.util.Collection;
Expand Down Expand Up @@ -64,7 +64,7 @@ public RootDataResolver<T> getDataResolver() {
}

@Override
public ObjectFilter getSelectFilter() {
public SelectFilter getSelectFilter() {
return getDelegate().getSelectFilter();
}
}
Loading

0 comments on commit 7dcf992

Please sign in to comment.