mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-22 10:26:18 +00:00
ref[storage]: flexible array convert
This commit is contained in:
@@ -19,9 +19,7 @@ import com.zfoo.storage.anno.AliasFieldName;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.interpreter.data.StorageData;
|
||||
import com.zfoo.storage.interpreter.data.StorageEnum;
|
||||
import com.zfoo.storage.strategy.*;
|
||||
import org.springframework.context.support.ConversionServiceFactoryBean;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import com.zfoo.storage.util.ConvertUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -34,22 +32,6 @@ import java.util.*;
|
||||
*/
|
||||
public class ResourceInterpreter {
|
||||
|
||||
private static final TypeDescriptor TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
|
||||
|
||||
private static final ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
|
||||
|
||||
static {
|
||||
var converters = new HashSet<>();
|
||||
converters.add(new JsonToArrayConverter());
|
||||
converters.add(new JsonToListConverter());
|
||||
converters.add(new JsonToMapConverter());
|
||||
converters.add(new JsonToObjectConverter());
|
||||
converters.add(new StringToClassConverter());
|
||||
converters.add(new StringToDateConverter());
|
||||
converters.add(new StringToMapConverter());
|
||||
conversionServiceFactoryBean.setConverters(converters);
|
||||
conversionServiceFactoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public static <T> List<T> read(InputStream inputStream, Class<T> clazz, String suffix) throws IOException {
|
||||
StorageData resource = null;
|
||||
@@ -83,8 +65,7 @@ public class ResourceInterpreter {
|
||||
for (var fieldInfo : fieldInfos) {
|
||||
var content = columns.get(fieldInfo.index);
|
||||
if (StringUtils.isNotEmpty(content) || fieldInfo.field.getType() == String.class) {
|
||||
var targetType = new TypeDescriptor(fieldInfo.field);
|
||||
var value = conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
|
||||
var value = ConvertUtils.convertField(content, fieldInfo.field);
|
||||
params[index++] = value;
|
||||
}
|
||||
}
|
||||
@@ -113,8 +94,7 @@ public class ResourceInterpreter {
|
||||
|
||||
private static void inject(Object instance, Field field, String content) {
|
||||
try {
|
||||
var targetType = new TypeDescriptor(field);
|
||||
var value = conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
|
||||
var value = ConvertUtils.convertField(content, field);
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
ReflectionUtils.setField(field, instance, value);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -14,9 +14,12 @@
|
||||
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 java.lang.reflect.Array;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -33,14 +36,29 @@ public class JsonToArrayConverter implements ConditionalGenericConverter {
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(String.class, Object[].class));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
String content = (String) source;
|
||||
return targetType.getType().getComponentType().isPrimitive()
|
||||
? JsonUtils.string2Object(content, targetType.getObjectType())
|
||||
: JsonUtils.string2Array(content, targetType.getType().getComponentType());
|
||||
var content = StringUtils.trim((String) source);
|
||||
var componentType = targetType.getType().getComponentType();
|
||||
// null safe,content为空则返回长度为0的数组
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Array.newInstance(componentType, 0);
|
||||
}
|
||||
// 如果为json格式,则以json格式解析
|
||||
if (content.startsWith("[") || content.endsWith("]")) {
|
||||
return JsonUtils.string2Object(content, targetType.getType());
|
||||
}
|
||||
// 用普通的逗号分隔符解析
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo 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
|
||||
*
|
||||
* http://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 com.zfoo.storage.util;
|
||||
|
||||
import com.zfoo.storage.strategy.*;
|
||||
import org.springframework.context.support.ConversionServiceFactoryBean;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
*/
|
||||
public abstract class ConvertUtils {
|
||||
|
||||
private static final TypeDescriptor TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
|
||||
|
||||
private static final ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
|
||||
|
||||
static {
|
||||
var converters = new HashSet<>();
|
||||
converters.add(new JsonToArrayConverter());
|
||||
converters.add(new JsonToListConverter());
|
||||
converters.add(new JsonToMapConverter());
|
||||
converters.add(new JsonToObjectConverter());
|
||||
converters.add(new StringToClassConverter());
|
||||
converters.add(new StringToDateConverter());
|
||||
converters.add(new StringToMapConverter());
|
||||
conversionServiceFactoryBean.setConverters(converters);
|
||||
conversionServiceFactoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
|
||||
public static <T> T convert(String content, Class<T> targetType) {
|
||||
return conversionServiceFactoryBean.getObject().convert(content, targetType);
|
||||
}
|
||||
|
||||
public static Object convertField(String content, Field field) {
|
||||
var targetType = new TypeDescriptor(field);
|
||||
return conversionServiceFactoryBean.getObject().convert(content, TYPE_DESCRIPTOR, targetType);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user