Skip to content

Commit 667b4df

Browse files
committed
started work
Signed-off-by: wind57 <[email protected]>
1 parent abf452a commit 667b4df

File tree

4 files changed

+41
-116
lines changed

4 files changed

+41
-116
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19-
import java.util.LinkedHashSet;
19+
import java.util.LinkedHashMap;
2020
import java.util.Map;
2121

2222
/**
@@ -25,10 +25,9 @@
2525
* Container that stores multiple sources, to be exact their names and their flattenned
2626
* data. We force a LinkedHashSet on purpose, to preserve the order of sources.
2727
*/
28-
public record MultipleSourcesContainer(LinkedHashSet<String> names, Map<String, Object> data) {
28+
public record MultipleSourcesContainer(LinkedHashMap<String, Map<String, Object>> data) {
2929

30-
private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashSet<>(0),
31-
Map.of());
30+
private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashMap<>(0));
3231

3332
public static MultipleSourcesContainer empty() {
3433
return EMPTY;

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.cloud.kubernetes.commons.config;
1818

1919
import java.util.HashMap;
20-
import java.util.LinkedHashSet;
20+
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222

2323
/**
@@ -32,47 +32,31 @@ private SourceDataFlattener() {
3232
/**
3333
* Flattens the data from rawData without any additional processing.
3434
*/
35-
static Map<String, Object> defaultFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
35+
static Map<String, Object> defaultFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
3636
Map<String, Object> flattenedData = new HashMap<>();
37-
38-
names.forEach(name -> {
39-
@SuppressWarnings("unchecked")
40-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
41-
flattenedData.putAll(singleDataEntry);
42-
});
43-
37+
sourceData.values().forEach(flattenedData::putAll);
4438
return flattenedData;
4539
}
4640

4741
/**
4842
* Flattens the data from rawData by adding a prefix for each key.
4943
*/
50-
static Map<String, Object> prefixFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData,
44+
static Map<String, Object> prefixFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData,
5145
String prefix) {
5246
Map<String, Object> flattenedData = new HashMap<>();
53-
54-
names.forEach(name -> {
55-
@SuppressWarnings("unchecked")
56-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
57-
singleDataEntry.forEach((key, value) -> flattenedData.put(prefix + "." + key, value));
58-
});
59-
47+
sourceData.values().forEach(data ->
48+
data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)));
6049
return flattenedData;
6150
}
6251

6352
/**
6453
* Flattens the data from rawData by adding a prefix for each key, which is equal to
6554
* the source name.
6655
*/
67-
static Map<String, Object> nameFlattenedSourceData(LinkedHashSet<String> names, Map<String, Object> rawData) {
56+
static Map<String, Object> nameFlattenedSourceData(LinkedHashMap<String, Map<String, Object>> sourceData) {
6857
Map<String, Object> flattenedData = new HashMap<>();
69-
70-
names.forEach(name -> {
71-
@SuppressWarnings("unchecked")
72-
Map<String, Object> singleDataEntry = (Map<String, Object>) rawData.getOrDefault(name, Map.of());
73-
singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value));
74-
});
75-
58+
sourceData.forEach((name, data) ->
59+
data.forEach((key, value) -> flattenedData.put(name + "." + key, value)));
7660
return flattenedData;
7761
}
7862

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.cloud.kubernetes.commons.config;
1818

1919
import java.util.HashMap;
20+
import java.util.LinkedHashMap;
2021
import java.util.LinkedHashSet;
2122
import java.util.Map;
2223

@@ -36,15 +37,11 @@ class SourceDataFlattenerTests {
3637
*/
3738
@Test
3839
void defaultFlattenedSourceDataNoOverlap() {
39-
LinkedHashSet<String> names = new LinkedHashSet<>();
40-
names.add("nameA");
41-
names.add("nameB");
40+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
41+
data.put("nameA", Map.of("a", "b"));
42+
data.put("nameB", Map.of("c", "d"));
4243

43-
Map<String, Object> rawData = new HashMap<>();
44-
rawData.put("nameA", Map.of("a", "b"));
45-
rawData.put("nameB", Map.of("c", "d"));
46-
47-
Map<String, Object> result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData);
44+
Map<String, Object> result = SourceDataFlattener.defaultFlattenedSourceData(data);
4845
Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "b", "c", "d"));
4946

