This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 65257ec73fd Modify unmarshal method to return a default object when
yaml is empty (#30206)
65257ec73fd is described below
commit 65257ec73fd5ab37bbd92b2ed4250daf6b11d021
Author: ZhangCheng <[email protected]>
AuthorDate: Tue Feb 20 17:06:52 2024 +0800
Modify unmarshal method to return a default object when yaml is empty
(#30206)
* Modify unmarshal method to return a default object when yaml is empty
* Modify unmarshal method to return a default object when yaml is empty
* Modify unmarshal method to return a default object when yaml is empty
* Modify unmarshal method to return a default object when yaml is empty
* Modify unmarshal method to return a default object when yaml is empty
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
* Remove AutoDataSource and unused test yaml
---
.../infra/util/yaml/YamlConfiguration.java | 10 ++++
.../shardingsphere/infra/util/yaml/YamlEngine.java | 17 ++++--
.../infra/util/yaml/YamlEngineTest.java | 64 +++++++++++++++-------
.../YamlShortcutsConfigurationFixture.java | 6 ++
.../util/src/test/resources/yaml/empty-config.yaml | 16 ++++++
.../config/YamlPipelineProcessConfiguration.java | 8 +--
...PipelineProcessConfigurationPersistService.java | 2 +-
...TableDataConsistencyCheckResultSwapperTest.java | 5 +-
.../backend/config/ProxyConfigurationLoader.java | 4 +-
.../yaml/YamlProxyDatabaseConfiguration.java | 6 ++
10 files changed, 103 insertions(+), 35 deletions(-)
diff --git
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlConfiguration.java
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlConfiguration.java
index 30017c8a634..a9a0df3159e 100644
---
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlConfiguration.java
+++
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlConfiguration.java
@@ -21,4 +21,14 @@ package org.apache.shardingsphere.infra.util.yaml;
* YAML configuration.
*/
public interface YamlConfiguration {
+
+ /**
+ * Check whether the YAML configuration is empty, indicating the absence
of any valid configuration items.
+ *
+ * @return check whether the YAML configuration is empty or not
+ */
+ default boolean isEmpty() {
+ // TODO Only global.yaml and database.yaml handle empty YAML file
currently.Other scenarios reading YAML files should also consider overriding
this method to check for empty files.
+ return false;
+ }
}
diff --git
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlEngine.java
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlEngine.java
index 0c78f1359bf..6e9dc650b30 100644
---
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlEngine.java
+++
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/yaml/YamlEngine.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.util.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import lombok.SneakyThrows;
import
org.apache.shardingsphere.infra.util.yaml.constructor.ShardingSphereYamlConstructor;
import
org.apache.shardingsphere.infra.util.yaml.representer.ShardingSphereYamlRepresenter;
import org.yaml.snakeyaml.DumperOptions;
@@ -49,9 +50,11 @@ public final class YamlEngine {
* @return object from YAML
* @throws IOException IO Exception
*/
+ @SneakyThrows(ReflectiveOperationException.class)
public static <T extends YamlConfiguration> T unmarshal(final File
yamlFile, final Class<T> classType) throws IOException {
try (BufferedReader inputStreamReader =
Files.newBufferedReader(Paths.get(yamlFile.toURI()))) {
- return new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(inputStreamReader, classType);
+ T result = new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(inputStreamReader, classType);
+ return null == result ? classType.getConstructor().newInstance() :
result;
}
}
@@ -64,9 +67,11 @@ public final class YamlEngine {
* @return object from YAML
* @throws IOException IO Exception
*/
+ @SneakyThrows(ReflectiveOperationException.class)
public static <T extends YamlConfiguration> T unmarshal(final byte[]
yamlBytes, final Class<T> classType) throws IOException {
try (InputStream inputStream = new ByteArrayInputStream(yamlBytes)) {
- return new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(inputStream, classType);
+ T result = new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(inputStream, classType);
+ return null == result ? classType.getConstructor().newInstance() :
result;
}
}
@@ -78,8 +83,10 @@ public final class YamlEngine {
* @param <T> type of class
* @return object from YAML
*/
+ @SneakyThrows(ReflectiveOperationException.class)
public static <T> T unmarshal(final String yamlContent, final Class<T>
classType) {
- return new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(yamlContent, classType);
+ T result = new Yaml(new
ShardingSphereYamlConstructor(classType)).loadAs(yamlContent, classType);
+ return null == result ? classType.getConstructor().newInstance() :
result;
}
/**
@@ -91,10 +98,12 @@ public final class YamlEngine {
* @param <T> type of class
* @return object from YAML
*/
+ @SneakyThrows(ReflectiveOperationException.class)
public static <T> T unmarshal(final String yamlContent, final Class<T>
classType, final boolean skipMissingProps) {
Representer representer = new Representer(new DumperOptions());
representer.getPropertyUtils().setSkipMissingProperties(skipMissingProps);
- return new Yaml(new ShardingSphereYamlConstructor(classType),
representer).loadAs(yamlContent, classType);
+ T result = new Yaml(new ShardingSphereYamlConstructor(classType),
representer).loadAs(yamlContent, classType);
+ return null == result ? classType.getConstructor().newInstance() :
result;
}
/**
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
index f773d71967c..a0a5f0bf2fa 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
@@ -33,6 +33,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
class YamlEngineTest {
@@ -46,23 +47,34 @@ class YamlEngineTest {
assertThat(actual.getName(), is("test"));
}
+ @Test
+ void assertUnmarshalWithEmptyFile() throws IOException {
+ URL url =
getClass().getClassLoader().getResource("yaml/empty-config.yaml");
+ assertNotNull(url);
+ YamlShortcutsConfigurationFixture actual = YamlEngine.unmarshal(new
File(url.getFile()), YamlShortcutsConfigurationFixture.class);
+ assertNotNull(actual);
+ assertTrue(actual.isEmpty());
+ }
+
@Test
void assertUnmarshalWithYamlBytes() throws IOException {
URL url =
getClass().getClassLoader().getResource("yaml/shortcuts-fixture.yaml");
assertNotNull(url);
- StringBuilder yamlContent = new StringBuilder();
- try (
- FileReader fileReader = new FileReader(url.getFile());
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- yamlContent.append(line).append(System.lineSeparator());
- }
- }
- YamlShortcutsConfigurationFixture actual =
YamlEngine.unmarshal(yamlContent.toString().getBytes(),
YamlShortcutsConfigurationFixture.class);
+ String yamlContent = readContent(url);
+ YamlShortcutsConfigurationFixture actual =
YamlEngine.unmarshal(yamlContent.getBytes(),
YamlShortcutsConfigurationFixture.class);
assertThat(actual.getName(), is("test"));
}
+ @Test
+ void assertUnmarshalWithEmptyYamlBytes() throws IOException {
+ URL url =
getClass().getClassLoader().getResource("yaml/empty-config.yaml");
+ assertNotNull(url);
+ String yamlContent = readContent(url);
+ YamlShortcutsConfigurationFixture actual =
YamlEngine.unmarshal(yamlContent.getBytes(),
YamlShortcutsConfigurationFixture.class);
+ assertNotNull(actual);
+ assertTrue(actual.isEmpty());
+ }
+
@Test
void assertUnmarshalWithYamlContentClassType() {
YamlShortcutsConfigurationFixture actual = YamlEngine.unmarshal("name:
test", YamlShortcutsConfigurationFixture.class);
@@ -81,6 +93,13 @@ class YamlEngineTest {
assertThat(actual.getProperty("password"), is("pwd"));
}
+ @Test
+ void assertUnmarshalWithEmptyProperties() {
+ Properties actual = YamlEngine.unmarshal("", Properties.class);
+ assertNotNull(actual);
+ assertTrue(actual.isEmpty());
+ }
+
@Test
void assertMarshal() {
YamlShortcutsConfigurationFixture actual = new
YamlShortcutsConfigurationFixture();
@@ -92,16 +111,8 @@ class YamlEngineTest {
void assertUnmarshalInvalidYaml() throws IOException {
URL url =
getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
assertNotNull(url);
- StringBuilder yamlContent = new StringBuilder();
- try (
- FileReader fileReader = new FileReader(url.getFile());
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- yamlContent.append(line).append(System.lineSeparator());
- }
- }
- assertThrows(ComposerException.class, () ->
YamlEngine.unmarshal(yamlContent.toString(), Object.class));
+ String yamlContent = readContent(url);
+ assertThrows(ComposerException.class, () ->
YamlEngine.unmarshal(yamlContent, Object.class));
}
@Test
@@ -114,4 +125,17 @@ class YamlEngineTest {
+ System.lineSeparator() + " name: test" +
System.lineSeparator();
assertThat(YamlEngine.marshal(Arrays.asList(actual, actualAnother)),
is(res));
}
+
+ private String readContent(final URL url) throws IOException {
+ StringBuilder result = new StringBuilder();
+ try (
+ FileReader fileReader = new FileReader(url.getFile());
+ BufferedReader reader = new BufferedReader(fileReader)) {
+ String line;
+ while (null != (line = reader.readLine())) {
+ result.append(line).append(System.lineSeparator());
+ }
+ }
+ return result.toString();
+ }
}
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/fixture/shortcuts/YamlShortcutsConfigurationFixture.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/fixture/shortcuts/YamlShortcutsConfigurationFixture.java
index 282aaf97ec2..237d7b55fff 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/fixture/shortcuts/YamlShortcutsConfigurationFixture.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/fixture/shortcuts/YamlShortcutsConfigurationFixture.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.util.yaml.fixture.shortcuts;
+import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
@@ -26,4 +27,9 @@ import
org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
public final class YamlShortcutsConfigurationFixture implements
YamlConfiguration {
private String name;
+
+ @Override
+ public boolean isEmpty() {
+ return Strings.isNullOrEmpty(name);
+ }
}
diff --git a/infra/util/src/test/resources/yaml/empty-config.yaml
b/infra/util/src/test/resources/yaml/empty-config.yaml
new file mode 100644
index 00000000000..b1312a0905c
--- /dev/null
+++ b/infra/util/src/test/resources/yaml/empty-config.yaml
@@ -0,0 +1,16 @@
+#
+# 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.
+#
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/progress/config/yaml/config/YamlPipelineProcessConfiguration.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/progress/config/yaml/config/YamlPipelineProcessConfiguration.java
index cd196682d74..ebd4041f1a6 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/progress/config/yaml/config/YamlPipelineProcessConfiguration.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/progress/config/yaml/config/YamlPipelineProcessConfiguration.java
@@ -35,12 +35,8 @@ public final class YamlPipelineProcessConfiguration
implements YamlConfiguration
private YamlAlgorithmConfiguration streamChannel;
- /**
- * Check all fields is null.
- *
- * @return true if all fields is null, otherwise is false.
- */
- public boolean isAllFieldsNull() {
+ @Override
+ public boolean isEmpty() {
return null == read && null == write && null == streamChannel;
}
}
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/PipelineProcessConfigurationPersistService.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/PipelineProcessConfigurationPersistService.java
index 55f6cc22dfe..3a70570ebd7 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/PipelineProcessConfigurationPersistService.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/metadata/PipelineProcessConfigurationPersistService.java
@@ -39,7 +39,7 @@ public final class PipelineProcessConfigurationPersistService
implements Pipelin
return null;
}
YamlPipelineProcessConfiguration yamlConfig =
YamlEngine.unmarshal(yamlText, YamlPipelineProcessConfiguration.class, true);
- return null == yamlConfig || yamlConfig.isAllFieldsNull() ? null :
swapper.swapToObject(yamlConfig);
+ return yamlConfig.isEmpty() ? null : swapper.swapToObject(yamlConfig);
}
@Override
diff --git
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/yaml/YamlTableDataConsistencyCheckResultSwapperTest.java
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/yaml/YamlTableDataConsistencyCheckResultSwapperTest.java
index 72a2d5c9cfa..08b11da43d7 100644
---
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/yaml/YamlTableDataConsistencyCheckResultSwapperTest.java
+++
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/result/yaml/YamlTableDataConsistencyCheckResultSwapperTest.java
@@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -102,11 +103,11 @@ class YamlTableDataConsistencyCheckResultSwapperTest {
@Test
void assertSwapToObjectWithEmptyString() {
-
assertNull(yamlTableDataConsistencyCheckResultSwapper.swapToObject(""));
+
assertNotNull(yamlTableDataConsistencyCheckResultSwapper.swapToObject(""));
}
@Test
void assertSwapToObjectWithBlankString() {
- assertNull(yamlTableDataConsistencyCheckResultSwapper.swapToObject("
"));
+
assertNotNull(yamlTableDataConsistencyCheckResultSwapper.swapToObject(" "));
}
}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
index e58fa5e7df9..49a919235a5 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
@@ -92,7 +92,7 @@ public final class ProxyConfigurationLoader {
private static YamlProxyServerConfiguration loadServerConfiguration(final
File yamlFile) throws IOException {
YamlProxyServerConfiguration result = YamlEngine.unmarshal(yamlFile,
YamlProxyServerConfiguration.class);
- return null == result ? new YamlProxyServerConfiguration() :
rebuildGlobalRuleConfiguration(result);
+ return rebuildGlobalRuleConfiguration(result);
}
private static YamlProxyServerConfiguration
rebuildGlobalRuleConfiguration(final YamlProxyServerConfiguration serverConfig)
{
@@ -138,7 +138,7 @@ public final class ProxyConfigurationLoader {
private static Optional<YamlProxyDatabaseConfiguration>
loadDatabaseConfiguration(final File yamlFile) throws IOException {
YamlProxyDatabaseConfiguration result = YamlEngine.unmarshal(yamlFile,
YamlProxyDatabaseConfiguration.class);
- if (null == result) {
+ if (result.isEmpty()) {
return Optional.empty();
}
Preconditions.checkNotNull(result.getDatabaseName(), "Property
`databaseName` in file `%s` is required.", yamlFile.getName());
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/YamlProxyDatabaseConfiguration.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/YamlProxyDatabaseConfiguration.java
index 5a94fda2f95..f1bc99c7704 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/YamlProxyDatabaseConfiguration.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/config/yaml/YamlProxyDatabaseConfiguration.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.config.yaml;
+import com.google.common.base.Strings;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
@@ -39,4 +40,9 @@ public final class YamlProxyDatabaseConfiguration implements
YamlConfiguration {
private Map<String, YamlProxyDataSourceConfiguration> dataSources = new
HashMap<>();
private Collection<YamlRuleConfiguration> rules = new LinkedList<>();
+
+ @Override
+ public boolean isEmpty() {
+ return Strings.isNullOrEmpty(databaseName) && dataSources.isEmpty() &&
rules.isEmpty();
+ }
}