Skip to content

Commit

Permalink
Store reference to created ElideConverter. Make priority constants
Browse files Browse the repository at this point in the history
  • Loading branch information
clayreimann committed Mar 10, 2017
1 parent 5e1e63a commit be71629
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions elide-core/src/main/java/com/yahoo/elide/ElideSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.yahoo.elide.core.filter.dialect.SubqueryFilterDialect;
import com.yahoo.elide.jsonapi.JsonApiMapper;
import com.yahoo.elide.security.PermissionExecutor;
import com.yahoo.elide.utils.coerce.converters.ElideConverter;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -35,4 +36,5 @@ public class ElideSettings {
@Getter private final int defaultPageSize;
@Getter private final boolean useFilterExpressions;
@Getter private final int updateStatusCode;
@Getter private final ElideConverter converter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.yahoo.elide.jsonapi.JsonApiMapper;
import com.yahoo.elide.security.PermissionExecutor;
import com.yahoo.elide.security.executors.ActivePermissionExecutor;
import com.yahoo.elide.utils.coerce.CoerceUtil;
import com.yahoo.elide.utils.coerce.converters.ElideConverter;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -70,6 +71,8 @@ public ElideSettings build() {
subqueryFilterDialects.add(new DefaultFilterDialect(entityDictionary));
}

CoerceUtil.setup(converter);

return new ElideSettings(
auditLogger,
dataStore,
Expand All @@ -81,7 +84,8 @@ public ElideSettings build() {
defaultMaxPageSize,
defaultPageSize,
useFilterExpressions,
updateStatusCode);
updateStatusCode,
converter);
}

public ElideSettingsBuilder withAuditLogger(AuditLogger auditLogger) {
Expand Down Expand Up @@ -161,6 +165,13 @@ public ElideSettingsBuilder withUseFilterExpressions(boolean useFilterExpression
return this;
}

/**
* You should strongly consider extending the map build by {@code ElideConverter.defaultConverters}. If you
* do not then you will lose Elide's default behavior for Maps and Enums
*
* @param converters
* @return
*/
public ElideSettingsBuilder withConverters(Map<Integer, ElideConverter.TypeCoercer> converters) {
this.converter = new ElideConverter(converters);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CoerceUtil {

//static block for setup and registering new converters
static {
setup();
setup(new ElideConverter());
}

/**
Expand All @@ -43,8 +43,9 @@ public static <T> T coerce(Object value, Class<T> cls) {

/**
* Perform CoerceUtil setup.
* @param elideConverter the converter to register
*/
private static void setup() {
BeanUtilsBean.setInstance(new BeanUtilsBean(new ElideConverter()));
public static void setup(ElideConverter elideConverter) {
BeanUtilsBean.setInstance(new BeanUtilsBean(elideConverter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class ElideConverter extends ConvertUtilsBean {
public static final BiFunction<Class<?>, Class<?>, Boolean> STR_NUM_TO_DATE = (source, target) ->
(String.class.isAssignableFrom(source) || Number.class.isAssignableFrom(source))
&& ClassUtils.isAssignable(target, Date.class);
public static final int HIGH_PRIORITY = 10;
public static final int MEDIUM_PRIORITY = 20;
public static final int LOW_PRIORITY = 30;

private static SortedSetMultimap<Integer, TypeCoercer> CONVERTERS = Multimaps.synchronizedSortedSetMultimap(
TreeMultimap.<Integer, TypeCoercer>create());
Expand Down Expand Up @@ -71,9 +74,9 @@ public Converter lookup(Class<?> sourceType, Class<?> targetType) {

public static Map<Integer, TypeCoercer> defaultConverters() {
Map<Integer, TypeCoercer> converters = new HashMap<>();
converters.put(10, new TypeCoercer(TARGET_IS_ENUM, TO_ENUM_CONVERTER));
converters.put(20, new TypeCoercer(SOURCE_IS_MAP, FROM_MAP_CONVERTER));
converters.put(30, new TypeCoercer(STR_NUM_TO_DATE, EPOCH_TO_DATE_CONVERTER));
converters.put(HIGH_PRIORITY, new TypeCoercer(TARGET_IS_ENUM, TO_ENUM_CONVERTER));
converters.put(MEDIUM_PRIORITY, new TypeCoercer(SOURCE_IS_MAP, FROM_MAP_CONVERTER));
converters.put(LOW_PRIORITY, new TypeCoercer(STR_NUM_TO_DATE, EPOCH_TO_DATE_CONVERTER));
return converters;
}

Expand Down

0 comments on commit be71629

Please sign in to comment.