Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Jan 29, 2024
1 parent 344ddf4 commit f3a2aa0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@ ds_1:
password:
```
用例:
- `jdbc:shardingsphere:classpath-environment:config.yaml`

### 其他实现
具体可参考 https://github.com/apache/shardingsphere-plugin 。
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ ds_1:
password:
```
Example:
- `jdbc:shardingsphere:classpath-environment:config.yaml`

### Other implementations
For details, please refer to https://github.com/apache/shardingsphere-plugin .
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,20 @@ public byte[] getContent(final String url, final String urlPrefix) {

private String replaceEnvironmentVariables(final String line) {
Matcher matcher = PATTERN.matcher(line);
if (matcher.find()) {
StringBuffer modifiedLine = new StringBuffer();
String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2);
String envName = envNameAndDefaultValue[0];
String envValue = getEnvironmentVariables(envName);
if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) {
matcher.appendReplacement(modifiedLine, "");
return modifiedLine.substring(0, modifiedLine.length() - 1);
}
if (Strings.isNullOrEmpty(envValue)) {
envValue = envNameAndDefaultValue[1];
}
matcher.appendReplacement(modifiedLine, envValue);
return modifiedLine.toString();
if (!matcher.find()) {
return line;
}
String[] envNameAndDefaultValue = matcher.group(1).split(KEY_VALUE_SEPARATOR, 2);
String envName = envNameAndDefaultValue[0];
String envValue = getEnvironmentVariables(envName);
if (Strings.isNullOrEmpty(envValue) && envNameAndDefaultValue[1].isEmpty()) {
String modifiedLineWithSpace = matcher.replaceAll("");
return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1);
}
if (Strings.isNullOrEmpty(envValue)) {
envValue = envNameAndDefaultValue[1];
}
return line;
return matcher.replaceAll(envValue);
}

/**
Expand Down

0 comments on commit f3a2aa0

Please sign in to comment.