Skip to content

Commit

Permalink
feat[storage]: custom convert
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 20, 2024
1 parent cc76daf commit 37cfbd6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
public class ArrayConverter implements ConditionalGenericConverter {

public static final ArrayConverter INSTANCE = new ArrayConverter();

private ArrayConverter() {
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
Expand Down
32 changes: 20 additions & 12 deletions storage/src/main/java/com/zfoo/storage/convert/ConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.zfoo.protocol.util.StringUtils;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;

import java.lang.reflect.Array;
Expand All @@ -27,29 +28,36 @@ public abstract class ConvertUtils {

private static final TypeDescriptor TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);

private static final ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
private static ConversionService conversionService = customConvert(Collections.emptySet());

static {

public static ConversionService customConvert(Set<?> customConverts) {
var converters = new HashSet<>();
converters.add(new ArrayConverter());
converters.add(new ListConverter());
converters.add(new SetConverter());
converters.add(new JsonToMapConverter());
converters.add(new JsonToObjectConverter());
converters.add(new StringToClassConverter());
converters.add(new StringToDateConverter());
// default convert
converters.add(ArrayConverter.INSTANCE);
converters.add(ListConverter.INSTANCE);
converters.add(SetConverter.INSTANCE);
converters.add(JsonToMapConverter.INSTANCE);
converters.add(JsonToObjectConverter.INSTANCE);
converters.add(StringToClassConverter.INSTANCE);
converters.add(StringToDateConverter.INSTANCE);
// custom convert
converters.addAll(customConverts);

ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
conversionServiceFactoryBean.setConverters(converters);
conversionServiceFactoryBean.afterPropertiesSet();
return conversionServiceFactoryBean.getObject();
}


public static <T> T convert(String content, Class<T> targetType) {
return conversionServiceFactoryBean.getObject().convert(content, targetType);
return conversionService.convert(content, targetType);
}

public static Object convertField(String content, Field field) {
var targetType = new TypeDescriptor(field);
return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
return conversionService.convert(content, TYPE_DESCRIPTOR, targetType);
}

public static Object convertToArray(String content, Class<?> componentType) {
Expand All @@ -75,7 +83,7 @@ public static <T> List<T> convertToList(String content, Class<T> genericType) {
var splits = content.split(StringUtils.COMMA_REGEX);
var list = new ArrayList<T>();
for (var split : splits) {
var value = ConvertUtils.convert(StringUtils.trim(split), genericType);
var value = convert(StringUtils.trim(split), genericType);
list.add(value);
}
return Collections.unmodifiableList(list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
* @author godotg
*/
public class JsonToMapConverter implements ConditionalGenericConverter {

public static final JsonToMapConverter INSTANCE = new JsonToMapConverter();

private JsonToMapConverter() {
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return sourceType.getType() == String.class && targetType.isMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
public class JsonToObjectConverter implements ConditionalGenericConverter {

public static final JsonToObjectConverter INSTANCE = new JsonToObjectConverter();

private JsonToObjectConverter() {
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
*/
public class ListConverter implements ConditionalGenericConverter {

public static final ListConverter INSTANCE = new ListConverter();

private ListConverter() {
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
public class SetConverter implements ConditionalGenericConverter {

public static final SetConverter INSTANCE = new SetConverter();

private SetConverter() {
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
public class StringToClassConverter implements Converter<String, Class<?>> {

public static final StringToClassConverter INSTANCE = new StringToClassConverter();

private StringToClassConverter() {
}

@Override
public Class<?> convert(String source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
*/
public class StringToDateConverter implements Converter<String, Date> {

public static final StringToDateConverter INSTANCE = new StringToDateConverter();

private StringToDateConverter() {
}

@Override
public Date convert(String source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class ConversionTest {
private static final ConversionServiceFactoryBean csfb = new ConversionServiceFactoryBean();
private static final Set<Object> converters = new HashSet<>();

private static final StringToDateConverter std = new StringToDateConverter();
private static final StringToClassConverter stcc = new StringToClassConverter();
private static final JsonToMapConverter jtmc = new JsonToMapConverter();
private static final ArrayConverter jtac = new ArrayConverter();
private static final StringToDateConverter std = StringToDateConverter.INSTANCE;
private static final StringToClassConverter stcc = StringToClassConverter.INSTANCE;
private static final JsonToMapConverter jtmc = JsonToMapConverter.INSTANCE;
private static final ArrayConverter jtac = ArrayConverter.INSTANCE;

static {
converters.add(std);
Expand Down

0 comments on commit 37cfbd6

Please sign in to comment.