mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add fill template
This commit is contained in:
@@ -93,6 +93,17 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得path表达式对应的值
|
||||
*
|
||||
* @param expression path表达式
|
||||
* @param <T> 属性值类型
|
||||
* @return 值
|
||||
*/
|
||||
public <T> T getProperty(final String expression) {
|
||||
return BeanUtil.getProperty(bean, expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得字段对应值
|
||||
*
|
||||
@@ -161,6 +172,18 @@ public class DynaBean implements Cloneable, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置属性值
|
||||
*
|
||||
* @param expression path表达式
|
||||
* @param value 值
|
||||
* @return this
|
||||
*/
|
||||
public DynaBean setProperty(final String expression, final Object value) {
|
||||
BeanUtil.setProperty(bean, expression, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置字段值
|
||||
*
|
||||
|
@@ -346,11 +346,7 @@ public abstract class StrTemplate {
|
||||
// 根据 占位符 返回 需要序列化的值
|
||||
final Object value = valueSupplier.apply(segment);
|
||||
if (value != null) {
|
||||
if (value instanceof String) {
|
||||
return (String) value;
|
||||
} else {
|
||||
return StrUtil.utf8Str(value);
|
||||
}
|
||||
return StrUtil.utf8Str(value);
|
||||
} else {
|
||||
// 处理null值
|
||||
return formatNullValue(segment);
|
||||
|
@@ -17,19 +17,16 @@
|
||||
package org.dromara.hutool.core.text.placeholder.template;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.bean.BeanDesc;
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.func.LambdaUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
import org.dromara.hutool.core.text.StrPool;
|
||||
import org.dromara.hutool.core.text.placeholder.StrTemplate;
|
||||
import org.dromara.hutool.core.text.placeholder.segment.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
|
||||
@@ -345,15 +342,6 @@ public class NamedPlaceholderStrTemplate extends StrTemplate {
|
||||
}
|
||||
if (beanOrMap instanceof Map) {
|
||||
return format((Map<String, ?>) beanOrMap);
|
||||
} else if (BeanUtil.isReadableBean(beanOrMap.getClass())) {
|
||||
final BeanDesc beanDesc = BeanUtil.getBeanDesc(beanOrMap.getClass());
|
||||
return format(fieldName -> {
|
||||
final Method getterMethod = beanDesc.getGetter(fieldName);
|
||||
if (getterMethod == null) {
|
||||
return null;
|
||||
}
|
||||
return LambdaUtil.buildGetter(getterMethod).apply(beanOrMap);
|
||||
});
|
||||
}
|
||||
return format(fieldName -> BeanUtil.getProperty(beanOrMap, fieldName));
|
||||
}
|
||||
@@ -374,14 +362,14 @@ public class NamedPlaceholderStrTemplate extends StrTemplate {
|
||||
/**
|
||||
* 使用 占位变量名称 从 valueSupplier 中查询值来 替换 占位符
|
||||
*
|
||||
* @param valueSupplier 根据 占位变量名称 返回 值
|
||||
* @param valueProvider 根据 占位变量名称 返回 值
|
||||
* @return 格式化字符串
|
||||
*/
|
||||
public String format(final Function<String, ?> valueSupplier) {
|
||||
if (valueSupplier == null) {
|
||||
public String format(final Function<String, ?> valueProvider) {
|
||||
if (valueProvider == null) {
|
||||
return getTemplate();
|
||||
}
|
||||
return formatBySegment(segment -> valueSupplier.apply(segment.getPlaceholder()));
|
||||
return formatBySegment(segment -> valueProvider.apply(segment.getPlaceholder()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user