Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Hocon inclusion of files without an extensions (master) #4168

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.config.hocon;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -36,6 +37,8 @@

class HoconConfigIncluder implements ConfigIncluder {
private static final System.Logger LOGGER = System.getLogger(HoconConfigIncluder.class.getName());
private static final String HOCON_EXTENSION = ".conf";

private ConfigParseOptions parseOptions;
private Function<String, Optional<InputStream>> relativeResourceFunction;
private Charset charset;
Expand All @@ -52,7 +55,7 @@ public ConfigIncluder withFallback(ConfigIncluder fallback) {
public ConfigObject include(ConfigIncludeContext context, String what) {
LOGGER.log(TRACE, String.format("Received request to include resource %s, %s",
what, context.parseOptions().getOriginDescription()));
Optional<InputStream> maybeStream = relativeResourceFunction.apply(what);
Optional<InputStream> maybeStream = relativeResourceFunction.apply(patchName(what));
if (maybeStream.isEmpty()) {
if (Objects.nonNull(context.parseOptions()) && !context.parseOptions().getAllowMissing()) {
throw new ConfigParserException(what + " is missing");
Expand All @@ -79,4 +82,19 @@ void relativeResourceFunction(Function<String, Optional<InputStream>> relativeRe
this.relativeResourceFunction = relativeResourceFunction;
}

/**
* Adds default Hocon extension if not present.
*
* @param what file name
* @return file name with extension
*/
static String patchName(String what) {
Optional<String> base = Optional.of(what)
.filter(f -> f.contains(File.separator))
.map(f -> f.substring(f.lastIndexOf(File.separator) + 1));
Optional<String> ext = Optional.of(base.orElse(what))
.filter(f -> f.contains("."))
.map(f -> f.substring(f.lastIndexOf(".") + 1));
return ext.isPresent() ? what : what + HOCON_EXTENSION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,23 @@ void testIncludesWithRequiredIncludeNotPresent() {
assertThat(cpe.getMessage(), is("bogus.conf is missing"));
}

@Test
void testClasspathIncludesNoExtension() {
Config config = Config.create(ClasspathConfigSource.create("conf/application4.conf"));

String value = config.get("app.greeting").asString().orElse(null);

assertThat("app.greeting should be loaded from application.conf", value, notNullValue());
assertThat(value, is("Hello"));

value = config.get("server.host").asString().orElse(null);

assertThat("server.host should be loaded from included.conf", value, notNullValue());
assertThat(value, is("localhost"));

value = config.get("server.port").asString().orElse(null);

assertThat("server.port should be loaded from sub/included.conf", value, notNullValue());
assertThat(value, is("8080"));
}
}
22 changes: 22 additions & 0 deletions config/hocon/src/test/resources/conf/application4.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Copyright (c) 2022 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

include "sub/included"
include "included"

app {
greeting = "Hello"
}
1 change: 1 addition & 0 deletions config/hocon/src/test/resources/conf/sub/included.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

server {
host = "127.0.0.1"
port = 8080
}