Skip to content

Commit 7ad8d37

Browse files
committed
release branch 0.0.5
1 parent 79c964e commit 7ad8d37

27 files changed

+322
-97
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: java
22
jdk:
3-
- oraclejdk7
3+
- oraclejdk8
44
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true
55
script: mvn test
66
after_success:

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# csv
22

3-
基于 java 注解生成加签验签 csv。
3+
基于 java 注解的 csv 读写框架
44

55
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.houbb/csv/badge.svg)](http://mvnrepository.com/artifact/com.github.houbb/csv)
66
[![Build Status](https://www.travis-ci.org/houbb/csv.svg?branch=master)](https://www.travis-ci.org/houbb/csv?branch=master)
@@ -34,6 +34,8 @@
3434

3535
- 内置支持集合、数组、Map 的存取
3636

37+
- 支持对象中内嵌其他对象
38+
3739
# 快速开始
3840

3941
## 环境
@@ -48,7 +50,7 @@ maven 3.x
4850
<dependency>
4951
<groupId>com.github.houbb</groupId>
5052
<artifactId>csv</artifactId>
51-
<version>0.0.3</version>
53+
<version>0.0.5</version>
5254
</dependency>
5355
```
5456

@@ -150,3 +152,5 @@ public void commonTest() {
150152
[02-CSV 字段注解的使用](doc/user/02-csv-注解使用.md)
151153

152154
[03-CSV 集合相关支持](doc/user/03-csv-支持集合类.md)
155+
156+
[04-CSV 内嵌对象使用](doc/user/04-csv-支持内嵌对象.md)

doc/CHANGE_LOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@
3333
|:---|:---|:---|:---|:--|
3434
| 1 | O | 优化 head/bom 信息的写入 | 2019-6-18 20:32:36 | |
3535
| 2 | O | 优化读写的实现方式,便于后期拓展 | 2019-6-18 20:32:36 | |
36-
| 3 | O | 添加缓存,提升性能 | 2019-6-18 20:32:36 | |
36+
| 3 | O | 添加缓存,提升性能 | 2019-6-18 20:32:36 | |
37+
38+
# release_0.0.5
39+
40+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
41+
|:---|:---|:---|:---|:--|
42+
| 1 | A | 支持内嵌对象 | 2019-6-20 11:18:14 | |

doc/issues/issues-03-deadcycle.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 如果一个类引用本身
2+
3+
```java
4+
class User {
5+
List<User> children;
6+
}
7+
```
8+
9+
很可能会导致死循环。
10+
11+
验证并修复这个问题。
12+
13+
最好在 @since 0.0.6 版本。
14+
15+
# 对于特殊符号的转义。
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# 支持内嵌对象
2+
3+
有时候我们希望像使用 mongo 一样,非常方便的存取 csv 的嵌套对象。
4+
5+
对于普通的 csv 都没有实现这个特性,本次做了一个尝试,支持内嵌对象的存取。
6+
7+
## 取舍
8+
9+
就像 csv 的简单,需要用到符号 `,` 一样。
10+
11+
内嵌对象为了不破坏 csv 的规范,使用了符号 `:`
12+
13+
换言之,也就是对象内容中不能使用这个符号。
14+
15+
后期会针对出现的符号进行转义,避免这种冲突。
16+
17+
# 测试案例
18+
19+
所有测试代码可以参考 [test]() 模块。
20+
21+
## 示例对象
22+
23+
- UserEntry.java
24+
25+
```java
26+
public class UserEntry {
27+
28+
/**
29+
* 名称
30+
*/
31+
private String name;
32+
33+
/**
34+
* 内嵌的用户信息
35+
*/
36+
@CsvEntry
37+
private User user;
38+
39+
//Getter/Setter/ToString
40+
}
41+
```
42+
43+
这里在需要内嵌的对象上使用注解 `@CsvEntry` 表示需要进行内嵌的对象转换。
44+
45+
- User.java
46+
47+
其中 User 对象是原来使用的普通 java 对象
48+
49+
```java
50+
public class User {
51+
52+
private String name;
53+
54+
private int age;
55+
56+
private float score;
57+
58+
private double money;
59+
60+
private boolean sex;
61+
62+
private short level;
63+
64+
private long id;
65+
66+
private char status;
67+
68+
private byte coin;
69+
70+
//Getter/Setter/ToString
71+
}
72+
```
73+
74+
## 写入测试
75+
76+
```java
77+
public void entryTest() {
78+
final String path = "src\\test\\resources\\entry.csv";
79+
CsvWriteBs.newInstance(path)
80+
.write(buildEntryList());
81+
}
82+
```
83+
84+
- buildEntryList()
85+
86+
负责对象构建代码,内容如下:
87+
88+
```java
89+
/**
90+
* 用户明细列表
91+
* @return 明细列表
92+
* @since 0.0.5
93+
*/
94+
private List<UserEntry> buildEntryList() {
95+
UserEntry userEntry = new UserEntry();
96+
userEntry.name("test");
97+
userEntry.user(buildCommonList().get(0));
98+
return Collections.singletonList(userEntry);
99+
}
100+
```
101+
102+
- buildCommonList()
103+
104+
```java
105+
private List<User> buildCommonList() {
106+
User user = new User();
107+
short s = 4;
108+
byte b = 1;
109+
user.age(10)
110+
.name("你好")
111+
.id(1L)
112+
.score(60)
113+
.coin(b)
114+
.level(s)
115+
.money(200)
116+
.sex(true)
117+
.status('Y');
118+
return Arrays.asList(user);
119+
}
120+
```
121+
122+
生成文件效果
123+
124+
```
125+
name,user
126+
test,你好:10:60.0:200.0:true:4:1:Y:1
127+
```
128+
129+
如你所见,这里内嵌对象的属性使用了 `:` 进行分隔。
130+
131+
## 读取测试
132+
133+
```java
134+
public void entryTest() {
135+
final String path = "src\\test\\resources\\entry.csv";
136+
List<UserEntry> userList = CsvReadBs.newInstance(path)
137+
.read(UserEntry.class);
138+
System.out.println(userList);
139+
}
140+
```
141+
142+
输出信息
143+
144+
```
145+
[UserEntry{name='test', user=User{name='你好', age=10, score=60.0, money=200.0, sex=true, level=4, id=1, status=Y, coin=1}}]
146+
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.houbb</groupId>
88
<artifactId>csv</artifactId>
9-
<version>0.0.5-SNAPSHOT</version>
9+
<version>0.0.5</version>
1010

1111
<properties>
1212
<!--============================== All Plugins START ==============================-->

src/main/java/com/github/houbb/csv/api/IReadConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface IReadConverter<T> {
1515
* @param context 上下文
1616
* @return 结果
1717
*/
18-
T convert(final SingleReadContext<T> context);
18+
T convert(final SingleReadContext context);
1919

2020
}

src/main/java/com/github/houbb/csv/support/context/SingleReadContext.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author binbin.hou
1010
* @since 0.0.4
1111
*/
12-
public class SingleReadContext<T> {
12+
public class SingleReadContext {
1313

1414
/**
1515
* 字符串的值
@@ -29,18 +29,31 @@ public class SingleReadContext<T> {
2929
/**
3030
* 类信息
3131
*/
32-
private Class<T> classType;
32+
private Class classType;
3333

3434
/**
3535
* 排序信息
3636
*/
37-
private ISort<T> sort;
37+
private ISort sort;
38+
39+
/**
40+
* 分隔符号
41+
* 默认使用:,
42+
* 一级明细::
43+
* 二级明细:::
44+
* 三级明细: :::
45+
* 依次类推。
46+
*
47+
* 目的:为了保证 , 号的语意性。
48+
* @since 0.0.5
49+
*/
50+
private String split;
3851

3952
public String value() {
4053
return value;
4154
}
4255

43-
public SingleReadContext<T> value(String value) {
56+
public SingleReadContext value(String value) {
4457
this.value = value;
4558
return this;
4659
}
@@ -49,7 +62,7 @@ public Field field() {
4962
return field;
5063
}
5164

52-
public SingleReadContext<T> field(Field field) {
65+
public SingleReadContext field(Field field) {
5366
this.field = field;
5467
return this;
5568
}
@@ -58,26 +71,35 @@ public Class fieldType() {
5871
return fieldType;
5972
}
6073

61-
public SingleReadContext<T> fieldType(Class fieldType) {
74+
public SingleReadContext fieldType(Class fieldType) {
6275
this.fieldType = fieldType;
6376
return this;
6477
}
6578

66-
public Class<T> classType() {
79+
public Class classType() {
6780
return classType;
6881
}
6982

70-
public SingleReadContext<T> classType(Class<T> classType) {
83+
public SingleReadContext classType(Class classType) {
7184
this.classType = classType;
7285
return this;
7386
}
7487

75-
public ISort<T> sort() {
88+
public ISort sort() {
7689
return sort;
7790
}
7891

79-
public SingleReadContext<T> sort(ISort<T> sort) {
92+
public SingleReadContext sort(ISort sort) {
8093
this.sort = sort;
8194
return this;
8295
}
96+
97+
public String split() {
98+
return split;
99+
}
100+
101+
public SingleReadContext split(String split) {
102+
this.split = split;
103+
return this;
104+
}
83105
}

src/main/java/com/github/houbb/csv/support/convert/read/CommonReadConverter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
import com.github.houbb.csv.api.IReadConverter;
44
import com.github.houbb.csv.support.context.SingleReadContext;
5+
import com.github.houbb.csv.support.context.SingleWriteContext;
56
import com.github.houbb.csv.support.convert.read.collection.ArrayReadConverter;
67
import com.github.houbb.csv.support.convert.read.collection.CollectionReadConverter;
78
import com.github.houbb.csv.support.convert.read.collection.MapReadConverter;
9+
import com.github.houbb.csv.support.convert.read.entry.EntryReadConverter;
810
import com.github.houbb.csv.support.convert.read.type.ITypeConverter;
911
import com.github.houbb.csv.support.convert.read.type.impl.*;
12+
import com.github.houbb.csv.support.convert.write.entry.EntryWriteConverter;
13+
import com.github.houbb.csv.util.CsvFieldUtil;
14+
import com.github.houbb.csv.util.CsvInnerUtil;
1015
import com.github.houbb.heaven.annotation.ThreadSafe;
1116
import com.github.houbb.heaven.support.instance.impl.InstanceFactory;
1217
import com.github.houbb.heaven.support.instance.impl.Instances;
18+
import com.github.houbb.heaven.support.sort.ISort;
1319
import com.github.houbb.heaven.util.lang.ObjectUtil;
1420
import com.github.houbb.heaven.util.lang.StringUtil;
1521
import com.github.houbb.heaven.util.lang.reflect.ClassTypeUtil;
@@ -48,6 +54,8 @@ public class CommonReadConverter implements IReadConverter<Object> {
4854
public Object convert(final SingleReadContext context) {
4955
final String value = context.value();
5056
final Field field = context.field();
57+
final String split = context.split();
58+
final ISort sort = context.sort();
5159

5260
//1. 为空判断
5361
if(StringUtil.isEmpty(value)) {
@@ -70,6 +78,19 @@ public Object convert(final SingleReadContext context) {
7078
if(ClassTypeUtil.isCollection(refType)) {
7179
return Instances.singletion(CollectionReadConverter.class).convert(context);
7280
}
81+
// 2.4 对象
82+
// 当前字段指定为 @CsvEntry 且为对象
83+
if(CsvFieldUtil.isEntryAble(field)) {
84+
final String nextSplit = CsvInnerUtil.getNextSplit(split);
85+
SingleReadContext singleReadContext = new SingleReadContext();
86+
singleReadContext.sort(sort)
87+
.value(value)
88+
.split(nextSplit)
89+
.classType(refType)
90+
.field(field)
91+
;
92+
return Instances.singletion(EntryReadConverter.class).convert(singleReadContext);
93+
}
7394

7495
// 3. 基本类型
7596
return this.convert(value, refType);

src/main/java/com/github/houbb/csv/support/convert/read/collection/ArrayReadConverter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import com.github.houbb.csv.support.context.SingleReadContext;
66
import com.github.houbb.csv.support.convert.read.CommonReadConverter;
77
import com.github.houbb.heaven.annotation.ThreadSafe;
8-
import com.github.houbb.heaven.constant.PunctuationConst;
98
import com.github.houbb.heaven.support.instance.impl.Instances;
10-
import com.github.houbb.heaven.util.lang.StringUtil;
119
import com.github.houbb.heaven.util.lang.reflect.ReflectFieldUtil;
1210

1311
import java.lang.reflect.Array;

0 commit comments

Comments
 (0)