add isXXX support

This commit is contained in:
Looly
2021-04-03 10:46:02 +08:00
parent cd9d902ff3
commit 78aaab53bc
2 changed files with 28 additions and 16 deletions

View File

@@ -3,12 +3,13 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.6.3 (2021-04-02) # 5.6.3 (2021-04-03)
### 新特性 ### 新特性
* 【core 】 修改数字转换的实现增加按照指定端序转换pr#1492@Github * 【core 】 修改数字转换的实现增加按照指定端序转换pr#1492@Github
* 【core 】 修改拆分byte数组时最后一组长度的规则pr#1494@Github * 【core 】 修改拆分byte数组时最后一组长度的规则pr#1494@Github
* 【core 】 新增根据日期获取节气pr#1496@Github * 【core 】 新增根据日期获取节气pr#1496@Github
* 【core 】 mapToBean()添加对布尔值is前缀的识别pr#294@Gitee
### Bug修复 ### Bug修复
* 【core 】 修复Validator.isUrl()传空返回trueissue#I3ETTY@Gitee * 【core 】 修复Validator.isUrl()传空返回trueissue#I3ETTY@Gitee

View File

@@ -10,7 +10,14 @@ import java.util.Map;
/** /**
* Map值提供者支持驼峰和下划线的key兼容。<br> * Map值提供者支持驼峰和下划线的key兼容。<br>
* 假设目标属性为firstName则Map中为firstName或first_name都可以对应到值。 * 假设目标属性为firstName则Map中以下形式的值都可以对应
* <ul>
* <li>firstName</li>
* <li>first_name</li>
* <li>isFirstName如果为Boolean或boolean类型的值</li>
* <li>is_first_name如果为Boolean或boolean类型的值</li>
* </ul>
* 为firstName或first_name都可以对应到值。
* *
* @author looly * @author looly
*/ */
@@ -50,17 +57,22 @@ public class MapValueProvider implements ValueProvider<String> {
@Override @Override
public Object value(String key, Type valueType) { public Object value(String key, Type valueType) {
Object value = map.get(getKey(key, valueType)); final String key1 = getKey(key, valueType);
if (null == key1) {
return null;
}
final Object value = map.get(key1);
return Convert.convertWithCheck(valueType, value, null, this.ignoreError); return Convert.convertWithCheck(valueType, value, null, this.ignoreError);
} }
@Override @Override
public boolean containsKey(String key) { public boolean containsKey(String key) {
return map.containsKey(getKey(key, null)); return null != getKey(key, null);
} }
/** /**
* 获得map中可能包含的key * 获得map中可能包含的key,不包含返回null
* *
* @param key map中可能包含的key * @param key map中可能包含的key
* @param valueType 值类型用于判断是否为Boolean可以为null * @param valueType 值类型用于判断是否为Boolean可以为null
@@ -72,27 +84,26 @@ public class MapValueProvider implements ValueProvider<String> {
} }
//检查下划线模式 //检查下划线模式
String containKey = StrUtil.toUnderlineCase(key); String customKey = StrUtil.toUnderlineCase(key);
if (map.containsKey(containKey)) { if (map.containsKey(customKey)) {
return containKey; return customKey;
} }
//检查boolean类型 //检查boolean类型
if (null == valueType || Boolean.class == valueType || boolean.class == valueType) { if (null == valueType || Boolean.class == valueType || boolean.class == valueType) {
//boolean类型字段字段名支持两种方式 //boolean类型字段字段名支持两种方式
containKey = StrUtil.upperFirstAndAddPre(key, "is"); customKey = StrUtil.upperFirstAndAddPre(key, "is");
if (map.containsKey(containKey)) { if (map.containsKey(customKey)) {
return containKey; return customKey;
} }
//检查下划线模式 //检查下划线模式
containKey = StrUtil.toUnderlineCase(containKey); customKey = StrUtil.toUnderlineCase(customKey);
if (map.containsKey(containKey)) { if (map.containsKey(customKey)) {
return containKey; return customKey;
} }
} }
return null;
return key;
} }
} }