mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-05 13:17:58 +08:00
!294 mapToBean()添加对布尔值is前缀的识别
Merge pull request !294 from yl-yue/v5-dev
This commit is contained in:
commit
cd9d902ff3
@ -50,23 +50,49 @@ public class MapValueProvider implements ValueProvider<String> {
|
||||
|
||||
@Override
|
||||
public Object value(String key, Type valueType) {
|
||||
Object value = map.get(key);
|
||||
if (null == value) {
|
||||
//检查下划线模式
|
||||
value = map.get(StrUtil.toUnderlineCase(key));
|
||||
}
|
||||
|
||||
Object value = map.get(getKey(key, valueType));
|
||||
return Convert.convertWithCheck(valueType, value, null, this.ignoreError);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(String key) {
|
||||
return map.containsKey(getKey(key, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得map中可能包含的key
|
||||
*
|
||||
* @param key map中可能包含的key
|
||||
* @param valueType 值类型,用于判断是否为Boolean,可以为null
|
||||
* @return map中可能包含的key
|
||||
*/
|
||||
private String getKey(String key, Type valueType) {
|
||||
if (map.containsKey(key)) {
|
||||
return true;
|
||||
} else {
|
||||
//检查下划线模式
|
||||
return map.containsKey(StrUtil.toUnderlineCase(key));
|
||||
return key;
|
||||
}
|
||||
|
||||
//检查下划线模式
|
||||
String containKey = StrUtil.toUnderlineCase(key);
|
||||
if (map.containsKey(containKey)) {
|
||||
return containKey;
|
||||
}
|
||||
|
||||
//检查boolean类型
|
||||
if (null == valueType || Boolean.class == valueType || boolean.class == valueType) {
|
||||
//boolean类型字段字段名支持两种方式
|
||||
containKey = StrUtil.upperFirstAndAddPre(key, "is");
|
||||
if (map.containsKey(containKey)) {
|
||||
return containKey;
|
||||
}
|
||||
|
||||
//检查下划线模式
|
||||
containKey = StrUtil.toUnderlineCase(containKey);
|
||||
if (map.containsKey(containKey)) {
|
||||
return containKey;
|
||||
}
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -18,13 +19,7 @@ import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Bean工具单元测试
|
||||
@ -201,6 +196,8 @@ public class BeanUtilTest {
|
||||
person.setName("测试A11");
|
||||
person.setSubName("sub名字");
|
||||
person.setSlow(true);
|
||||
person.setBooleana(true);
|
||||
person.setBooleanb(true);
|
||||
|
||||
Map<String, Object> map = BeanUtil.beanToMap(person);
|
||||
Assert.assertEquals("sub名字", map.get("aliasSubName"));
|
||||
@ -211,9 +208,13 @@ public class BeanUtilTest {
|
||||
Map<String, Object> map = MapUtil.newHashMap();
|
||||
map.put("aliasSubName", "sub名字");
|
||||
map.put("slow", true);
|
||||
map.put("is_booleana", "1");
|
||||
map.put("is_booleanb", true);
|
||||
|
||||
final SubPersonWithAlias subPersonWithAlias = BeanUtil.toBean(map, SubPersonWithAlias.class);
|
||||
Assert.assertEquals("sub名字", subPersonWithAlias.getSubName());
|
||||
Assert.assertTrue(subPersonWithAlias.isBooleana());
|
||||
Assert.assertEquals(true, subPersonWithAlias.getBooleanb());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -360,11 +361,14 @@ public class BeanUtilTest {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static class SubPersonWithAlias extends Person {
|
||||
// boolean参数值非isXXX形式
|
||||
@Alias("aliasSubName")
|
||||
private String subName;
|
||||
private Boolean slow;
|
||||
private boolean booleana;
|
||||
private Boolean booleanb;
|
||||
}
|
||||
|
||||
@Getter
|
||||
|
Loading…
Reference in New Issue
Block a user