Skip to content

Commit e9ac2a0

Browse files
authored
Support for Hocon inclusion of files without an extensions. Uses default '.conf'. (helidon-io#4168)
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
1 parent f95907a commit e9ac2a0

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

config/hocon/src/main/java/io/helidon/config/hocon/HoconConfigIncluder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.helidon.config.hocon;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122
import java.io.InputStreamReader;
@@ -36,6 +37,8 @@
3637

3738
class HoconConfigIncluder implements ConfigIncluder {
3839
private static final System.Logger LOGGER = System.getLogger(HoconConfigIncluder.class.getName());
40+
private static final String HOCON_EXTENSION = ".conf";
41+
3942
private ConfigParseOptions parseOptions;
4043
private Function<String, Optional<InputStream>> relativeResourceFunction;
4144
private Charset charset;
@@ -52,7 +55,7 @@ public ConfigIncluder withFallback(ConfigIncluder fallback) {
5255
public ConfigObject include(ConfigIncludeContext context, String what) {
5356
LOGGER.log(TRACE, String.format("Received request to include resource %s, %s",
5457
what, context.parseOptions().getOriginDescription()));
55-
Optional<InputStream> maybeStream = relativeResourceFunction.apply(what);
58+
Optional<InputStream> maybeStream = relativeResourceFunction.apply(patchName(what));
5659
if (maybeStream.isEmpty()) {
5760
if (Objects.nonNull(context.parseOptions()) && !context.parseOptions().getAllowMissing()) {
5861
throw new ConfigParserException(what + " is missing");
@@ -79,4 +82,19 @@ void relativeResourceFunction(Function<String, Optional<InputStream>> relativeRe
7982
this.relativeResourceFunction = relativeResourceFunction;
8083
}
8184

85+
/**
86+
* Adds default Hocon extension if not present.
87+
*
88+
* @param what file name
89+
* @return file name with extension
90+
*/
91+
static String patchName(String what) {
92+
Optional<String> base = Optional.of(what)
93+
.filter(f -> f.contains(File.separator))
94+
.map(f -> f.substring(f.lastIndexOf(File.separator) + 1));
95+
Optional<String> ext = Optional.of(base.orElse(what))
96+
.filter(f -> f.contains("."))
97+
.map(f -> f.substring(f.lastIndexOf(".") + 1));
98+
return ext.isPresent() ? what : what + HOCON_EXTENSION;
99+
}
82100
}

config/hocon/src/test/java/io/helidon/config/hocon/IncludeTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,23 @@ void testIncludesWithRequiredIncludeNotPresent() {
7373
assertThat(cpe.getMessage(), is("bogus.conf is missing"));
7474
}
7575

76+
@Test
77+
void testClasspathIncludesNoExtension() {
78+
Config config = Config.create(ClasspathConfigSource.create("conf/application4.conf"));
79+
80+
String value = config.get("app.greeting").asString().orElse(null);
81+
82+
assertThat("app.greeting should be loaded from application.conf", value, notNullValue());
83+
assertThat(value, is("Hello"));
84+
85+
value = config.get("server.host").asString().orElse(null);
86+
87+
assertThat("server.host should be loaded from included.conf", value, notNullValue());
88+
assertThat(value, is("localhost"));
89+
90+
value = config.get("server.port").asString().orElse(null);
91+
92+
assertThat("server.port should be loaded from sub/included.conf", value, notNullValue());
93+
assertThat(value, is("8080"));
94+
}
7695
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2022 Oracle and/or its affiliates.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
include "sub/included"
18+
include "included"
19+
20+
app {
21+
greeting = "Hello"
22+
}

config/hocon/src/test/resources/conf/sub/included.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
server {
1818
host = "127.0.0.1"
19+
port = 8080
1920
}

0 commit comments

Comments
 (0)