5047
}
@@ -58,17 +55,12 @@ void defaultFlattenedSourceDataNoOverlap() {
5855
*/
5956
@Test
6057
void defaultFlattenedSourceDataOverlap() {
61-
LinkedHashSet<String> names = new LinkedHashSet<>();
62-
names.add("nameA");
63-
names.add("nameB");
64-
names.add("nameC");
65-
66-
Map<String, Object> rawData = new HashMap<>();
67-
rawData.put("nameA", Map.of("a", "b"));
68-
rawData.put("nameB", Map.of("c", "d"));
69-
rawData.put("nameC", Map.of("a", "w"));
58+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
59+
data.put("nameA", Map.of("a", "b"));
60+
data.put("nameB", Map.of("c", "d"));
61+
data.put("nameC", Map.of("a", "w"));
7062

71-
Map<String, Object> result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData);
63+
Map<String, Object> result = SourceDataFlattener.defaultFlattenedSourceData(data);
7264
Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "w", "c", "d"));
7365

7466
}
@@ -82,15 +74,11 @@ void defaultFlattenedSourceDataOverlap() {
8274
*/
8375
@Test
8476
void prefixFlattenedSourceDataNoOverlap() {
85-
LinkedHashSet<String> names = new LinkedHashSet<>();
86-
names.add("nameA");
87-
names.add("nameB");
88-
89-
Map<String, Object> rawData = new HashMap<>();
90-
rawData.put("nameA", Map.of("a", "b"));
91-
rawData.put("nameB", Map.of("c", "d"));
77+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
78+
data.put("nameA", Map.of("a", "b"));
79+
data.put("nameB", Map.of("c", "d"));
9280

93-
Map<String, Object> result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one");
81+
Map<String, Object> result = SourceDataFlattener.prefixFlattenedSourceData(data, "one");
9482
Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "b", "one.c", "d"));
9583

9684
}
@@ -105,17 +93,12 @@ void prefixFlattenedSourceDataNoOverlap() {
10593
*/
10694
@Test
10795
void prefixFlattenedSourceDataOverlap() {
108-
LinkedHashSet<String> names = new LinkedHashSet<>();
109-
names.add("nameA");
110-
names.add("nameB");
111-
names.add("nameC");
96+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
97+
data.put("nameA", Map.of("a", "b"));
98+
data.put("nameB", Map.of("c", "d"));
99+
data.put("nameC", Map.of("a", "w"));
112100

113-
Map<String, Object> rawData = new HashMap<>();
114-
rawData.put("nameA", Map.of("a", "b"));
115-
rawData.put("nameB", Map.of("c", "d"));
116-
rawData.put("nameC", Map.of("a", "w"));
117-
118-
Map<String, Object> result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one");
101+
Map<String, Object> result = SourceDataFlattener.prefixFlattenedSourceData(data, "one");
119102
Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "w", "one.c", "d"));
120103

121104
}
@@ -128,15 +111,11 @@ void prefixFlattenedSourceDataOverlap() {
128111
*/
129112
@Test
130113
void nameFlattenedSourceDataNoOverlap() {
131-
LinkedHashSet<String> names = new LinkedHashSet<>();
132-
names.add("nameA");
133-
names.add("nameB");
134-
135-
Map<String, Object> rawData = new HashMap<>();
136-
rawData.put("nameA", Map.of("a", "b"));
137-
rawData.put("nameB", Map.of("c", "d"));
114+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
115+
data.put("nameA", Map.of("a", "b"));
116+
data.put("nameB", Map.of("c", "d"));
138117

139-
Map<String, Object> result = SourceDataFlattener.nameFlattenedSourceData(names, rawData);
118+
Map<String, Object> result = SourceDataFlattener.nameFlattenedSourceData(data);
140119
Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d"));
141120

142121
}
@@ -150,17 +129,12 @@ void nameFlattenedSourceDataNoOverlap() {
150129
*/
151130
@Test
152131
void nameFlattenedSourceDataOverlap() {
153-
LinkedHashSet<String> names = new LinkedHashSet<>();
154-
names.add("nameA");
155-
names.add("nameB");
156-
names.add("nameC");
157-
158-
Map<String, Object> rawData = new HashMap<>();
159-
rawData.put("nameA", Map.of("a", "b"));
160-
rawData.put("nameB", Map.of("c", "d"));
161-
rawData.put("nameC", Map.of("a", "w"));
132+
LinkedHashMap<String, Map<String, Object>> data = new LinkedHashMap<>();
133+
data.put("nameA", Map.of("a", "b"));
134+
data.put("nameB", Map.of("c", "d"));
135+
data.put("nameC", Map.of("a", "w"));
162136

163-
Map<String, Object> result = SourceDataFlattener.nameFlattenedSourceData(names, rawData);
137+
Map<String, Object> result = SourceDataFlattener.nameFlattenedSourceData(data);
164138
Assertions.assertThat(result)
165139
.containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d", "nameC.a", "w"));
166140

0 commit comments

Comments
 (0)