Skip to content

Commit 21e1da9

Browse files
committed
Implement writeYaml
1 parent 810f615 commit 21e1da9

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/properties/ApplicationProperties.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
public class ApplicationProperties {
3131

32+
private static final String YAML_SPACE = " ";
33+
3234
private final Map<String, Object> properties = new HashMap<>();
3335

3436
/**
@@ -73,8 +75,50 @@ void writeTo(PrintWriter writer) {
7375
}
7476
}
7577

76-
void writeToYaml(PrintWriter writer) {
77-
// TODO: Impl
78+
void writeYaml(PrintWriter writer) {
79+
Map<String, Object> nested = flattenToNestedMap(this.properties);
80+
writeYamlRecursive(nested, writer, 0);
81+
}
82+
83+
private static Map<String, Object> flattenToNestedMap(Map<String, Object> flatMap) {
84+
Map<String, Object> nested = new HashMap<>();
85+
flatMap.forEach((key, value) -> {
86+
String[] path = parseKeyPath(key);
87+
insertValueAtPath(nested, path, value);
88+
});
89+
return nested;
90+
}
91+
92+
private static String[] parseKeyPath(String key) {
93+
return key.split("\\.");
94+
}
95+
96+
@SuppressWarnings("unchecked")
97+
private static void insertValueAtPath(Map<String, Object> map, String[] path, Object value) {
98+
Map<String, Object> current = map;
99+
for (int i = 0; i < path.length - 1; i++) {
100+
String segment = path[i];
101+
current = (Map<String, Object>) current.computeIfAbsent(segment, (k) -> new HashMap<>());
102+
}
103+
current.put(path[path.length - 1], value);
104+
}
105+
106+
private static void writeYamlRecursive(Map<String, Object> map, PrintWriter writer, int indent) {
107+
map.entrySet().forEach((entry) -> writeEntry(entry, writer, indent));
108+
}
109+
110+
@SuppressWarnings("unchecked")
111+
private static void writeEntry(Map.Entry<String, Object> entry, PrintWriter writer, int indent) {
112+
String indentStr = YAML_SPACE.repeat(indent);
113+
Object value = entry.getValue();
114+
115+
if (value instanceof Map<?, ?> nestedMap) {
116+
writer.printf("%s%s:%n", indentStr, entry.getKey());
117+
writeYamlRecursive((Map<String, Object>) nestedMap, writer, indent + 1);
118+
}
119+
else {
120+
writer.printf("%s%s: %s%n", indentStr, entry.getKey(), value);
121+
}
78122
}
79123

80124
private void add(String key, Object value) {

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/properties/ApplicationYamlPropertiesContributor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void contribute(Path projectRoot) throws IOException {
5050
}
5151
try (PrintWriter writer = new PrintWriter(Files.newOutputStream(output, StandardOpenOption.APPEND), false,
5252
StandardCharsets.UTF_8)) {
53-
this.properties.writeToYaml(writer);
53+
this.properties.writeYaml(writer);
5454
}
5555
}
5656

initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/properties/ApplicationPropertiesTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ void shouldFailOnExistingProperty() {
7171
.withMessage("Property 'test' already exists");
7272
}
7373

74+
@Test
75+
void writeYaml() {
76+
ApplicationProperties properties = new ApplicationProperties();
77+
properties.add("name", "testapp");
78+
properties.add("port", 8080);
79+
properties.add("app.version", "1.0");
80+
properties.add("db.host", "localhost");
81+
properties.add("app.config.debug", true);
82+
properties.add("db.connection.timeout", 30);
83+
String written = writeYaml(properties);
84+
assertThat(written).contains("name: testapp", "port: 8080", "app:\n config:\n debug: true\n version: 1.0",
85+
"db:\n host: localhost\n connection:\n timeout: 30");
86+
}
87+
7488
private String write(ApplicationProperties properties) {
7589
StringWriter stringWriter = new StringWriter();
7690
try (PrintWriter writer = new PrintWriter(stringWriter)) {
@@ -79,4 +93,12 @@ private String write(ApplicationProperties properties) {
7993
return stringWriter.toString();
8094
}
8195

96+
private String writeYaml(ApplicationProperties properties) {
97+
StringWriter stringWriter = new StringWriter();
98+
try (PrintWriter writer = new PrintWriter(stringWriter)) {
99+
properties.writeYaml(writer);
100+
}
101+
return stringWriter.toString();
102+
}
103+
82104
}

0 commit comments

Comments
 (0)