hertzbeat icon indicating copy to clipboard operation
hertzbeat copied to clipboard

[Task] refactor nest block to improve readability by guard

Open Thespica opened this issue 1 month ago • 3 comments

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

Thespica avatar May 31 '24 15:05 Thespica