Skip to content

Commit 750fc98

Browse files
committed
release branch 0.1.0
1 parent 8d23930 commit 750fc98

22 files changed

+563
-9
lines changed

CHANGE_LOG.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 变更日志
2+
3+
| 类型 | 说明 |
4+
|:----|:----|
5+
| A | 新增 |
6+
| U | 更新 |
7+
| D | 删除 |
8+
| T | 测试 |
9+
| O | 优化 |
10+
| F | 修复BUG |
11+
12+
# release_0.0.1
13+
14+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
15+
|:---|:---|:---|:---|:--|
16+
| 1 | A | 基本功能的实现 | 2019-6-1 13:55:32 | |
17+
18+
# release_0.0.2
19+
20+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
21+
|:---|:---|:---|:---|:--|
22+
| 1 | O | 注解相关测试用例的完善 | 2019-6-10 13:55:32 | |
23+
24+
# release_0.0.3
25+
26+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
27+
|:---|:---|:---|:---|:--|
28+
| 1 | A | 支持集合、数组、Map 等常见集合 | 2019-6-17 19:42:30 | |
29+
30+
# release_0.0.4
31+
32+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
33+
|:---|:---|:---|:---|:--|
34+
| 1 | O | 优化 head/bom 信息的写入 | 2019-6-18 20:32:36 | |
35+
| 2 | 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 | |
43+
44+
# release_0.0.6
45+
46+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
47+
|:---|:---|:---|:---|:--|
48+
| 1 | A | 支持特殊字符的转义 | 2019-6-20 17:23:30 | |
49+
50+
# release_0.0.7
51+
52+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
53+
|:---|:---|:---|:---|:--|
54+
| 1 | F | 修复文件生成序列化标识问题 | 2020-2-10 09:36:32 | |
55+
56+
# release_0.0.8
57+
58+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
59+
|:---|:---|:---|:---|:--|
60+
| 1 | A | 支持各种信息读取的新方式 | 2020-3-23 16:44:27 | |
61+
| 2 | A | 新增 CsvHelper,为操作提供便利性。 | 2020-3-23 16:44:27 | |
62+
| 3 | O | CsvReaderBs 引导类优化,便于后期调整 | 2020-3-23 16:44:27 | |
63+
64+
# release_0.0.9
65+
66+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
67+
|:---|:---|:---|:---|:--|
68+
| 1 | A | 支持文件写入为 APPEND 模式 | 2020-10-26 23:59:34 | |
69+
70+
# release_0.1.0
71+
72+
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
73+
|:---|:---|:---|:---|:--|
74+
| 1 | A | 新增枚举值映射处理 | 2020-10-26 23:59:34 | |

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747

4848
- 支持特殊字符转义
4949

50-
### v0.0.9 变更
50+
### v0.1.0 变更
5151

52-
- write() 写入文件方式调整
52+
- 枚举值映射
5353

54-
多次写入相同文件时,默认使用 APPEND 的方式对内容进行追加。
54+
支持快速简单的枚举值映射
5555

5656
# 快速开始
5757

