Skip to content

Commit

Permalink
Refactor MaskRuleConfigurationToDistSQLConverter (#33460)
Browse files Browse the repository at this point in the history
* Add more test cases on MaskRuleConfigurationToDistSQLConverter

* Add more test cases on MaskRuleConfigurationToDistSQLConverter

* Add more test cases on MaskRuleConfigurationToDistSQLConverter

* Add more test cases on MaskRuleConfigurationToDistSQLConverter
  • Loading branch information
terrymanu authored Oct 29, 2024
1 parent 5e27981 commit d02f936
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
}

0 comments on commit d02f936

Please sign in to comment.