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

zhangliang 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 cb70d6b67ab Add YamlColumnSwapper (#33247)
cb70d6b67ab is described below

commit cb70d6b67aba6286058c3e0584541bb6058221f9
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Oct 15 00:53:46 2024 +0800

    Add YamlColumnSwapper (#33247)
---
 .../yaml/schema/swapper/YamlColumnSwapper.java     | 48 ++++++++++++
 .../yaml/schema/swapper/YamlConstraintSwapper.java | 41 ++++++++++
 .../yaml/schema/swapper/YamlIndexSwapper.java      | 45 +++++++++++
 .../yaml/schema/swapper/YamlTableSwapper.java      | 88 ++++++----------------
 4 files changed, 159 insertions(+), 63 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlColumnSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlColumnSwapper.java
new file mode 100644
index 00000000000..45eb0ada072
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlColumnSwapper.java
@@ -0,0 +1,48 @@
+/*
+ * 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.infra.yaml.schema.swapper;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
+import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumn;
+
+/**
+ * YAML column swapper.
+ */
+public final class YamlColumnSwapper implements 
YamlConfigurationSwapper<YamlShardingSphereColumn, ShardingSphereColumn> {
+    
+    @Override
+    public YamlShardingSphereColumn swapToYamlConfiguration(final 
ShardingSphereColumn data) {
+        YamlShardingSphereColumn result = new YamlShardingSphereColumn();
+        result.setName(data.getName());
+        result.setDataType(data.getDataType());
+        result.setPrimaryKey(data.isPrimaryKey());
+        result.setGenerated(data.isGenerated());
+        result.setCaseSensitive(data.isCaseSensitive());
+        result.setVisible(data.isVisible());
+        result.setUnsigned(data.isUnsigned());
+        result.setNullable(data.isNullable());
+        return result;
+    }
+    
+    @Override
+    public ShardingSphereColumn swapToObject(final YamlShardingSphereColumn 
yamlConfig) {
+        return new ShardingSphereColumn(yamlConfig.getName(), 
yamlConfig.getDataType(),
+                yamlConfig.isPrimaryKey(), yamlConfig.isGenerated(), 
yamlConfig.isCaseSensitive(), yamlConfig.isVisible(), yamlConfig.isUnsigned(), 
yamlConfig.isNullable());
+    }
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlConstraintSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlConstraintSwapper.java
new file mode 100644
index 00000000000..888d4a35079
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlConstraintSwapper.java
@@ -0,0 +1,41 @@
+/*
+ * 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.infra.yaml.schema.swapper;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereConstraint;
+import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereConstraint;
+
+/**
+ * YAML constraint swapper.
+ */
+public final class YamlConstraintSwapper implements 
YamlConfigurationSwapper<YamlShardingSphereConstraint, 
ShardingSphereConstraint> {
+    
+    @Override
+    public YamlShardingSphereConstraint swapToYamlConfiguration(final 
ShardingSphereConstraint data) {
+        YamlShardingSphereConstraint result = new 
YamlShardingSphereConstraint();
+        result.setName(data.getName());
+        result.setReferencedTableName(data.getReferencedTableName());
+        return result;
+    }
+    
+    @Override
+    public ShardingSphereConstraint swapToObject(final 
YamlShardingSphereConstraint yamlConfig) {
+        return new ShardingSphereConstraint(yamlConfig.getName(), 
yamlConfig.getReferencedTableName());
+    }
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlIndexSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlIndexSwapper.java
new file mode 100644
index 00000000000..0f1aaf0cdeb
--- /dev/null
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlIndexSwapper.java
@@ -0,0 +1,45 @@
+/*
+ * 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.infra.yaml.schema.swapper;
+
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
+import 
org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereIndex;
+
+/**
+ * YAML index swapper.
+ */
+public final class YamlIndexSwapper implements 
YamlConfigurationSwapper<YamlShardingSphereIndex, ShardingSphereIndex> {
+    
+    @Override
+    public YamlShardingSphereIndex swapToYamlConfiguration(final 
ShardingSphereIndex data) {
+        YamlShardingSphereIndex result = new YamlShardingSphereIndex();
+        result.setName(data.getName());
+        result.getColumns().addAll(data.getColumns());
+        result.setUnique(data.isUnique());
+        return result;
+    }
+    
+    @Override
+    public ShardingSphereIndex swapToObject(final YamlShardingSphereIndex 
yamlConfig) {
+        ShardingSphereIndex result = new 
ShardingSphereIndex(yamlConfig.getName());
+        result.getColumns().addAll(yamlConfig.getColumns());
+        result.setUnique(yamlConfig.isUnique());
+        return result;
+    }
+}
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlTableSwapper.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlTableSwapper.java
index 296dfc4849a..d35726d4fcf 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlTableSwapper.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlTableSwapper.java
@@ -38,88 +38,50 @@ import java.util.stream.Collectors;
  */
 public final class YamlTableSwapper implements 
YamlConfigurationSwapper<YamlShardingSphereTable, ShardingSphereTable> {
     
+    private final YamlColumnSwapper columnSwapper = new YamlColumnSwapper();
+    
+    private final YamlIndexSwapper indexSwapper = new YamlIndexSwapper();
+    
+    private final YamlConstraintSwapper constraintSwapper = new 
YamlConstraintSwapper();
+    
     @Override
     public YamlShardingSphereTable swapToYamlConfiguration(final 
ShardingSphereTable table) {
         YamlShardingSphereTable result = new YamlShardingSphereTable();
-        result.setColumns(swapYamlColumns(table.getColumnValues()));
-        result.setIndexes(swapYamlIndexes(table.getIndexValues()));
-        
result.setConstraints(swapYamlConstraints(table.getConstraintValues()));
         result.setName(table.getName());
+        result.setColumns(swapToYamlColumns(table.getColumnValues()));
+        result.setIndexes(swapToYamlIndexes(table.getIndexValues()));
+        
result.setConstraints(swapToYamlConstraints(table.getConstraintValues()));
         result.setType(table.getType());
         return result;
     }
     
-    @Override
-    public ShardingSphereTable swapToObject(final YamlShardingSphereTable 
yamlConfig) {
-        return new ShardingSphereTable(
-                yamlConfig.getName(), swapColumns(yamlConfig.getColumns()), 
swapIndexes(yamlConfig.getIndexes()), 
swapConstraints(yamlConfig.getConstraints()), yamlConfig.getType());
-    }
-    
-    private Collection<ShardingSphereConstraint> swapConstraints(final 
Map<String, YamlShardingSphereConstraint> constraints) {
-        return null == constraints ? Collections.emptyList() : 
constraints.values().stream().map(this::swapConstraint).collect(Collectors.toList());
-    }
-    
-    private ShardingSphereConstraint swapConstraint(final 
YamlShardingSphereConstraint constraint) {
-        return new ShardingSphereConstraint(constraint.getName(), 
constraint.getReferencedTableName());
+    private Map<String, YamlShardingSphereColumn> swapToYamlColumns(final 
Collection<ShardingSphereColumn> columns) {
+        return columns.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), columnSwapper::swapToYamlConfiguration, (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private Collection<ShardingSphereIndex> swapIndexes(final Map<String, 
YamlShardingSphereIndex> indexes) {
-        return null == indexes ? Collections.emptyList() : 
indexes.values().stream().map(this::swapIndex).collect(Collectors.toList());
-    }
-    
-    private ShardingSphereIndex swapIndex(final YamlShardingSphereIndex index) 
{
-        ShardingSphereIndex result = new ShardingSphereIndex(index.getName());
-        result.getColumns().addAll(index.getColumns());
-        result.setUnique(index.isUnique());
-        return result;
+    private Map<String, YamlShardingSphereIndex> swapToYamlIndexes(final 
Collection<ShardingSphereIndex> indexes) {
+        return indexes.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), indexSwapper::swapToYamlConfiguration, (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private Collection<ShardingSphereColumn> swapColumns(final Map<String, 
YamlShardingSphereColumn> indexes) {
-        return null == indexes ? Collections.emptyList() : 
indexes.values().stream().map(this::swapColumn).collect(Collectors.toList());
+    private Map<String, YamlShardingSphereConstraint> 
swapToYamlConstraints(final Collection<ShardingSphereConstraint> constrains) {
+        return constrains.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), constraintSwapper::swapToYamlConfiguration, 
(oldValue, currentValue) -> oldValue, LinkedHashMap::new));
     }
     
-    private ShardingSphereColumn swapColumn(final YamlShardingSphereColumn 
column) {
-        return new ShardingSphereColumn(column.getName(), 
column.getDataType(), column.isPrimaryKey(), column.isGenerated(), 
column.isCaseSensitive(), column.isVisible(), column.isUnsigned(),
-                column.isNullable());
-    }
-    
-    private Map<String, YamlShardingSphereConstraint> 
swapYamlConstraints(final Collection<ShardingSphereConstraint> constrains) {
-        return constrains.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), this::swapYamlConstraint, (oldValue, currentValue) 
-> oldValue, LinkedHashMap::new));
-    }
-    
-    private YamlShardingSphereConstraint swapYamlConstraint(final 
ShardingSphereConstraint constraint) {
-        YamlShardingSphereConstraint result = new 
YamlShardingSphereConstraint();
-        result.setName(constraint.getName());
-        result.setReferencedTableName(constraint.getReferencedTableName());
-        return result;
-    }
-    
-    private Map<String, YamlShardingSphereIndex> swapYamlIndexes(final 
Collection<ShardingSphereIndex> indexes) {
-        return indexes.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), this::swapYamlIndex, (oldValue, currentValue) -> 
oldValue, LinkedHashMap::new));
+    @Override
+    public ShardingSphereTable swapToObject(final YamlShardingSphereTable 
yamlConfig) {
+        return new ShardingSphereTable(yamlConfig.getName(),
+                swapToColumns(yamlConfig.getColumns()), 
swapToIndexes(yamlConfig.getIndexes()), 
swapToConstraints(yamlConfig.getConstraints()), yamlConfig.getType());
     }
     
-    private YamlShardingSphereIndex swapYamlIndex(final ShardingSphereIndex 
index) {
-        YamlShardingSphereIndex result = new YamlShardingSphereIndex();
-        result.setName(index.getName());
-        result.getColumns().addAll(index.getColumns());
-        result.setUnique(index.isUnique());
-        return result;
+    private Collection<ShardingSphereColumn> swapToColumns(final Map<String, 
YamlShardingSphereColumn> columns) {
+        return null == columns ? Collections.emptyList() : 
columns.values().stream().map(columnSwapper::swapToObject).collect(Collectors.toList());
     }
     
-    private Map<String, YamlShardingSphereColumn> swapYamlColumns(final 
Collection<ShardingSphereColumn> columns) {
-        return columns.stream().collect(Collectors.toMap(key -> 
key.getName().toLowerCase(), this::swapYamlColumn, (oldValue, currentValue) -> 
oldValue, LinkedHashMap::new));
+    private Collection<ShardingSphereIndex> swapToIndexes(final Map<String, 
YamlShardingSphereIndex> indexes) {
+        return null == indexes ? Collections.emptyList() : 
indexes.values().stream().map(indexSwapper::swapToObject).collect(Collectors.toList());
     }
     
-    private YamlShardingSphereColumn swapYamlColumn(final ShardingSphereColumn 
column) {
-        YamlShardingSphereColumn result = new YamlShardingSphereColumn();
-        result.setName(column.getName());
-        result.setCaseSensitive(column.isCaseSensitive());
-        result.setGenerated(column.isGenerated());
-        result.setPrimaryKey(column.isPrimaryKey());
-        result.setDataType(column.getDataType());
-        result.setVisible(column.isVisible());
-        result.setUnsigned(column.isUnsigned());
-        result.setNullable(column.isNullable());
-        return result;
+    private Collection<ShardingSphereConstraint> swapToConstraints(final 
Map<String, YamlShardingSphereConstraint> constraints) {
+        return null == constraints ? Collections.emptyList() : 
constraints.values().stream().map(constraintSwapper::swapToObject).collect(Collectors.toList());
     }
 }

Reply via email to