From be716290db7bb9a2c151e5b21247c36b9c8f046f Mon Sep 17 00:00:00 2001 From: Clay Reimann Date: Fri, 10 Mar 2017 11:17:53 -0600 Subject: [PATCH] Store reference to created ElideConverter. Make priority constants --- .../main/java/com/yahoo/elide/ElideSettings.java | 2 ++ .../java/com/yahoo/elide/ElideSettingsBuilder.java | 13 ++++++++++++- .../com/yahoo/elide/utils/coerce/CoerceUtil.java | 7 ++++--- .../utils/coerce/converters/ElideConverter.java | 9 ++++++--- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/elide-core/src/main/java/com/yahoo/elide/ElideSettings.java b/elide-core/src/main/java/com/yahoo/elide/ElideSettings.java index 13c9cc8c5e..93b28e7cd5 100644 --- a/elide-core/src/main/java/com/yahoo/elide/ElideSettings.java +++ b/elide-core/src/main/java/com/yahoo/elide/ElideSettings.java @@ -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; @@ -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; } diff --git a/elide-core/src/main/java/com/yahoo/elide/ElideSettingsBuilder.java b/elide-core/src/main/java/com/yahoo/elide/ElideSettingsBuilder.java index 7f82d7d782..6616ef105a 100644 --- a/elide-core/src/main/java/com/yahoo/elide/ElideSettingsBuilder.java +++ b/elide-core/src/main/java/com/yahoo/elide/ElideSettingsBuilder.java @@ -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; @@ -70,6 +71,8 @@ public ElideSettings build() { subqueryFilterDialects.add(new DefaultFilterDialect(entityDictionary)); } + CoerceUtil.setup(converter); + return new ElideSettings( auditLogger, dataStore, @@ -81,7 +84,8 @@ public ElideSettings build() { defaultMaxPageSize, defaultPageSize, useFilterExpressions, - updateStatusCode); + updateStatusCode, + converter); } public ElideSettingsBuilder withAuditLogger(AuditLogger auditLogger) { @@ -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 converters) { this.converter = new ElideConverter(converters); return this; diff --git a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java index 58f0d20530..0ecce63c29 100644 --- a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java +++ b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/CoerceUtil.java @@ -19,7 +19,7 @@ public class CoerceUtil { //static block for setup and registering new converters static { - setup(); + setup(new ElideConverter()); } /** @@ -43,8 +43,9 @@ public static T coerce(Object value, Class 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)); } } diff --git a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideConverter.java b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideConverter.java index 8a191a6677..9114aede4f 100644 --- a/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideConverter.java +++ b/elide-core/src/main/java/com/yahoo/elide/utils/coerce/converters/ElideConverter.java @@ -35,6 +35,9 @@ public class ElideConverter extends ConvertUtilsBean { public static final BiFunction, 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 CONVERTERS = Multimaps.synchronizedSortedSetMultimap( TreeMultimap.create()); @@ -71,9 +74,9 @@ public Converter lookup(Class sourceType, Class targetType) { public static Map defaultConverters() { Map 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; }