Skip to content

Commit 6828489

Browse files
author
Anton Bashirov
authored
Merge pull request #278 from art-community/fix/fix-config-with-dot
fix dots in configs
2 parents c79ca13 + eae7ddd commit 6828489

File tree

10 files changed

+17860
-23
lines changed

10 files changed

+17860
-23
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ hs_err_pid*
5353
/build
5454
build
5555
dist
56-
package-lock.json
5756
node_modules
5857
.cache
5958
!gradle/wrapper/gradle-wrapper.jar

application-config-typesafe/src/main/java/ru/art/config/TypesafeConfigLoader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import static java.lang.System.*;
2525
import static java.nio.file.Paths.*;
2626
import static java.text.MessageFormat.*;
27-
import static java.util.Objects.isNull;
27+
import static java.util.Objects.*;
2828
import static ru.art.config.TypesafeConfigLoaderConstants.*;
29-
import static ru.art.config.TypesafeConfigLoadingExceptionMessages.CONFIG_FILE_NOT_FOUND;
29+
import static ru.art.config.TypesafeConfigLoadingExceptionMessages.*;
3030
import static ru.art.core.checker.CheckerForEmptiness.*;
3131
import static ru.art.core.constants.SystemProperties.*;
3232
import static ru.art.core.extension.FileExtensions.*;
@@ -61,8 +61,7 @@ static URL loadTypeSafeConfigUrl(ConfigSyntax configSyntax) {
6161
private static Reader loadConfigReader(ConfigSyntax configSyntax) throws IOException {
6262
URL url = loadTypeSafeConfigUrl(configSyntax);
6363
if (isNull(url)) {
64-
throw new TypesafeConfigLoadingException(format(CONFIG_FILE_NOT_FOUND, configSyntax.toString()
65-
.toLowerCase()));
64+
throw new TypesafeConfigLoadingException(format(CONFIG_FILE_NOT_FOUND, configSyntax.toString().toLowerCase()));
6665
}
6766
return new InputStreamReader(url.openStream());
6867
}

application-config/src/main/java/ru/art/config/Config.java

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static java.util.stream.Collectors.*;
3636
import static java.util.stream.StreamSupport.*;
3737
import static ru.art.config.constants.ConfigExceptionMessages.*;
38+
import static ru.art.core.constants.ArrayConstants.EMPTY_ARRAY_INDEX;
3839
import static ru.art.core.constants.StringConstants.*;
3940
import java.util.*;
4041

@@ -92,7 +93,7 @@ public Config getConfig(String sectionId) {
9293
case HOCON:
9394
return new Config(asTypesafeConfig().getConfig(sectionId), configType);
9495
case YAML:
95-
return new Config(asYamlConfig().at(SLASH + sectionId.replace(DOT, SLASH)), configType);
96+
return new Config(getYamlConfigNode(sectionId), configType);
9697
case REMOTE_ENTITY_CONFIG:
9798
return new Config(asEntityConfig().find(sectionId), configType);
9899
default:
@@ -113,7 +114,7 @@ public String getString(String path) {
113114
case HOCON:
114115
return asTypesafeConfig().getString(path);
115116
case YAML:
116-
return asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).asText();
117+
return getYamlConfigNode(path).asText();
117118
default:
118119
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
119120
}
@@ -131,7 +132,7 @@ public Integer getInt(String path) {
131132
case HOCON:
132133
return asTypesafeConfig().getInt(path);
133134
case YAML:
134-
return asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).asInt();
135+
return getYamlConfigNode(path).asInt();
135136
default:
136137
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
137138
}
@@ -149,7 +150,7 @@ public Long getLong(String path) {
149150
case HOCON:
150151
return asTypesafeConfig().getLong(path);
151152
case YAML:
152-
return asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).asLong();
153+
return getYamlConfigNode(path).asLong();
153154
default:
154155
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
155156
}
@@ -167,7 +168,7 @@ public Double getDouble(String path) {
167168
case HOCON:
168169
return asTypesafeConfig().getDouble(path);
169170
case YAML:
170-
return asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).asDouble();
171+
return getYamlConfigNode(path).asDouble();
171172
default:
172173
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
173174
}
@@ -185,7 +186,7 @@ public Boolean getBool(String path) {
185186
case HOCON:
186187
return asTypesafeConfig().getBoolean(path);
187188
case YAML:
188-
return asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).asBoolean();
189+
return getYamlConfigNode(path).asBoolean();
189190
default:
190191
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
191192
}
@@ -205,7 +206,7 @@ public List<Config> getConfigList(String path) {
205206
case HOCON:
206207
return asTypesafeConfig().getConfigList(path).stream().map(configObject -> new Config(configObject, configType)).collect(toList());
207208
case YAML:
208-
return stream(spliteratorUnknownSize(asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator(), ORDERED), false)
209+
return stream(spliteratorUnknownSize(getYamlConfigNode(path).iterator(), ORDERED), false)
209210
.map(configObject -> new Config(configObject, configType))
210211
.collect(toList());
211212
default:
@@ -225,7 +226,7 @@ public List<String> getStringList(String path) {
225226
case HOCON:
226227
return asTypesafeConfig().getStringList(path);
227228
case YAML:
228-
return stream(((Iterable<JsonNode>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator()).spliterator(), false)
229+
return stream(((Iterable<JsonNode>) () -> getYamlConfigNode(path).iterator()).spliterator(), false)
229230
.map(JsonNode::asText)
230231
.collect(toList());
231232
default:
@@ -245,7 +246,7 @@ public List<Integer> getIntList(String path) {
245246
case HOCON:
246247
return asTypesafeConfig().getIntList(path);
247248
case YAML:
248-
return stream(((Iterable<JsonNode>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator()).spliterator(), false)
249+
return stream(((Iterable<JsonNode>) () -> getYamlConfigNode(path).iterator()).spliterator(), false)
249250
.map(JsonNode::asInt)
250251
.collect(toList());
251252
default:
@@ -265,7 +266,7 @@ public List<Double> getDoubleList(String path) {
265266
case HOCON:
266267
return asTypesafeConfig().getDoubleList(path);
267268
case YAML:
268-
return stream(((Iterable<JsonNode>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator()).spliterator(), false)
269+
return stream(((Iterable<JsonNode>) () -> getYamlConfigNode(path).iterator()).spliterator(), false)
269270
.map(JsonNode::asDouble)
270271
.collect(toList());
271272
default:
@@ -285,7 +286,7 @@ public List<Long> getLongList(String path) {
285286
case HOCON:
286287
return asTypesafeConfig().getLongList(path);
287288
case YAML:
288-
return stream(((Iterable<JsonNode>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator()).spliterator(), false)
289+
return stream(((Iterable<JsonNode>) () -> getYamlConfigNode(path).iterator()).spliterator(), false)
289290
.map(JsonNode::asLong)
290291
.collect(toList());
291292
default:
@@ -305,7 +306,7 @@ public List<Boolean> getBoolList(String path) {
305306
case HOCON:
306307
return asTypesafeConfig().getBooleanList(path);
307308
case YAML:
308-
return stream(((Iterable<JsonNode>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).iterator()).spliterator(), false)
309+
return stream(((Iterable<JsonNode>) () -> getYamlConfigNode(path).iterator()).spliterator(), false)
309310
.map(JsonNode::asBoolean)
310311
.collect(toList());
311312
default:
@@ -321,7 +322,7 @@ public Set<String> getKeys(String path) {
321322
case HOCON:
322323
return asTypesafeConfig().getObject(path).keySet();
323324
case YAML:
324-
return stream(((Iterable<String>) () -> asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).fieldNames()).spliterator(), false).collect(toSet());
325+
return stream(((Iterable<String>) () -> getYamlConfigNode(path).fieldNames()).spliterator(), false).collect(toSet());
325326
case REMOTE_ENTITY_CONFIG:
326327
return asEntityConfig().findEntity(path).getFieldNames();
327328
default:
@@ -357,7 +358,7 @@ public boolean hasPath(String path) {
357358
case HOCON:
358359
return asTypesafeConfig().hasPath(path);
359360
case YAML:
360-
JsonNodeType nodeType = asYamlConfig().at(SLASH + path.replace(DOT, SLASH)).getNodeType();
361+
JsonNodeType nodeType = getYamlConfigNode(path).getNodeType();
361362
return nodeType != NULL && nodeType != MISSING;
362363
default:
363364
throw new ConfigException(format(UNKNOWN_CONFIG_TYPE, configType));
@@ -372,4 +373,43 @@ public Properties getProperties(String path) {
372373
properties.putAll(getKeys(path).stream().collect(toMap(identity(), key -> getString(path + DOT + key))));
373374
return properties;
374375
}
376+
377+
378+
private JsonNode getYamlConfigNode(String path) {
379+
JsonNode yamlConfig = asYamlConfig();
380+
JsonNode node = yamlConfig.path(path);
381+
JsonNodeType nodeType = node.getNodeType();
382+
if (nodeType != NULL && nodeType != MISSING) {
383+
return node;
384+
}
385+
int dotIndex = path.indexOf(DOT);
386+
if (dotIndex == EMPTY_ARRAY_INDEX) {
387+
return MissingNode.getInstance();
388+
}
389+
node = yamlConfig.path(path.substring(0, dotIndex));
390+
path = path.substring(dotIndex + 1);
391+
while (true) {
392+
JsonNode valueNode = node.path(path);
393+
JsonNodeType valueNodeType = valueNode.getNodeType();
394+
switch (valueNodeType) {
395+
case OBJECT:
396+
case BINARY:
397+
case BOOLEAN:
398+
case NUMBER:
399+
case ARRAY:
400+
case STRING:
401+
return valueNode;
402+
case MISSING:
403+
case POJO:
404+
case NULL:
405+
break;
406+
}
407+
dotIndex = path.indexOf(DOT);
408+
if (dotIndex == EMPTY_ARRAY_INDEX) {
409+
return MissingNode.getInstance();
410+
}
411+
node = node.path(path.substring(0, dotIndex));
412+
path = path.substring(dotIndex + 1);
413+
}
414+
}
375415
}

0 commit comments

Comments
 (0)