doc/user/02-csv-注解使用.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@
3535
* @return 处理实现类
3636
*/
3737
Class<? extends IWriteConverter> writeConverter() default StringWriteConverter.class;
38+
39+
/**
40+
* 读映射
41+
*
42+
* S:成功;F:失败
43+
* 枚举值用 ; 分割,映射用 : 分割。
44+
*
45+
* 优先级:相比较 readConverter 优先使用 readMapping,如果值不为空,且当前字段的值为 String,才进行处理。否则忽略。
46+
* 注意:必须要有 : 分割符号,否则报错。只对 String 类型字段生效。
47+
* @return
48+
* @since 0.1.0
49+
*/
50+
String readMapping() default "";
51+
52+
/**
53+
* 写映射
54+
*
55+
* 优先级:相比较 writeConverter 优先使用 writeMapping,如果值不为空,且当前字段的值为 String,才进行处理。否则忽略。
56+
* 注意:必须要有 : 分割符号,否则报错。只对 String 类型字段生效。
57+
* @return 映射结果
58+
* @since 0.1.0
59+
*/
60+
String writeMapping() default "";
3861
```
3962

4063
## 属性概览表
@@ -46,6 +69,8 @@
4669
| writeRequire | true | 当前字段是否需要写入 csv 文件 |
4770
| readConverter | CommonReadConverter | 将 csv 中的字符串转化为当前字段类型,支持 8 大基本类型+String |
4871
| writeConverter | StringWriteConverter | 直接调用当前字段值 toString() 方法,null 直接为空字符串 |
72+
| readMapping | "" | 读取时的枚举值映射 |
73+
| writeMapping | "" | 写入时的枚举值映射 |
4974

5075
其中 readConverter/writeConverter 支持用户自定义
5176

@@ -156,3 +181,51 @@ public void annotationTest() {
156181
```
157182
[UserAnnotation{name='你好', password='null', birthday=Mon Jun 03 00:00:00 CST 2019}]
158183
```
184+
185+
# 枚举映射
186+
187+
## 对象定义
188+
189+
```java
190+
public class UserMapping {
191+
192+
@Csv(readMapping = "S:成功;F:失败", writeMapping = "S:成功;F:失败")
193+
private String status;
194+
195+
//getter/setter/toString()
196+
197+
}
198+
```
199+
200+
这种方法适合快速的枚举导出,如果比较复杂,可以使用对应的 converter 即可。
201+
202+
## 写入
203+
204+
```java
205+
UserMapping userMapping = new UserMapping();
206+
userMapping.status("S");
207+
208+
CsvHelper.write(Collections.singletonList(userMapping), CsvWriters.filePath("src\\test\\resources\\mapping.csv"));
209+
```
210+
211+
输出文件效果
212+
213+
```
214+
status
215+
成功
216+
```
217+
218+
枚举值 S 被映射成了成功。
219+
220+
## 读取
221+
222+
有写入,就有对应的读取。
223+
224+
```java
225+
final String path = "src\\test\\resources\\mapping.csv";
226+
227+
List<UserMapping> userList = CsvHelper.read(path, UserMapping.class);
228+
Assert.assertEquals("[UserMapping{status='S'}]", userList.toString());
229+
```
230+
231+
可以把成功映射为 S,便于一些枚举值的映射处理和存储。

release.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ ECHO "============================= RELEASE START..."
1010

1111
:: 版本号信息(需要手动指定)
1212
:::: 旧版本名称
13-
SET version=0.0.9
13+
SET version=0.1.0
1414
:::: 新版本名称
15-
SET newVersion=0.1.0
15+
SET newVersion=0.1.1
1616
:::: 组织名称
1717
SET groupName=com.github.houbb
1818
:::: 项目名称

src/main/java/com/github/houbb/csv/annotation/Csv.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@
4949
*/
5050
Class<? extends IWriteConverter> writeConverter() default CommonWriteConverter.class;
5151

52+
/**
53+
* 读映射
54+
*
55+
* S:成功;F:失败
56+
* 枚举值用 ; 分割,映射用 : 分割。
57+
*
58+
* 优先级:相比较 readConverter 优先使用 readMapping,如果值不为空,且当前字段的值为 String,才进行处理。否则忽略。
59+
* 注意:必须要有 : 分割符号,否则报错。只对 String 类型字段生效。
60+
* @return 读
61+
* @since 0.1.0
62+
*/
63+
String readMapping() default "";
64+
65+
/**
66+
* 写映射
67+
*
68+
* 优先级:相比较 writeConverter 优先使用 writeMapping,如果值不为空,且当前字段的值为 String,才进行处理。否则忽略。
69+
* 注意:必须要有 : 分割符号,否则报错。只对 String 类型字段生效。
70+
* @return 映射结果
71+
* @since 0.1.0
72+
*/
73+
String writeMapping() default "";
74+
5275
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.houbb.csv.api;
2+
3+
/**
4+
* 枚举值编码和描述接口
5+
* @author binbin.hou
6+
* @since 0.1.0
7+
*/
8+
public interface ICodeDesc {
9+
10+
/**
11+
* 获取编码
12+
* @return 编码
13+
* @since 0.1.0
14+
*/
15+
String getCode();
16+
17+
/**
18+
* 获取描述
19+
* @return 描述
20+
* @since 0.1.0
21+
*/
22+
String getDesc();
23+
24+
/**
25+
* 列表
26+
* @return 列表
27+
* @since 0.1.0
28+
*/
29+
ICodeDesc[] list();
30+
31+
}

src/main/java/com/github/houbb/csv/constant/CsvConst.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@ private CsvConst(){}
5454
*/
5555
public static final int DEFAULT_END_INDEX = Integer.MAX_VALUE;
5656

57+
/**
58+
* 映射分割符号
59+
* @since 0.1.0
60+
*/
61+
public static final String MAPPING_SPLITTER = ";";
62+
63+
/**
64+
* 每一组元素的分割符号
65+
* @since 0.1.0
66+
*/
67+
public static final String MAPPING_UNIT_SPLITTER = ":";
68+
5769
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.houbb.csv.support.context;
22

3+
import com.github.houbb.csv.annotation.Csv;
34
import com.github.houbb.heaven.support.sort.ISort;
45

56
import java.lang.reflect.Field;
@@ -55,6 +56,12 @@ public class SingleReadContext {
5556
*/
5657
private boolean escape;
5758

59+
/**
60+
* csv 注解信息
61+
* @since 0.1.0
62+
*/
63+
private Csv csv;
64+
5865
/**
5966
* 创建新的实例
6067
* @return 新的实例
@@ -126,4 +133,13 @@ public SingleReadContext escape(boolean escape) {
126133
this.escape = escape;
127134
return this;
128135
}
136+
137+
public Csv csv() {
138+
return csv;
139+
}
140+
141+
public SingleReadContext csv(Csv csv) {
142+
this.csv = csv;
143+
return this;
144+
}
129145
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.houbb.csv.support.context;
22

3+
import com.github.houbb.csv.annotation.Csv;
34
import com.github.houbb.heaven.support.sort.ISort;
45

56
import java.lang.reflect.Field;
@@ -63,6 +64,12 @@ public static SingleWriteContext newInstance() {
6364
*/
6465
private boolean escape;
6566

67+
/**
68+
* 注解信息
69+
* @since 0.1.0
70+
*/
71+
private Csv csv;
72+
6673
public ISort sort() {
6774
return sort;
6875
}
@@ -116,4 +123,13 @@ public SingleWriteContext escape(boolean escape) {
116123
this.escape = escape;
117124
return this;
118125
}
126+
127+
public Csv csv() {
128+
return csv;
129+
}
130+
131+
public SingleWriteContext csv(Csv csv) {
132+
this.csv = csv;
133+
return this;
134+
}
119135
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.houbb.csv.support.convert.mapping;
2+
3+
import com.github.houbb.csv.api.IReadConverter;
4+
import com.github.houbb.csv.support.context.SingleReadContext;
5+
import com.github.houbb.csv.util.CsvInnerUtil;
6+
import com.github.houbb.heaven.annotation.ThreadSafe;
7+
import com.github.houbb.heaven.util.lang.StringUtil;
8+
9+
import java.util.Map;
10+
11+
/**
12+
* 读取转换器,将 string 转换为 field
13+
* @author binbin.hou
14+
* @since 0.1.0
15+
*/
16+
@ThreadSafe
17+
public class ReadMappingConverter implements IReadConverter<String> {
18+
19+
@Override
20+
public String convert(SingleReadContext context) {
21+
String value = context.value();
22+
if(StringUtil.isEmpty(value)) {
23+
return value;
24+
}
25+
26+
Map<String, String> map = CsvInnerUtil.getMappingMap(context.csv().readMapping());
27+
for(Map.Entry<String, String> entry : map.entrySet()) {
28+
if(entry.getValue().equalsIgnoreCase(value)) {
29+
return entry.getKey();
30+
}
31+
}
32+
return value;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)