You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2022/02/07 05:18:11 UTC
[shardingsphere] branch master updated: Update ShardingSphereYamlRepresenter to skip empty map with marshal yaml (#15273)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 cc0f11a Update ShardingSphereYamlRepresenter to skip empty map with marshal yaml (#15273)
cc0f11a is described below
commit cc0f11ab03ed74836b6c2290f3b1ca87013bfb7d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Feb 7 13:17:15 2022 +0800
Update ShardingSphereYamlRepresenter to skip empty map with marshal yaml (#15273)
---
.../representer/ShardingSphereYamlRepresenter.java | 22 ++++++++++++++++++++++
.../fixture/ShardingSphereYamlObjectFixture.java | 2 ++
.../ShardingSphereYamlRepresenterTest.java | 7 +++++++
3 files changed, 31 insertions(+)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenter.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenter.java
index 1ada4e2..07eca42 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenter.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenter.java
@@ -22,10 +22,16 @@ import org.apache.shardingsphere.infra.yaml.engine.representer.processor.Default
import org.apache.shardingsphere.infra.yaml.engine.representer.processor.ShardingSphereYamlTupleProcessor;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import org.yaml.snakeyaml.introspector.Property;
+import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
/**
* ShardingSphere YAML representer.
*/
@@ -49,4 +55,20 @@ public final class ShardingSphereYamlRepresenter extends Representer {
}
return new DefaultYamlTupleProcessor().process(nodeTuple);
}
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Override
+ protected Node representMapping(final Tag tag, final Map<?, ?> mapping, final Boolean flowStyle) {
+ Map skippedEmptyValuesMapping = new LinkedHashMap<>(mapping.size(), 1);
+ for (Entry<?, ?> entry : mapping.entrySet()) {
+ if (entry.getValue() instanceof Collection && ((Collection) entry.getValue()).isEmpty()) {
+ continue;
+ }
+ if (entry.getValue() instanceof Map && ((Map) entry.getValue()).isEmpty()) {
+ continue;
+ }
+ skippedEmptyValuesMapping.put(entry.getKey(), entry.getValue());
+ }
+ return super.representMapping(tag, skippedEmptyValuesMapping, flowStyle);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
index 8b31536..7db2f32 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
@@ -33,6 +33,8 @@ public final class ShardingSphereYamlObjectFixture {
private Map<String, String> map;
+ private Map<String, Map<String, String>> embeddedMap;
+
private CustomizedClassFixture customizedClass;
private String customizedTag;
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
index 0e346c4..3c737ea 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
@@ -22,11 +22,13 @@ import org.junit.Test;
import org.yaml.snakeyaml.Yaml;
import java.util.Arrays;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
public final class ShardingSphereYamlRepresenterTest {
@@ -46,10 +48,15 @@ public final class ShardingSphereYamlRepresenterTest {
map.put("key1", "value1");
map.put("key2", "value2");
actual.setMap(map);
+ actual.setEmbeddedMap(new LinkedHashMap<>());
+ actual.getEmbeddedMap().put("embedded_map_1", new LinkedHashMap<>());
+ actual.getEmbeddedMap().put("embedded_map_2", Collections.singletonMap("embedded_map_foo", "embedded_map_foo_value"));
actual.setCustomizedTag("customized_tag");
String expected = new Yaml(new ShardingSphereYamlRepresenter()).dumpAsMap(actual);
assertThat(expected, containsString("collection:\n- value1\n- value2\n"));
assertThat(expected, containsString("map:\n key1: value1\n key2: value2\n"));
+ assertThat(expected, not(containsString("embedded_map_1")));
+ assertThat(expected, containsString("embeddedMap:\n embedded_map_2:\n embedded_map_foo: embedded_map_foo_value\n"));
assertThat(expected, containsString("value: value\n"));
assertThat(expected, containsString("customizedTag: converted_customized_tag\n"));
}