Skip to content

Commit 5910576

Browse files
committed
Support JSON and YAML expansion in config
1 parent 712d669 commit 5910576

File tree

26 files changed

+229
-99
lines changed

26 files changed

+229
-99
lines changed

CHANGELOG.md

Lines changed: 140 additions & 77 deletions
Large diffs are not rendered by default.

cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.12.1-SNAPSHOT</version>
17+
<version>3.13.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-cli</artifactId>

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed</artifactId>
17-
<version>3.12.1-SNAPSHOT</version>
17+
<version>3.13.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-core</artifactId>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright © 2013-2021, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.seed.core.internal.configuration;
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
12+
import org.seedstack.coffig.TreeNode;
13+
import org.seedstack.coffig.node.MapNode;
14+
import org.seedstack.coffig.node.NamedNode;
15+
import org.seedstack.coffig.node.ValueNode;
16+
import org.seedstack.coffig.provider.JacksonProvider;
17+
import org.seedstack.coffig.spi.ConfigurationProcessor;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
public class JsonConfigProcessor implements ConfigurationProcessor {
25+
private final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
26+
27+
@Override
28+
public void process(MapNode configuration) {
29+
Map<MapNode, List<String>> toParse = new HashMap<>();
30+
31+
configuration.walk()
32+
.filter(node -> node instanceof MapNode)
33+
.map(MapNode.class::cast)
34+
.forEach(mapNode -> mapNode.namedNodes()
35+
.filter(this::isJsonOrYamlFormatted)
36+
.forEach(namedNode -> toParse.computeIfAbsent(mapNode, k -> new ArrayList<>())
37+
.add(namedNode.name()))
38+
);
39+
40+
// Replace the value with the parsed JSON/YAML
41+
toParse.forEach((parent, list) -> list.forEach(key -> {
42+
TreeNode toChange = parent.remove(key);
43+
String newKey = key.substring(0, key.length() - 5);
44+
45+
try {
46+
parent.set(newKey, JacksonProvider.buildTreeFromString(yamlMapper, toChange.value()));
47+
} catch (Exception e) {
48+
parent.set(newKey, new ValueNode(TreeNode.formatNodeError(e)));
49+
}
50+
}));
51+
}
52+
53+
private boolean isJsonOrYamlFormatted(NamedNode namedNode) {
54+
return namedNode.name().endsWith("|json") || namedNode.name().endsWith("|yaml");
55+
}
56+
}

core/src/main/resources/META-INF/services/org.seedstack.coffig.spi.ConfigurationProcessor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ org.seedstack.seed.core.internal.configuration.SystemPropertiesProcessor
22
org.seedstack.seed.core.internal.configuration.RemovalProcessor
33
org.seedstack.seed.core.internal.configuration.ProfileProcessor
44
org.seedstack.seed.core.internal.configuration.SecureConfigurationProcessor
5+
org.seedstack.seed.core.internal.configuration.JsonConfigProcessor

core/src/test/java/org/seedstack/seed/core/ConfigurationIT.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import org.seedstack.coffig.Coffig;
2121
import org.seedstack.coffig.Config;
2222
import org.seedstack.coffig.SingleValue;
23+
import org.seedstack.coffig.TreeNode;
2324
import org.seedstack.coffig.internal.ConfigurationException;
25+
import org.seedstack.coffig.node.MapNode;
2426
import org.seedstack.seed.Application;
2527
import org.seedstack.seed.ApplicationConfig;
2628
import org.seedstack.seed.Bind;
@@ -176,6 +178,12 @@ public void configuration_functions() {
176178
assertThat(configuration.get(String.class, "functions.randomUuid")).isNotEmpty();
177179
}
178180

181+
@Test
182+
public void json_expansion() {
183+
Coffig configuration = injector.getInstance(Application.class).getConfiguration();
184+
assertThat(configuration.get(String.class, "jsonVal.key1")).isEqualTo("val1");
185+
}
186+
179187
@Test
180188
public void applicationInjection() {
181189
assertThat(application).isNotNull();

core/src/test/resources/application.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ functions:
8282
availableTcpPort: $availableTcpPort('port1')
8383
availableUdpPort: $availableUdpPort('port2')
8484
randomUuid: $randomUuid()
85+
jsonVal|json: '{ "key1": "val1" }'
86+
8587
arrayOfMapContainer:
8688
basicProperty: customId
8789
arrayOfMaps:

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
<groupId>org.seedstack.seed</groupId>
2121
<artifactId>seed</artifactId>
22-
<version>3.12.1-SNAPSHOT</version>
22+
<version>3.13.0-SNAPSHOT</version>
2323
<packaging>pom</packaging>
2424

2525
<properties>
2626
<nuun-kernel.version>1.0.M11</nuun-kernel.version>
2727
<javassist.version>3.27.0-GA</javassist.version>
2828
<shed.version>1.1.6</shed.version>
29-
<coffig.version>3.1.7</coffig.version>
29+
<coffig.version>3.1.8-SNAPSHOT</coffig.version>
3030
<commons-cli.version>1.4</commons-cli.version>
3131
<guice.version>5.0.1</guice.version>
3232
<guava.version>30.1.1-jre</guava.version>

rest/core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.12.1-SNAPSHOT</version>
17+
<version>3.13.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-core</artifactId>

rest/jersey2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.seedstack.seed</groupId>
1616
<artifactId>seed-rest</artifactId>
17-
<version>3.12.1-SNAPSHOT</version>
17+
<version>3.13.0-SNAPSHOT</version>
1818
</parent>
1919

2020
<artifactId>seed-rest-jersey2</artifactId>

0 commit comments

Comments
 (0)