Skip to content

Commit 32dcded

Browse files
author
wubingheng
committed
optimize converting utils.
1 parent 04f4e6e commit 32dcded

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

Diff for: src/main/java/com/qiniu/convert/Converter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public List<T> convertToVList(List<E> lineList) {
2121
try {
2222
mapList.add(convertToV(line));
2323
} catch (Exception e) {
24-
errorList.add(JsonUtils.toJson(line) + "\tcvterr " + e.getMessage());
24+
errorList.add(JsonUtils.toJson(line) + "\tconvert error " + e.getMessage());
2525
}
2626
}
2727
}

Diff for: src/main/java/com/qiniu/process/filtration/BaseFilter.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.qiniu.process.filtration;
22

3+
import com.qiniu.util.ConvertingUtils;
4+
35
import java.io.IOException;
46
import java.time.LocalDateTime;
57
import java.util.List;
@@ -111,7 +113,7 @@ public boolean filterKey(T item) {
111113
public boolean filterMimeType(T item) {
112114
try {
113115
if (item == null) return false;
114-
String mType = valueFrom(item, "mime");
116+
String mType = valueFrom(item, ConvertingUtils.defaultMimeField);
115117
boolean result = false;
116118
if (checkList(mimeType)) {
117119
result = mimeType.stream().anyMatch(mType::contains);
@@ -127,7 +129,7 @@ public boolean filterMimeType(T item) {
127129
public boolean filterDatetime(T item) {
128130
try {
129131
if (item == null) return false;
130-
LocalDateTime localDateTime = LocalDateTime.parse(valueFrom(item, "datetime"));
132+
LocalDateTime localDateTime = LocalDateTime.parse(valueFrom(item, ConvertingUtils.defaultDatetimeField));
131133
return localDateTime.compareTo(datetimeMax) <= 0 && localDateTime.compareTo(datetimeMin) >= 0;
132134
} catch (Exception e) {
133135
return true;
@@ -137,7 +139,7 @@ public boolean filterDatetime(T item) {
137139
public boolean filterType(T item) {
138140
try {
139141
if (item == null) return false;
140-
return valueFrom(item, "type").equals(type);
142+
return valueFrom(item, ConvertingUtils.defaultTypeField).equals(type);
141143
} catch (NullPointerException e) {
142144
return true;
143145
}
@@ -146,7 +148,7 @@ public boolean filterType(T item) {
146148
public boolean filterStatus(T item) {
147149
try {
148150
if (item == null) return false;
149-
return valueFrom(item, "status").equals(status);
151+
return valueFrom(item, ConvertingUtils.defaultStatusField).equals(status);
150152
} catch (NullPointerException e) {
151153
return true;
152154
}

Diff for: src/main/java/com/qiniu/process/filtration/SeniorFilter.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonObject;
66
import com.google.gson.reflect.TypeToken;
77
import com.qiniu.config.JsonFile;
8+
import com.qiniu.util.ConvertingUtils;
89
import com.qiniu.util.JsonUtils;
910

1011
import java.io.IOException;
@@ -59,7 +60,8 @@ public List<T> checkMimeType(List<T> lineList) {
5960
if (line == null) continue;
6061
key = valueFrom(line, "key");
6162
if (key.contains(".")) {
62-
String finalKeyMimePair = key.substring(key.lastIndexOf(".") + 1) + ":" + valueFrom(line, "mime");
63+
String finalKeyMimePair = key.substring(key.lastIndexOf(".") + 1) + ":" +
64+
valueFrom(line, ConvertingUtils.defaultMimeField);
6365
if (extMimeList.parallelStream().anyMatch(extMime ->
6466
finalKeyMimePair.split("/")[0].equalsIgnoreCase(extMime))) {
6567
continue;
@@ -83,7 +85,8 @@ public boolean checkMimeType(T line) {
8385
try {
8486
key = valueFrom(line, "key");
8587
if (key.contains(".")) {
86-
String finalKeyMimePair = key.substring(key.lastIndexOf(".") + 1) + ":" + valueFrom(line, "mime");
88+
String finalKeyMimePair = key.substring(key.lastIndexOf(".") + 1) + ":" +
89+
valueFrom(line, ConvertingUtils.defaultMimeField);
8790
if (extMimeList.parallelStream().anyMatch(extMime ->
8891
finalKeyMimePair.split("/")[0].equalsIgnoreCase(extMime))) {
8992
return false;

Diff for: src/main/java/com/qiniu/util/ConvertingUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static <T> T toPair(JsonObject json, Map<String, String> indexMap, KeyVal
275275
for (String index : indexMap.keySet()) {
276276
jsonElement = json.get(index);
277277
if (jsonElement == null || jsonElement instanceof JsonNull) {
278-
throw new IOException("the index: " + index + " can't be found in " + json);
278+
if (!allFieldsSet.contains(index)) throw new IOException("the index: " + index + " can't be found in " + json);
279279
} else {
280280
pair.put(indexMap.get(index), JsonUtils.toString(jsonElement));
281281
}
@@ -314,7 +314,7 @@ public static <T> T toPair(Map<String, String> line, List<String> fields, KeyVal
314314
else if (intFields.contains(field)) pair.put(field, Integer.valueOf(value));
315315
else pair.put(field, value);
316316
} else {
317-
throw new IOException("the field: " + field + " can't be found in " + line);
317+
if (!allFieldsSet.contains(field)) throw new IOException("the field: " + field + " can't be found in " + line);
318318
}
319319
}
320320
if (pair.size() == 0) throw new IOException("empty result string.");

0 commit comments

Comments
 (0)