Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Task] refactor nest block to improve readability by guard #2056

Open
1 task
Thespica opened this issue May 31, 2024 · 4 comments
Open
1 task

[Task] refactor nest block to improve readability by guard #2056

Thespica opened this issue May 31, 2024 · 4 comments

Comments

@Thespica
Copy link
Contributor

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
@tomsun28
Copy link
Contributor

tomsun28 commented Jun 1, 2024

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

@ankurnotwarikoo
Copy link

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

@LiuTianyou
Copy link
Contributor

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

@hasimmollah
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants