[Task] refactor nest block to improve readability by guard
Description
There are some code with deep nest block, which are hard to read and understand. We can refactor nest block to improve readability by guard
For example, in CollectUtil.java:
JsonObject jsonObject = jsonElement.getAsJsonObject();
Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
JsonElement element = entry.getValue();
// Replace normal VALUE value
if (element.isJsonPrimitive()) {
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (cryingMatcher.find()) {
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param != null) {
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
}
jsonObject.addProperty(entry.getKey(), value);
}
} else {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
}
}
can be replaced by:
JsonObject jsonObject = jsonElement.getAsJsonObject();
Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
JsonElement element = entry.getValue();
if (!element.isJsonPrimitive()) {
jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
continue;
}
// Replace normal VALUE value
// Check if there are special characters Replace
String value = element.getAsString();
Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
if (!cryingMatcher.find()) {
continue;
}
cryingMatcher.reset();
while (cryingMatcher.find()) {
String group = cryingMatcher.group();
String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
Configmap param = configmap.get(replaceField);
if (param == null) {
continue;
}
if (param.getValue() == null) {
if (group.length() == value.length()) {
value = null;
break;
} else {
value = value.replace(group, "");
}
} else {
value = value.replace(group, (String) param.getValue());
}
}
jsonObject.addProperty(entry.getKey(), value);
Task List
- [ ] collector/src/main/java/org/apache/hertzbeat/collector/util/CollectUtil.java
hi, this look good. 👍 Welcome help us refactor submit pr.
I would like to help. Can let me know how to get started ? That'd be great help.
I would like to help. Can let me know how to get started ? That'd be great help.
@ankurnotwarikoo Hello, you can refer to the following links to contribute code to Apache hertzbeat. We look forward to your participation. https://hertzbeat.apache.org/docs/community/contribution https://hertzbeat.apache.org/docs/community/document https://hertzbeat.apache.org/docs/community/submit_code https://hertzbeat.apache.org/docs/community/code_style_and_quality_guide
I have opened the PR, though the PR is linked to this issue, however from PR not able to link the issue, not seeing any button in development section in PR to link this issue
I have opened the PR, though the PR is linked to this issue, however from PR not able to link the issue, not seeing any button in development section in PR to link this issue
hi sorry for missing this, the pr has been merged.