mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-22 04:26:08 +00:00
ref[storage]: refactor convert
This commit is contained in:
@@ -81,9 +81,7 @@ public class ResourceInterpreter {
|
||||
|
||||
for (var fieldInfo : fieldInfos) {
|
||||
var content = columns.get(fieldInfo.index);
|
||||
if (StringUtils.isNotEmpty(content) || fieldInfo.field.getType() == String.class) {
|
||||
inject(instance, fieldInfo.field, content);
|
||||
}
|
||||
inject(instance, fieldInfo.field, content);
|
||||
}
|
||||
result.add(instance);
|
||||
}
|
||||
|
||||
@@ -56,9 +56,7 @@ public class ListConverter implements ConditionalGenericConverter {
|
||||
clazz = (Class<?>) type;
|
||||
}
|
||||
if (content.startsWith("[") || content.endsWith("]")) {
|
||||
return clazz.equals(List.class)
|
||||
? Collections.unmodifiableList(JsonUtils.string2List(content, clazz))
|
||||
: JsonUtils.string2List(content, clazz);
|
||||
return Collections.unmodifiableList(JsonUtils.string2List(content, clazz));
|
||||
}
|
||||
return ConvertUtils.convertToList(content, clazz);
|
||||
}
|
||||
|
||||
@@ -55,9 +55,7 @@ public class SetConverter implements ConditionalGenericConverter {
|
||||
clazz = (Class<?>) type;
|
||||
}
|
||||
if (content.startsWith("[") || content.endsWith("]")) {
|
||||
return clazz.equals(Set.class)
|
||||
? Collections.unmodifiableSet(JsonUtils.string2Set(content, clazz))
|
||||
: JsonUtils.string2Set(content, clazz);
|
||||
return Collections.unmodifiableSet(JsonUtils.string2Set(content, clazz));
|
||||
}
|
||||
return ConvertUtils.convertToSet(content, clazz);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public abstract class ConvertUtils {
|
||||
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());
|
||||
@@ -60,12 +61,10 @@ public abstract class ConvertUtils {
|
||||
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);
|
||||
var list = convertToList(content, componentType);
|
||||
Object array = Array.newInstance(componentType, list.size());
|
||||
for (var i = 0; i < list.size(); i++) {
|
||||
Array.set(array, i, list.get(i));
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -90,13 +89,6 @@ public abstract class ConvertUtils {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
var splits = content.split(StringUtils.COMMA_REGEX);
|
||||
var length = splits.length;
|
||||
var set = new HashSet<T>();
|
||||
for (var i = 0; i < length; i++) {
|
||||
var value = ConvertUtils.convert(StringUtils.trim(splits[i]), type);
|
||||
set.add(value);
|
||||
}
|
||||
return Collections.unmodifiableSet(set);
|
||||
return Set.copyOf(convertToList(content, type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.zfoo.storage.anno.Index;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
@@ -39,6 +40,9 @@ public class StudentResource {
|
||||
private int age;
|
||||
private float score;
|
||||
private String[] courses;
|
||||
private int[] intArray;
|
||||
private List<Integer> intList;
|
||||
private Set<Integer> intSet;
|
||||
private User[] users;
|
||||
private List<User> userList;
|
||||
private User user;
|
||||
@@ -67,6 +71,18 @@ public class StudentResource {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public int[] getIntArray() {
|
||||
return intArray;
|
||||
}
|
||||
|
||||
public List<Integer> getIntList() {
|
||||
return intList;
|
||||
}
|
||||
|
||||
public Set<Integer> getIntSet() {
|
||||
return intSet;
|
||||
}
|
||||
|
||||
public User[] getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user