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 3d6a9ec0c78f6..236b2f1267904 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 fec4de69af4ce..2f35dd9213e73 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.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 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 columnRuleConfig, final Map maskAlgorithms) { - StringBuilder result = new StringBuilder(); - Iterator 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 maskAlgorithms) { - return AlgorithmDistSQLConverter.getAlgorithmType(maskAlgorithms.get(columnRuleConfig.getMaskAlgorithm())); + private String convertMaskTable(final MaskTableRuleConfiguration tableRuleConfig, final Map maskAlgorithms) { + return String.format(MaskConvertDistSQLConstants.MASK_TABLE, tableRuleConfig.getName(), convertMaskColumns(tableRuleConfig.getColumns(), maskAlgorithms)); + } + + private String convertMaskColumns(final Collection columnRuleConfigs, final Map maskAlgorithms) { + return columnRuleConfigs.stream().map(each -> convertMaskColumn(each, maskAlgorithms)).collect(Collectors.joining(DistSQLConstants.COMMA + System.lineSeparator())); + } + + private String convertMaskColumn(final MaskColumnRuleConfiguration columnRuleConfig, final Map 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 48cb573057537..e0842b8c55b58 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.MaskTableRuleConfiguration; import org.junit.jupiter.api.Test; +import java.util.Arrays; import java.util.Collections; import java.util.Properties; @@ -48,14 +49,17 @@ void assertConvertWithEmptyTables() { @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)); } }