Skip to content

Commit

Permalink
ref[storage]: list convert
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 19, 2024
1 parent db0d91e commit 9d069cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.core.convert.converter.ConditionalGenericConverter;

import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Set;

/**
Expand Down Expand Up @@ -49,16 +48,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
}
// 如果为json格式,则以json格式解析
if (content.startsWith("[") || content.endsWith("]")) {
return JsonUtils.string2Object(content, targetType.getType());
return componentType.isPrimitive()
? JsonUtils.string2Object(content, targetType.getObjectType())
: JsonUtils.string2Array(content, componentType);
}
// 用普通的逗号分隔符解析
var splits = content.split(StringUtils.COMMA_REGEX);
var length = splits.length;
Object array = Array.newInstance(componentType, length);
for (var i = 0; i < length; i++) {
Object value = ConvertUtils.convert(splits[i], componentType);
Array.set(array, i, value);
}
return array;
return ConvertUtils.convertToArray(content, componentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package com.zfoo.storage.strategy;

import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.util.ConvertUtils;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -42,19 +44,20 @@ public Set<ConvertiblePair> getConvertibleTypes() {

@Override
public Object convert(Object source, @NonNull TypeDescriptor sourceType, TypeDescriptor targetType) {
// String content = (String) source;
// return targetType.isPrimitive() ? JsonUtil.string2Object(content, targetType.getObjectType())
// : JsonUtil.string2Array(content, targetType.getType());
var content = StringUtils.trim((String) source);
if (StringUtils.isEmpty(content)) {
return Collections.emptyList();
}
Class<?> clazz = null;

String content = (String) source;
Type type = targetType.getResolvableType().getGeneric(0).getType();
if (type instanceof Class) {
clazz = (Class<?>) type;
} else if (type instanceof ParameterizedType parameterizedType) {
clazz = (Class<?>) parameterizedType.getRawType();
}

return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
if (content.startsWith("[") || content.endsWith("]")) {
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
}
return Collections.unmodifiableList(ConvertUtils.convertToList(content, clazz));
}
}
36 changes: 36 additions & 0 deletions storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
*/
package com.zfoo.storage.util;

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

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
* @author godotg
Expand Down Expand Up @@ -51,4 +56,35 @@ public static Object convertField(String content, Field field) {
return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
}

public static Object convertToArray(String content, Class<?> componentType) {
content = StringUtils.trim(content);
// null safe,content为空则返回长度为0的数组
if (StringUtils.isEmpty(content)) {
return Array.newInstance(componentType, 0);
}
// 用普通的逗号分隔符解析
var splits = content.split(StringUtils.COMMA_REGEX);
var length = splits.length;
Object array = Array.newInstance(componentType, length);
for (var i = 0; i < length; i++) {
Object value = ConvertUtils.convert(StringUtils.trim(splits[i]), componentType);
Array.set(array, i, value);
}
return array;
}

public static <T> List<T> convertToList(String content, Class<T> type) {
content = StringUtils.trim(content);
if (StringUtils.isEmpty(content)) {
return Collections.emptyList();
}
var splits = content.split(StringUtils.COMMA_REGEX);
var length = splits.length;
var list = new ArrayList<T>();
for (var i = 0; i < length; i++) {
var value = ConvertUtils.convert(StringUtils.trim(splits[i]), type);
list.add(value);
}
return list;
}
}

0 comments on commit 9d069cd

Please sign in to comment.