Skip to content

Commit

Permalink
FMWK-621 Add support for an optional custom conversions bean (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored Dec 3, 2024
1 parent ed79506 commit 86f57a3
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
Expand All @@ -40,6 +41,7 @@
import org.springframework.data.aerospike.cache.AerospikeCacheKeyProcessor;
import org.springframework.data.aerospike.cache.AerospikeCacheKeyProcessorImpl;
import org.springframework.data.aerospike.convert.AerospikeCustomConversions;
import org.springframework.data.aerospike.convert.AerospikeCustomConverters;
import org.springframework.data.aerospike.convert.AerospikeTypeAliasAccessor;
import org.springframework.data.aerospike.convert.MappingAerospikeConverter;
import org.springframework.data.aerospike.core.AerospikeExceptionTranslator;
Expand All @@ -59,6 +61,7 @@
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -109,12 +112,17 @@ public AerospikeTypeAliasAccessor aerospikeTypeAliasAccessor(AerospikeDataSettin
: new AerospikeTypeAliasAccessor();
}

@Bean(name = "aerospikeCustomConversions")
public AerospikeCustomConversions customConversions() {
return new AerospikeCustomConversions(customConverters());
@Bean
public AerospikeCustomConversions customConversions(@Autowired(required = false)
AerospikeCustomConverters converters) {
List<Object> aggregatedCustomConverters = new ArrayList<>(customConverters());
if (converters != null) {
aggregatedCustomConverters.addAll(converters.getCustomConverters());
}
return new AerospikeCustomConversions(aggregatedCustomConverters);
}

protected List<?> customConverters() {
protected List<Object> customConverters() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.aerospike.convert;

import lombok.Getter;
import org.springframework.data.aerospike.mapping.AerospikeSimpleTypes;
import org.springframework.data.convert.CustomConversions;

Expand All @@ -28,15 +29,16 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Anastasiia Smirnova
* @see org.springframework.data.convert.CustomConversions
* @see CustomConversions
* @see org.springframework.data.mapping.model.SimpleTypeHolder
* @see AerospikeSimpleTypes
*/
public class AerospikeCustomConversions extends CustomConversions {

private static final StoreConversions STORE_CONVERSIONS;

private static final List<Object> STORE_CONVERTERS;
@Getter
private final List<Object> customConverters;

static {
List<Object> converters = new ArrayList<>();
Expand All @@ -49,11 +51,12 @@ public class AerospikeCustomConversions extends CustomConversions {
}

/**
* Create a new instance with a given list of converters.
* Create a new instance with a given list of converters
*
* @param converters the list of custom converters.
* @param converters the list of custom converters
*/
public AerospikeCustomConversions(final List<?> converters) {
public AerospikeCustomConversions(final List<Object> converters) {
super(STORE_CONVERSIONS, converters);
this.customConverters = converters;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2020 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.aerospike.convert;

import lombok.Getter;

import java.util.List;

/**
* Storage for a list of custom converters
*/
public class AerospikeCustomConverters {

@Getter
private final List<Object> customConverters;

/**
* Create a storage for custom converters
*
* @param converters a list of custom converters
*/
public AerospikeCustomConverters(final List<Object> converters) {
this.customConverters = converters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface ReactiveAerospikeOperations {
MappingContext<?, ?> getMappingContext();

/**
* @return converter in use.
* @return Converter in use.
*/
MappingAerospikeConverter getAerospikeConverter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BlockingTestConfig extends AbstractAerospikeDataConfiguration {
Environment env;

@Override
protected List<?> customConverters() {
protected List<Object> customConverters() {
return Arrays.asList(
SampleClasses.CompositeKey.CompositeKeyToStringConverter.INSTANCE,
SampleClasses.CompositeKey.StringToCompositeKeyConverter.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ReactiveTestConfig extends AbstractReactiveAerospikeDataConfigurati
Environment env;

@Override
protected List<?> customConverters() {
protected List<Object> customConverters() {
return Arrays.asList(
SampleClasses.CompositeKey.CompositeKeyToStringConverter.INSTANCE,
SampleClasses.CompositeKey.StringToCompositeKeyConverter.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ private static Class<?> checkForPageable(Class<?> argType) {
return argType;
}

private static MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions) {
private static MappingAerospikeConverter getMappingAerospikeConverter(AerospikeCustomConversions conversions)
{
MappingAerospikeConverter converter = new MappingAerospikeConverter(new AerospikeMappingContext(),
conversions, new AerospikeTypeAliasAccessor(), new AerospikeDataSettings());
converter.afterPropertiesSet();
Expand Down

0 comments on commit 86f57a3

Please sign in to comment.