You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/02/26 18:30:32 UTC

(shardingsphere) branch master updated: Add YamlRuleConfigurationFieldUtil (#34799)

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 8b7fdaa754f Add YamlRuleConfigurationFieldUtil (#34799)
8b7fdaa754f is described below

commit 8b7fdaa754fc117cece9ebe95e35327cd7689eb7
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Feb 27 02:30:25 2025 +0800

    Add YamlRuleConfigurationFieldUtil (#34799)
    
    * Add YamlRuleConfigurationFieldUtil
    
    * Add YamlRuleConfigurationFieldUtil
---
 .../node/rule/node/DatabaseRuleNodeGenerator.java  | 22 ++------
 .../rule/tuple/YamlRuleConfigurationFieldUtil.java | 59 ++++++++++++++++++++++
 .../YamlRuleRepositoryTupleSwapperEngine.java      | 23 ++-------
 3 files changed, 69 insertions(+), 35 deletions(-)

diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/node/DatabaseRuleNodeGenerator.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/node/DatabaseRuleNodeGenerator.java
index 96e7fd69049..a92e55b4051 100644
--- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/node/DatabaseRuleNodeGenerator.java
+++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/node/DatabaseRuleNodeGenerator.java
@@ -17,22 +17,19 @@
 
 package org.apache.shardingsphere.mode.node.rule.node;
 
-import com.google.common.base.CaseFormat;
 import com.google.common.base.Preconditions;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
+import org.apache.shardingsphere.mode.node.rule.tuple.YamlRuleConfigurationFieldUtil;
 import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleEntity;
 import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleField;
 import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleKeyListNameGenerator;
 
 import java.lang.reflect.Field;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Comparator;
 import java.util.LinkedList;
 import java.util.Map;
-import java.util.stream.Collectors;
 
 /**
  * Database rule node generator.
@@ -51,26 +48,17 @@ public final class DatabaseRuleNodeGenerator {
         Preconditions.checkNotNull(tupleEntity, "Can not find @RuleRepositoryTupleEntity on class: ", yamlRuleConfigurationClass.getName());
         Collection<String> namedItems = new LinkedList<>();
         Collection<String> uniqueItems = new LinkedList<>();
-        for (Field each : getFields(yamlRuleConfigurationClass)) {
+        for (Field each : YamlRuleConfigurationFieldUtil.getFields(yamlRuleConfigurationClass)) {
             if (null == each.getAnnotation(RuleRepositoryTupleField.class)) {
                 continue;
             }
+            String tupleName = YamlRuleConfigurationFieldUtil.getTupleName(each);
             if (each.getType().equals(Map.class) || each.getType().equals(Collection.class) && null != each.getAnnotation(RuleRepositoryTupleKeyListNameGenerator.class)) {
-                namedItems.add(getPersistTupleName(each));
+                namedItems.add(tupleName);
             } else {
-                uniqueItems.add(getPersistTupleName(each));
+                uniqueItems.add(tupleName);
             }
         }
         return new DatabaseRuleNode(tupleEntity.value(), namedItems, uniqueItems);
     }
-    
-    private static Collection<Field> getFields(final Class<? extends YamlRuleConfiguration> yamlRuleConfigurationClass) {
-        return Arrays.stream(yamlRuleConfigurationClass.getDeclaredFields())
-                .filter(each -> null != each.getAnnotation(RuleRepositoryTupleField.class))
-                .sorted(Comparator.comparingInt(o -> o.getAnnotation(RuleRepositoryTupleField.class).type().ordinal())).collect(Collectors.toList());
-    }
-    
-    private static String getPersistTupleName(final Field field) {
-        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
-    }
 }
diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleConfigurationFieldUtil.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleConfigurationFieldUtil.java
new file mode 100644
index 00000000000..37a587b24dc
--- /dev/null
+++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleConfigurationFieldUtil.java
@@ -0,0 +1,59 @@
+/*
+ * 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.mode.node.rule.tuple;
+
+import com.google.common.base.CaseFormat;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
+import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleField;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.stream.Collectors;
+
+/**
+ * YAML rule configuration field utility class.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class YamlRuleConfigurationFieldUtil {
+    
+    /**
+     * Get fields from YAML rule configuration.
+     *
+     * @param yamlRuleConfigurationClass YAML rule configuration class
+     * @return got fields
+     */
+    public static Collection<Field> getFields(final Class<? extends YamlRuleConfiguration> yamlRuleConfigurationClass) {
+        return Arrays.stream(yamlRuleConfigurationClass.getDeclaredFields())
+                .filter(each -> null != each.getAnnotation(RuleRepositoryTupleField.class))
+                .sorted(Comparator.comparingInt(o -> o.getAnnotation(RuleRepositoryTupleField.class).type().ordinal())).collect(Collectors.toList());
+    }
+    
+    /**
+     * Get persist tuple name.
+     *
+     * @param field field from YAML rule configuration
+     * @return persist tuple name
+     */
+    public static String getTupleName(final Field field) {
+        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
+    }
+}
diff --git a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
index d954e641778..36d0617b203 100644
--- a/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
+++ b/mode/node/src/main/java/org/apache/shardingsphere/mode/node/rule/tuple/YamlRuleRepositoryTupleSwapperEngine.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.mode.node.rule.tuple;
 
-import com.google.common.base.CaseFormat;
 import com.google.common.base.Strings;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
@@ -36,15 +35,12 @@ import org.apache.shardingsphere.mode.node.path.type.metadata.rule.DatabaseRuleN
 import org.apache.shardingsphere.mode.node.rule.node.DatabaseRuleNode;
 import org.apache.shardingsphere.mode.node.rule.node.DatabaseRuleNodeGenerator;
 import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleEntity;
-import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleField;
 import org.apache.shardingsphere.mode.node.rule.tuple.annotation.RuleRepositoryTupleKeyListNameGenerator;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.ParameterizedType;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -73,7 +69,7 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
             return Collections.singleton(new RuleRepositoryTuple(entity.value(), YamlEngine.marshal(yamlRuleConfig)));
         }
         Collection<RuleRepositoryTuple> result = new LinkedList<>();
-        for (Field each : getFields(yamlRuleConfig.getClass())) {
+        for (Field each : YamlRuleConfigurationFieldUtil.getFields(yamlRuleConfig.getClass())) {
             boolean isAccessible = each.isAccessible();
             each.setAccessible(true);
             result.addAll(swapToTuples(yamlRuleConfig, each));
@@ -89,7 +85,7 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
         if (null == fieldValue) {
             return Collections.emptyList();
         }
-        String tupleName = getTupleName(field);
+        String tupleName = YamlRuleConfigurationFieldUtil.getTupleName(field);
         RuleRepositoryTupleKeyListNameGenerator tupleKeyListNameGenerator = field.getAnnotation(RuleRepositoryTupleKeyListNameGenerator.class);
         if (null != tupleKeyListNameGenerator && fieldValue instanceof Collection) {
             Collection<RuleRepositoryTuple> result = new LinkedList<>();
@@ -121,16 +117,6 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
         return Collections.singleton(new RuleRepositoryTuple(tupleName, YamlEngine.marshal(fieldValue)));
     }
     
-    private Collection<Field> getFields(final Class<? extends YamlRuleConfiguration> yamlRuleConfigurationClass) {
-        return Arrays.stream(yamlRuleConfigurationClass.getDeclaredFields())
-                .filter(each -> null != each.getAnnotation(RuleRepositoryTupleField.class))
-                .sorted(Comparator.comparingInt(o -> o.getAnnotation(RuleRepositoryTupleField.class).type().ordinal())).collect(Collectors.toList());
-    }
-    
-    private String getTupleName(final Field field) {
-        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
-    }
-    
     /**
      * Swap from rule repository tuple to YAML rule configurations.
      *
@@ -143,7 +129,8 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
         if (null == entity) {
             return Optional.empty();
         }
-        return entity.leaf() ? swapToYamlRuleConfiguration(tuples, toBeSwappedType, entity) : swapToYamlRuleConfiguration(tuples, toBeSwappedType, getFields(toBeSwappedType));
+        return entity.leaf() ? swapToYamlRuleConfiguration(tuples, toBeSwappedType, entity)
+                : swapToYamlRuleConfiguration(tuples, toBeSwappedType, YamlRuleConfigurationFieldUtil.getFields(toBeSwappedType));
     }
     
     @SneakyThrows(ReflectiveOperationException.class)
@@ -200,7 +187,7 @@ public final class YamlRuleRepositoryTupleSwapperEngine {
     @SneakyThrows(ReflectiveOperationException.class)
     private void setFieldValue(final YamlRuleConfiguration yamlRuleConfig, final Field field, final String ruleType, final RuleRepositoryTuple tuple) {
         Object fieldValue = field.get(yamlRuleConfig);
-        String tupleName = getTupleName(field);
+        String tupleName = YamlRuleConfigurationFieldUtil.getTupleName(field);
         RuleRepositoryTupleKeyListNameGenerator tupleKeyListNameGenerator = field.getAnnotation(RuleRepositoryTupleKeyListNameGenerator.class);
         if (null != tupleKeyListNameGenerator && fieldValue instanceof Collection) {
             DatabaseRuleNodePath databaseRuleNodePath = new DatabaseRuleNodePath(NodePathPattern.IDENTIFIER, ruleType, new DatabaseRuleItem(tupleName, NodePathPattern.QUALIFIED_IDENTIFIER));