Skip to content

Commit

Permalink
ref[storage]: flexible array convert
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 19, 2024
1 parent 891af8f commit db0d91e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
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;
Expand All @@ -34,22 +32,6 @@
*/
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;
Expand Down Expand Up @@ -83,8 +65,7 @@ public static <T> List<T> read(InputStream inputStream, Class<T> clazz, String s
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;
}
}
Expand Down Expand Up @@ -113,8 +94,7 @@ public static <T> List<T> read(InputStream inputStream, Class<T> clazz, String s

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,14 +36,29 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {

@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;
}
}
54 changes: 54 additions & 0 deletions storage/src/main/java/com/zfoo/storage/util/ConvertUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit db0d91e

Please sign in to comment.