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 year 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

hi, this look good. 👍 Welcome help us refactor submit pr.

tomsun28 avatar Jun 01 '24 01:06 tomsun28

I would like to help. Can let me know how to get started ? That'd be great help.

ankurnotwarikoo avatar Jun 04 '24 03:06 ankurnotwarikoo

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

LiuTianyou avatar Jun 12 '24 13:06 LiuTianyou

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

hasimmollah avatar Nov 23 '24 10:11 hasimmollah

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.

tomsun28 avatar Jan 26 '25 08:01 tomsun28