This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 ec7eff957f5 Add compatible encrypt configuration swapper (#26082)
ec7eff957f5 is described below

commit ec7eff957f50efac29097ba93a399e4150ad550e
Author: ChenJiaHao <[email protected]>
AuthorDate: Wed Jun 7 10:27:58 2023 +0800

    Add compatible encrypt configuration swapper (#26082)
    
    * Add compatible encrypt configuration swapper
    
    * Fix code style
---
 ...lCompatibleEncryptRuleConfigurationSwapper.java | 81 ++++++++++++++++++++++
 ...patibleEncryptRuleConfigurationSwapperTest.java | 64 +++++++++++++++++
 2 files changed, 145 insertions(+)

diff --git 
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapper.java
 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..aac2ab3dea1
--- /dev/null
+++ 
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapper.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.yaml.swapper;
+
+import 
org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration;
+import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
+import 
org.apache.shardingsphere.encrypt.metadata.converter.EncryptNodeConverter;
+import 
org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlCompatibleEncryptTableRuleConfigurationSwapper;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapper;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Map.Entry;
+
+/**
+ * TODO Rename to YamlCompatibleEncryptRuleConfigurationSwapper when metadata 
structure adjustment
+ * YAML encrypt rule configuration swapper.
+ *
+ * @deprecated Should use new api, compatible api will remove in next version.
+ */
+@Deprecated
+public final class NewYamlCompatibleEncryptRuleConfigurationSwapper implements 
NewYamlRuleConfigurationSwapper<CompatibleEncryptRuleConfiguration> {
+    
+    private final YamlCompatibleEncryptTableRuleConfigurationSwapper 
tableSwapper = new YamlCompatibleEncryptTableRuleConfigurationSwapper();
+    
+    private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new 
YamlAlgorithmConfigurationSwapper();
+    
+    @Override
+    public Collection<YamlDataNode> swapToDataNodes(final 
CompatibleEncryptRuleConfiguration data) {
+        Collection<YamlDataNode> result = new LinkedHashSet<>();
+        for (EncryptTableRuleConfiguration each : data.getTables()) {
+            result.add(new 
YamlDataNode(EncryptNodeConverter.getTableNamePath(each.getName()), 
YamlEngine.marshal(tableSwapper.swapToYamlConfiguration(each))));
+        }
+        for (Entry<String, AlgorithmConfiguration> entry : 
data.getEncryptors().entrySet()) {
+            result.add(new 
YamlDataNode(EncryptNodeConverter.getEncryptorPath(entry.getKey()), 
YamlEngine.marshal(algorithmSwapper.swapToYamlConfiguration(entry.getValue()))));
+        }
+        return result;
+    }
+    
+    @Override
+    public CompatibleEncryptRuleConfiguration swapToObject(final 
Collection<YamlDataNode> dataNodes) {
+        // TODO to be completed
+        return new CompatibleEncryptRuleConfiguration(Collections.emptyList(), 
Collections.emptyMap());
+    }
+    
+    @Override
+    public Class<CompatibleEncryptRuleConfiguration> getTypeClass() {
+        return CompatibleEncryptRuleConfiguration.class;
+    }
+    
+    @Override
+    public String getRuleTagName() {
+        return "COMPATIBLE_ENCRYPT";
+    }
+    
+    @Override
+    public int getOrder() {
+        return EncryptOrder.COMPATIBLE_ORDER;
+    }
+}
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapperTest.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..69b4c805ffb
--- /dev/null
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlCompatibleEncryptRuleConfigurationSwapperTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.yaml.swapper;
+
+import 
org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration;
+import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration;
+import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
+import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+@Deprecated
+class NewYamlCompatibleEncryptRuleConfigurationSwapperTest {
+    
+    private final NewYamlCompatibleEncryptRuleConfigurationSwapper swapper = 
new NewYamlCompatibleEncryptRuleConfigurationSwapper();
+    
+    @Test
+    void assertSwapEmptyConfigToDataNodes() {
+        CompatibleEncryptRuleConfiguration config = new 
CompatibleEncryptRuleConfiguration(Collections.emptyList(), 
Collections.emptyMap());
+        Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+        assertThat(result.size(), is(0));
+    }
+    
+    @Test
+    void assertSwapFullConfigToDataNodes() {
+        CompatibleEncryptRuleConfiguration config = createMaximumEncryptRule();
+        Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+        assertThat(result.size(), is(2));
+        Iterator<YamlDataNode> iterator = result.iterator();
+        assertThat(iterator.next().getKey(), is("tables/foo"));
+        assertThat(iterator.next().getKey(), is("encryptors/FOO"));
+    }
+    
+    private CompatibleEncryptRuleConfiguration createMaximumEncryptRule() {
+        Collection<EncryptTableRuleConfiguration> tables = new LinkedList<>();
+        tables.add(new EncryptTableRuleConfiguration("foo", 
Collections.singleton(new EncryptColumnRuleConfiguration("foo_column", new 
EncryptColumnItemRuleConfiguration("FIXTURE")))));
+        return new CompatibleEncryptRuleConfiguration(tables, 
Collections.singletonMap("FOO", new AlgorithmConfiguration("FOO", new 
Properties())));
+    }
+}

Reply via email to