Skip to content

Commit 0ecc2c1

Browse files
committed
add info to parser yml to obj
1 parent 2e0e794 commit 0ecc2c1

File tree

4 files changed

+30
-82
lines changed

4 files changed

+30
-82
lines changed

platform-api/src/main/java/com/flow/platform/api/util/CommonUtil.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.flow.platform.api.util;
1818

19-
import com.google.common.base.Strings;
2019
import java.math.BigInteger;
2120
import java.time.LocalDateTime;
2221
import java.time.format.DateTimeFormatter;

platform-api/src/main/java/com/flow/platform/api/util/NodeUtil.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import com.flow.platform.api.domain.node.Node;
2121
import com.flow.platform.api.exception.YmlException;
2222
import com.flow.platform.yml.parser.YmlParser;
23-
import com.flow.platform.yml.parser.exception.YmlParseException;
2423
import com.flow.platform.yml.parser.exception.YmlFormatException;
25-
import com.google.common.base.Strings;
24+
import com.flow.platform.yml.parser.exception.YmlParseException;
2625
import com.google.common.io.Files;
2726
import java.io.File;
2827
import java.nio.file.Path;
@@ -73,9 +72,9 @@ public static Node buildFromYml(String yml, String root) {
7372
try {
7473
flows = YmlParser.fromYml(yml, Flow[].class);
7574
} catch (YmlParseException e) {
76-
throw new YmlException("Yml parser error", e);
75+
throw new YmlException(e.getMessage());
7776
} catch (YmlFormatException e) {
78-
throw new YmlException("Yml validate error", e);
77+
throw new YmlException(e.getMessage());
7978
}
8079

8180
// current version only support single flow

platform-yml-parser/src/main/java/com/flow/platform/yml/parser/YmlParser.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ public class YmlParser {
3434

3535
private final static YamlConfig yamlConfig = new YamlConfig();
3636

37+
private final static String YML_ILLEGAL_MESSAGE = "Yml illegal format, please confirm yml format";
38+
private final static String YML_CONVERT_MESSAGE = "Object to yml error, please retry";
39+
3740
static {
3841
yamlConfig.writeConfig.setAutoAnchor(false);
3942
yamlConfig.writeConfig.setWriteClassname(WriteClassName.NEVER);
4043
}
4144

4245
private static <T> T fromObject(Object o, Type typeOfT) {
4346
if (o == null) {
44-
throw new YmlParseException("yml parser error");
47+
throw new YmlParseException(YML_ILLEGAL_MESSAGE);
4548
}
4649
return (T) TypeAdaptorFactory.getAdaptor(typeOfT).read(o);
4750
}
@@ -63,7 +66,7 @@ public static <T> T fromYml(String str, Type typeOfT) {
6366
YamlReader yamlReader = new YamlReader(str, yamlConfig);
6467
result = (Map) yamlReader.read();
6568
} catch (Throwable throwable) {
66-
throw new YmlParseException("Load Yml error");
69+
throw new YmlParseException(YML_ILLEGAL_MESSAGE);
6770
}
6871
return fromObject(result.get("flow"), typeOfT);
6972
}
@@ -78,14 +81,14 @@ public static <T> String toYml(T t) {
7881
Map<String, Object> map = new HashMap<>();
7982
Object o = toObject(t);
8083
map.put("flow", o);
81-
String yml = null;
84+
String yml;
8285

8386
try (Writer stringWriter = new StringWriter()) {
8487
YamlWriter writer = new YamlWriter(stringWriter, yamlConfig);
8588
writer.write(map);
8689
yml = stringWriter.toString();
8790
} catch (Throwable throwable) {
88-
throw new YmlParseException(String.format("Object to yaml error"), throwable);
91+
throw new YmlParseException(YML_CONVERT_MESSAGE, throwable);
8992
}
9093

9194
return yml;

platform-yml-parser/src/main/java/com/flow/platform/yml/parser/adaptor/YmlAdaptor.java

Lines changed: 20 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
8080
YmlSerializer ymlSerializer = field.getAnnotation(YmlSerializer.class);
8181

8282
//filter no annotations field
83-
if (noAnnotationField(field)) {
83+
if (isNullOrEmpty(ymlSerializer)) {
8484
continue;
8585
}
8686

8787
//filter ignore field
88-
if (ignoreField(field)) {
88+
if (isIgnore(ymlSerializer)) {
8989
continue;
9090
}
9191
setValueToField(o, field, instance, ymlSerializer, clazz);
@@ -96,12 +96,12 @@ protected <T> T doRead(Object o, Class<T> clazz) {
9696
YmlSerializer ymlSerializer = method.getAnnotation(YmlSerializer.class);
9797

9898
//filter no annotations method
99-
if (noAnnotationMethod(method)) {
99+
if (isNullOrEmpty(ymlSerializer)) {
100100
continue;
101101
}
102102

103103
//filter ignore method
104-
if (ignoreMethod(method)) {
104+
if (isIgnore(ymlSerializer)) {
105105
continue;
106106
}
107107

@@ -120,9 +120,9 @@ private <T> void setValueToField(Object o, Field field, T instance, YmlSerialize
120120
Object obj = ((Map) o).get(getAnnotationMappingName(field.getName(), ymlSerializer));
121121

122122
// required field
123-
if (requiredField(field)) {
123+
if (requiredField(ymlSerializer)) {
124124
if (obj == null) {
125-
throw new YmlParseException(String.format("required field - %s", field.getName()));
125+
throw new YmlParseException(String.format("required field - %s , please confirm", field.getName()));
126126
}
127127
}
128128

@@ -170,7 +170,7 @@ protected <T> Object doWrite(Field field, T t, YmlSerializer ymlSerializer) {
170170
//annotation provider adaptor
171171
if (ymlSerializer.adaptor() != EmptyAdapter.class) {
172172
try {
173-
YmlAdaptor instance = (YmlAdaptor) ymlSerializer.adaptor().newInstance();
173+
YmlAdaptor instance = ymlSerializer.adaptor().newInstance();
174174
return instance.write(field.get(t));
175175
} catch (Throwable throwable) {
176176
throw new YmlParseException(
@@ -254,7 +254,7 @@ public <T> Object doWrite(T clazz) {
254254
// 获取 field 对应的值
255255
YmlSerializer ymlSerializer = field.getAnnotation(YmlSerializer.class);
256256

257-
if (noAnnotationField(field)) {
257+
if (isNullOrEmpty(ymlSerializer)) {
258258
continue;
259259
}
260260

@@ -267,7 +267,7 @@ public <T> Object doWrite(T clazz) {
267267
// 获取 field 对应的值
268268
YmlSerializer ymlSerializer = method.getAnnotation(YmlSerializer.class);
269269

270-
if (noAnnotationMethod(method)) {
270+
if (isNullOrEmpty(ymlSerializer)) {
271271
continue;
272272
}
273273

@@ -300,12 +300,10 @@ private String getAnnotationMappingName(String s, YmlSerializer ymlSerializer) {
300300

301301
/**
302302
* detect required field from annotation
303-
*
304-
* @param field field
305-
* @return True or false
303+
* @param annotation
304+
* @return
306305
*/
307-
private Boolean requiredField(Field field) {
308-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
306+
private Boolean requiredField(YmlSerializer annotation) {
309307
if (annotation == null) {
310308
return false;
311309
}
@@ -318,10 +316,11 @@ private Boolean requiredField(Field field) {
318316
}
319317

320318
/**
321-
* filter no annotation field
319+
* detect annotation has or not
320+
* @param annotation
321+
* @return
322322
*/
323-
private Boolean noAnnotationField(Field field) {
324-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
323+
private Boolean isNullOrEmpty(YmlSerializer annotation) {
325324
if (annotation == null) {
326325
return true;
327326
}
@@ -330,10 +329,11 @@ private Boolean noAnnotationField(Field field) {
330329
}
331330

332331
/**
333-
* filter ignore field
332+
* filter ignore action
333+
* @param annotation
334+
* @return
334335
*/
335-
private Boolean ignoreField(Field field) {
336-
YmlSerializer annotation = field.getAnnotation(YmlSerializer.class);
336+
private Boolean isIgnore(YmlSerializer annotation) {
337337
if (annotation.ignore()) {
338338
return true;
339339
}
@@ -342,55 +342,6 @@ private Boolean ignoreField(Field field) {
342342
}
343343

344344

345-
/**
346-
* filter no annotation method
347-
*/
348-
private Boolean noAnnotationMethod(Method method) {
349-
YmlSerializer annotation = method.getAnnotation(YmlSerializer.class);
350-
if (annotation == null) {
351-
return true;
352-
}
353-
354-
return false;
355-
}
356-
357-
/**
358-
* filter ignore method
359-
*/
360-
private Boolean ignoreMethod(Method method) {
361-
YmlSerializer annotation = method.getAnnotation(YmlSerializer.class);
362-
if (annotation.ignore()) {
363-
return true;
364-
}
365-
366-
return false;
367-
}
368-
369-
370-
/**
371-
* get all fields
372-
*
373-
* @return Map<name, field>
374-
*/
375-
private Map<String, Field> allFields(Class<?> clazz) {
376-
Map<String, Field> map = new HashMap<>();
377-
378-
while (true) {
379-
if (clazz == null) {
380-
break;
381-
}
382-
383-
for (Field field : clazz.getDeclaredFields()) {
384-
map.put(field.getName(), field);
385-
}
386-
387-
clazz = clazz.getSuperclass();
388-
}
389-
390-
return map;
391-
}
392-
393-
394345
private String fieldNameForSetterGetter(String fieldName) {
395346
return Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
396347
}
@@ -411,10 +362,6 @@ private Method getGetterMethod(Field field, Class<?> clazz) {
411362
return getMethod(field, clazz, "get");
412363
}
413364

414-
private Method getSetterMethod(Field field, Class<?> clazz) {
415-
return getMethod(field, clazz, "set");
416-
}
417-
418365
/**
419366
* get method
420367
*/

0 commit comments

Comments
 (0)