Handle the number key like 1: foo.
Signed-off-by: Mengqi Xu <2663479778@qq.com>
This commit is contained in:
parent
b3d3e467e4
commit
585bbfdb44
|
@ -245,13 +245,7 @@ public abstract class YamlProcessor {
|
|||
if (value instanceof Map) {
|
||||
value = asMap(value);
|
||||
}
|
||||
if (key instanceof CharSequence) {
|
||||
result.put(key.toString(), value);
|
||||
}
|
||||
else {
|
||||
// It has to be a map key in this case
|
||||
result.put("[" + key.toString() + "]", value);
|
||||
}
|
||||
result.put(key.toString(), value);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -128,4 +128,18 @@ class YamlMapFactoryBeanTests {
|
|||
this.factory.getObject().get("mymap"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMapWithIntegerKey() {
|
||||
this.factory.setResources(new ByteArrayResource("foo:\n 1: bar".getBytes()));
|
||||
Map<String, Object> map = this.factory.getObject();
|
||||
|
||||
assertThat(map).hasSize(1);
|
||||
assertThat(map.containsKey("foo")).isTrue();
|
||||
Object object = map.get("foo");
|
||||
assertThat(object).isInstanceOf(LinkedHashMap.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> sub = (Map<String, Object>) object;
|
||||
assertThat(sub.containsKey("1")).isTrue();
|
||||
assertThat(sub.get("1")).isEqualTo("bar");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ class YamlProcessorTests {
|
|||
void integerKeyBehaves() {
|
||||
setYaml("foo: bar\n1: bar");
|
||||
this.processor.process((properties, map) -> {
|
||||
assertThat(properties.get("[1]")).isEqualTo("bar");
|
||||
assertThat(properties.get("1")).isEqualTo("bar");
|
||||
assertThat(properties).hasSize(2);
|
||||
});
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ class YamlProcessorTests {
|
|||
void integerDeepKeyBehaves() {
|
||||
setYaml("foo:\n 1: bar");
|
||||
this.processor.process((properties, map) -> {
|
||||
assertThat(properties.get("foo[1]")).isEqualTo("bar");
|
||||
assertThat(properties.get("foo.1")).isEqualTo("bar");
|
||||
assertThat(properties).hasSize(1);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -180,7 +180,8 @@ class YamlPropertiesFactoryBeanTests {
|
|||
new ByteArrayResource("indexed2:\n - \"[a]\": foo\n \"[b]\": bar".getBytes()),
|
||||
new ByteArrayResource("only-left-bracket:\n \"[/key1/\": foo".getBytes()),
|
||||
new ByteArrayResource("only-right-bracket:\n \"/key1/]\": foo".getBytes()),
|
||||
new ByteArrayResource("special-bracket:\n \"][/key1/][\": foo".getBytes()));
|
||||
new ByteArrayResource("special-bracket:\n \"][/key1/][\": foo".getBytes()),
|
||||
new ByteArrayResource("number-key:\n 1: foo".getBytes()));
|
||||
|
||||
Properties properties = factory.getObject();
|
||||
assertThat(properties.getProperty("root.webservices[[domain.test:8080]][0].username")).isEqualTo("foo");
|
||||
|
@ -195,6 +196,8 @@ class YamlPropertiesFactoryBeanTests {
|
|||
assertThat(properties.getProperty("only-left-bracket[[/key1/]")).isEqualTo("foo");
|
||||
assertThat(properties.getProperty("only-right-bracket[/key1/]]")).isEqualTo("foo");
|
||||
assertThat(properties.getProperty("special-bracket.][/key1/][")).isEqualTo("foo");
|
||||
|
||||
assertThat(properties.getProperty("number-key.1")).isEqualTo("foo");
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue