You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2024/10/29 16:11:33 UTC
(shardingsphere) branch master updated: Refactor MaskRuleConfigurationToDistSQLConverter (#33460)
This is an automated email from the ASF dual-hosted git repository.
chengzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d02f936c88f Refactor MaskRuleConfigurationToDistSQLConverter (#33460)
d02f936c88f is described below
commit d02f936c88fe753d1a1e2512910b2c622de08565
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Oct 30 00:11:28 2024 +0800
Refactor MaskRuleConfigurationToDistSQLConverter (#33460)
* Add more test cases on MaskRuleConfigurationToDistSQLConverter
* Add more test cases on MaskRuleConfigurationToDistSQLConverter
* Add more test cases on MaskRuleConfigurationToDistSQLConverter
* Add more test cases on MaskRuleConfigurationToDistSQLConverter
---
.../converter/MaskConvertDistSQLConstants.java | 2 +-
.../MaskRuleConfigurationToDistSQLConverter.java | 43 ++++++++--------------
...askRuleConfigurationToDistSQLConverterTest.java | 14 ++++---
3 files changed, 25 insertions(+), 34 deletions(-)
diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskConvertDistSQLConstants.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskConvertDistSQLConstants.java
index 3d6a9ec0c78..236b2f12679 100644
--- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskConvertDistSQLConstants.java
+++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskConvertDistSQLConstants.java
@@ -28,7 +28,7 @@ public final class MaskConvertDistSQLConstants {
public static final String CREATE_MASK_RULE = "CREATE MASK RULE";
- public static final String MASK_RULE = " %s ("
+ public static final String MASK_TABLE = " %s ("
+ System.lineSeparator()
+ "COLUMNS("
+ System.lineSeparator()
diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverter.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverter.java
index fec4de69af4..2f35dd9213e 100644
--- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverter.java
+++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverter.java
@@ -26,8 +26,8 @@ import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import java.util.Collection;
-import java.util.Iterator;
import java.util.Map;
+import java.util.stream.Collectors;
/**
* Mask rule configuration to DistSQL converter.
@@ -36,37 +36,24 @@ public final class MaskRuleConfigurationToDistSQLConverter implements RuleConfig
@Override
public String convert(final MaskRuleConfiguration ruleConfig) {
- if (ruleConfig.getTables().isEmpty()) {
- return "";
- }
- StringBuilder result = new StringBuilder(MaskConvertDistSQLConstants.CREATE_MASK_RULE);
- Iterator<MaskTableRuleConfiguration> iterator = ruleConfig.getTables().iterator();
- while (iterator.hasNext()) {
- MaskTableRuleConfiguration tableRuleConfig = iterator.next();
- result.append(String.format(MaskConvertDistSQLConstants.MASK_RULE, tableRuleConfig.getName(), getMaskColumns(tableRuleConfig.getColumns(), ruleConfig.getMaskAlgorithms())));
- if (iterator.hasNext()) {
- result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
- }
- }
- result.append(DistSQLConstants.SEMI);
- return result.toString();
+ return ruleConfig.getTables().isEmpty() ? "" : MaskConvertDistSQLConstants.CREATE_MASK_RULE + convertMaskTables(ruleConfig) + DistSQLConstants.SEMI;
}
- private String getMaskColumns(final Collection<MaskColumnRuleConfiguration> columnRuleConfig, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
- StringBuilder result = new StringBuilder();
- Iterator<MaskColumnRuleConfiguration> iterator = columnRuleConfig.iterator();
- while (iterator.hasNext()) {
- MaskColumnRuleConfiguration column = iterator.next();
- result.append(String.format(MaskConvertDistSQLConstants.MASK_COLUMN, column.getLogicColumn(), getMaskAlgorithms(column, maskAlgorithms)));
- if (iterator.hasNext()) {
- result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
- }
- }
- return result.toString();
+ private String convertMaskTables(final MaskRuleConfiguration ruleConfig) {
+ return ruleConfig.getTables().stream().map(each -> convertMaskTable(each, ruleConfig.getMaskAlgorithms())).collect(Collectors.joining(DistSQLConstants.COMMA + System.lineSeparator()));
}
- private String getMaskAlgorithms(final MaskColumnRuleConfiguration columnRuleConfig, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
- return AlgorithmDistSQLConverter.getAlgorithmType(maskAlgorithms.get(columnRuleConfig.getMaskAlgorithm()));
+ private String convertMaskTable(final MaskTableRuleConfiguration tableRuleConfig, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
+ return String.format(MaskConvertDistSQLConstants.MASK_TABLE, tableRuleConfig.getName(), convertMaskColumns(tableRuleConfig.getColumns(), maskAlgorithms));
+ }
+
+ private String convertMaskColumns(final Collection<MaskColumnRuleConfiguration> columnRuleConfigs, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
+ return columnRuleConfigs.stream().map(each -> convertMaskColumn(each, maskAlgorithms)).collect(Collectors.joining(DistSQLConstants.COMMA + System.lineSeparator()));
+ }
+
+ private String convertMaskColumn(final MaskColumnRuleConfiguration columnRuleConfig, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
+ return String.format(MaskConvertDistSQLConstants.MASK_COLUMN,
+ columnRuleConfig.getLogicColumn(), AlgorithmDistSQLConverter.getAlgorithmType(maskAlgorithms.get(columnRuleConfig.getMaskAlgorithm())));
}
@Override
diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
index 48cb5730575..e0842b8c55b 100644
--- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
+++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/converter/MaskRuleConfigurationToDistSQLConverterTest.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.junit.jupiter.api.Test;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
@@ -48,14 +49,17 @@ class MaskRuleConfigurationToDistSQLConverterTest {
@Test
void assertConvert() {
MaskRuleConfiguration maskRuleConfig = getMaskRuleConfiguration();
- assertThat(converter.convert(maskRuleConfig),
- is("CREATE MASK RULE foo_tbl (" + System.lineSeparator() + "COLUMNS(" + System.lineSeparator() + "(NAME=foo_col, TYPE(NAME='md5'))" + System.lineSeparator() + "));"));
+ assertThat(converter.convert(maskRuleConfig), is("CREATE MASK RULE"
+ + " foo_tbl (" + System.lineSeparator() + "COLUMNS(" + System.lineSeparator() + "(NAME=foo_col_1, TYPE(NAME='md5'))," + System.lineSeparator() + "(NAME=foo_col_2, TYPE(NAME='md5'))"
+ + System.lineSeparator() + "))," + System.lineSeparator()
+ + " bar_tbl (" + System.lineSeparator() + "COLUMNS(" + System.lineSeparator() + "(NAME=bar_col, TYPE(NAME='md5'))" + System.lineSeparator() + "));"));
}
private MaskRuleConfiguration getMaskRuleConfiguration() {
- MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("foo_col", "foo_tbl_foo_col_md5");
- MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("foo_tbl", Collections.singleton(maskColumnRuleConfig));
+ MaskTableRuleConfiguration fooTableRuleConfig = new MaskTableRuleConfiguration("foo_tbl",
+ Arrays.asList(new MaskColumnRuleConfiguration("foo_col_1", "md5_algo"), new MaskColumnRuleConfiguration("foo_col_2", "md5_algo")));
+ MaskTableRuleConfiguration barTableRuleConfig = new MaskTableRuleConfiguration("bar_tbl", Collections.singleton(new MaskColumnRuleConfiguration("bar_col", "md5_algo")));
AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties());
- return new MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig), Collections.singletonMap("foo_tbl_foo_col_md5", algorithmConfig));
+ return new MaskRuleConfiguration(Arrays.asList(fooTableRuleConfig, barTableRuleConfig), Collections.singletonMap("md5_algo", algorithmConfig));
}
}