Skip to content

Commit f301080

Browse files
@Requires(value = "XXX") is case-sensitive; lowercase config silently creates no bean
1 parent 8ffb86d commit f301080

6 files changed

Lines changed: 17 additions & 32 deletions

File tree

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/FilesImportFactory.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import com.fasterxml.jackson.databind.ObjectMapper;
66

7-
import io.micronaut.context.annotation.Bean;
87
import io.micronaut.context.annotation.Factory;
98
import io.micronaut.context.annotation.Requires;
9+
import jakarta.inject.Singleton;
1010

1111
/**
1212
* Factory handling Files import based on the {@link ImportFileProperties.Format} defined in configuration.
@@ -15,27 +15,14 @@
1515
@Requires(property = "consul.files")
1616
public class FilesImportFactory {
1717

18-
@Bean
19-
@Requires(property = "consul.files.format", value = "JSON")
20-
JsonFilesImporter jsonFilesImporter(final ImportFileProperties importFileProperties, final ObjectMapper objectMapper) {
21-
final var rootPath = Paths.get(importFileProperties.getRootPath());
22-
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
23-
return new JsonFilesImporter(rootPath, targetPath, objectMapper);
24-
}
25-
26-
@Bean
27-
@Requires(property = "consul.files.format", value = "PROPERTIES")
28-
PropertiesFilesImporter propertiesFilesImporter(final ImportFileProperties importFileProperties) {
29-
final var rootPath = Paths.get(importFileProperties.getRootPath());
30-
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
31-
return new PropertiesFilesImporter(rootPath, targetPath);
32-
}
33-
34-
@Bean
35-
@Requires(property = "consul.files.format", value = "YAML")
36-
YamlFilesImporter yamlFilesImporter(final ImportFileProperties importFileProperties) {
37-
final var rootPath = Paths.get(importFileProperties.getRootPath());
38-
final var targetPath = rootPath.resolve(importFileProperties.getTarget());
39-
return new YamlFilesImporter(rootPath, targetPath);
18+
@Singleton
19+
FilesImporter filesImporter(final ImportFileProperties properties, final ObjectMapper objectMapper) {
20+
final var rootPath = Paths.get(properties.getRootPath());
21+
final var targetPath = rootPath.resolve(properties.getTarget());
22+
return switch (properties.getFormat()) {
23+
case JSON -> new JsonFilesImporter(rootPath, targetPath, objectMapper);
24+
case PROPERTIES -> new PropertiesFilesImporter(rootPath, targetPath);
25+
case YAML -> new YamlFilesImporter(rootPath, targetPath);
26+
};
4027
}
4128
}

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/JsonFilesImporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected SequencedMap<String, Object> readFile(@NonNull final File file) throws
5959
@NonNull
6060
@Override
6161
protected String writeValueAsString(@NonNull final Map<String, Object> map) throws JsonProcessingException {
62-
if (map == null || map.isEmpty()) {
62+
if (map.isEmpty()) {
6363
return "";
6464
}
6565
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);

consul-populate-core/src/main/java/com/frogdevelopment/consul/populate/files/YamlFilesImporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected SequencedMap<String, Object> readFile(@NonNull final File file) throws
5252
@NonNull
5353
@Override
5454
protected String writeValueAsString(@NonNull final Map<String, Object> map) {
55-
if (map == null || map.isEmpty()) {
55+
if (map.isEmpty()) {
5656
return "";
5757
}
5858
return yaml.get().dumpAsMap(map);

consul-populate-core/src/test/java/com/frogdevelopment/consul/populate/files/JsonFilesImporterTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class JsonFilesImporterTest extends BaseFilesImporterTest {
5353
}""";
5454

5555
@Inject
56-
private JsonFilesImporter filesImporter;
56+
private FilesImporter filesImporter;
5757

5858
@ParameterizedTest
5959
@CsvSource({
@@ -82,8 +82,7 @@ void should_mergeJson() {
8282
}
8383

8484
@Test
85-
void should_handle_empty() throws JsonProcessingException {
86-
// given
85+
void should_handle_empty() throws Exception {
8786
// when
8887
final var value = filesImporter.writeValueAsString(new TreeMap<>());
8988

consul-populate-core/src/test/java/com/frogdevelopment/consul/populate/files/PropertiesFilesImporterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class PropertiesFilesImporterTest extends BaseFilesImporterTest {
2727
very.deep.nested.field.that.will.but.not=here""";
2828

2929
@Inject
30-
private PropertiesFilesImporter filesImporter;
30+
private FilesImporter filesImporter;
3131

3232
@ParameterizedTest
3333
@CsvSource({

consul-populate-core/src/test/java/com/frogdevelopment/consul/populate/files/YamlFilesImporterTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class YamlFilesImporterTest extends BaseFilesImporterTest {
4343
""";
4444

4545
@Inject
46-
private YamlFilesImporter filesImporter;
46+
private FilesImporter filesImporter;
4747

4848
@ParameterizedTest
4949
@CsvSource({
@@ -74,8 +74,7 @@ void should_mergeYaml() {
7474
}
7575

7676
@Test
77-
void should_handle_empty() {
78-
// given
77+
void should_handle_empty() throws Exception {
7978
// when
8079
final var value = filesImporter.writeValueAsString(new TreeMap<>());
8180

0 commit comments

Comments
 (0)