35
35
import static java .util .stream .Collectors .*;
36
36
import static java .util .stream .StreamSupport .*;
37
37
import static ru .art .config .constants .ConfigExceptionMessages .*;
38
+ import static ru .art .core .constants .ArrayConstants .EMPTY_ARRAY_INDEX ;
38
39
import static ru .art .core .constants .StringConstants .*;
39
40
import java .util .*;
40
41
@@ -92,7 +93,7 @@ public Config getConfig(String sectionId) {
92
93
case HOCON :
93
94
return new Config (asTypesafeConfig ().getConfig (sectionId ), configType );
94
95
case YAML :
95
- return new Config (asYamlConfig (). at ( SLASH + sectionId . replace ( DOT , SLASH ) ), configType );
96
+ return new Config (getYamlConfigNode ( sectionId ), configType );
96
97
case REMOTE_ENTITY_CONFIG :
97
98
return new Config (asEntityConfig ().find (sectionId ), configType );
98
99
default :
@@ -113,7 +114,7 @@ public String getString(String path) {
113
114
case HOCON :
114
115
return asTypesafeConfig ().getString (path );
115
116
case YAML :
116
- return asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).asText ();
117
+ return getYamlConfigNode ( path ).asText ();
117
118
default :
118
119
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
119
120
}
@@ -131,7 +132,7 @@ public Integer getInt(String path) {
131
132
case HOCON :
132
133
return asTypesafeConfig ().getInt (path );
133
134
case YAML :
134
- return asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).asInt ();
135
+ return getYamlConfigNode ( path ).asInt ();
135
136
default :
136
137
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
137
138
}
@@ -149,7 +150,7 @@ public Long getLong(String path) {
149
150
case HOCON :
150
151
return asTypesafeConfig ().getLong (path );
151
152
case YAML :
152
- return asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).asLong ();
153
+ return getYamlConfigNode ( path ).asLong ();
153
154
default :
154
155
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
155
156
}
@@ -167,7 +168,7 @@ public Double getDouble(String path) {
167
168
case HOCON :
168
169
return asTypesafeConfig ().getDouble (path );
169
170
case YAML :
170
- return asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).asDouble ();
171
+ return getYamlConfigNode ( path ).asDouble ();
171
172
default :
172
173
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
173
174
}
@@ -185,7 +186,7 @@ public Boolean getBool(String path) {
185
186
case HOCON :
186
187
return asTypesafeConfig ().getBoolean (path );
187
188
case YAML :
188
- return asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).asBoolean ();
189
+ return getYamlConfigNode ( path ).asBoolean ();
189
190
default :
190
191
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
191
192
}
@@ -205,7 +206,7 @@ public List<Config> getConfigList(String path) {
205
206
case HOCON :
206
207
return asTypesafeConfig ().getConfigList (path ).stream ().map (configObject -> new Config (configObject , configType )).collect (toList ());
207
208
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 )
209
210
.map (configObject -> new Config (configObject , configType ))
210
211
.collect (toList ());
211
212
default :
@@ -225,7 +226,7 @@ public List<String> getStringList(String path) {
225
226
case HOCON :
226
227
return asTypesafeConfig ().getStringList (path );
227
228
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 )
229
230
.map (JsonNode ::asText )
230
231
.collect (toList ());
231
232
default :
@@ -245,7 +246,7 @@ public List<Integer> getIntList(String path) {
245
246
case HOCON :
246
247
return asTypesafeConfig ().getIntList (path );
247
248
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 )
249
250
.map (JsonNode ::asInt )
250
251
.collect (toList ());
251
252
default :
@@ -265,7 +266,7 @@ public List<Double> getDoubleList(String path) {
265
266
case HOCON :
266
267
return asTypesafeConfig ().getDoubleList (path );
267
268
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 )
269
270
.map (JsonNode ::asDouble )
270
271
.collect (toList ());
271
272
default :
@@ -285,7 +286,7 @@ public List<Long> getLongList(String path) {
285
286
case HOCON :
286
287
return asTypesafeConfig ().getLongList (path );
287
288
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 )
289
290
.map (JsonNode ::asLong )
290
291
.collect (toList ());
291
292
default :
@@ -305,7 +306,7 @@ public List<Boolean> getBoolList(String path) {
305
306
case HOCON :
306
307
return asTypesafeConfig ().getBooleanList (path );
307
308
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 )
309
310
.map (JsonNode ::asBoolean )
310
311
.collect (toList ());
311
312
default :
@@ -321,7 +322,7 @@ public Set<String> getKeys(String path) {
321
322
case HOCON :
322
323
return asTypesafeConfig ().getObject (path ).keySet ();
323
324
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 ());
325
326
case REMOTE_ENTITY_CONFIG :
326
327
return asEntityConfig ().findEntity (path ).getFieldNames ();
327
328
default :
@@ -357,7 +358,7 @@ public boolean hasPath(String path) {
357
358
case HOCON :
358
359
return asTypesafeConfig ().hasPath (path );
359
360
case YAML :
360
- JsonNodeType nodeType = asYamlConfig (). at ( SLASH + path . replace ( DOT , SLASH ) ).getNodeType ();
361
+ JsonNodeType nodeType = getYamlConfigNode ( path ).getNodeType ();
361
362
return nodeType != NULL && nodeType != MISSING ;
362
363
default :
363
364
throw new ConfigException (format (UNKNOWN_CONFIG_TYPE , configType ));
@@ -372,4 +373,43 @@ public Properties getProperties(String path) {
372
373
properties .putAll (getKeys (path ).stream ().collect (toMap (identity (), key -> getString (path + DOT + key ))));
373
374
return properties ;
374
375
}
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
+ }
375
415
}
0 commit comments