Skip to content

Commit a2dd49a

Browse files
committed
feat: added yaml configuration support
1 parent 2e575ab commit a2dd49a

File tree

14 files changed

+1045
-4
lines changed

14 files changed

+1045
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ deltaspike/core/impl/data
1616
deltaspike/modules/jsf/impl/data
1717
deltaspike/modules/security/impl/data
1818
*.log
19+
bin/
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.deltaspike.core.util;
20+
21+
import jakarta.enterprise.inject.Typed;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.StringJoiner;
25+
26+
/**
27+
* Utility to flatten nested {@link Map}s into a single level key:value pair
28+
* set of properties.
29+
*
30+
* @since 1.9.6
31+
*/
32+
@Typed
33+
public abstract class MapUtils
34+
{
35+
/**
36+
* Don't construct this class, you should only be using the
37+
* <code>static</code> methods.
38+
*/
39+
private MapUtils()
40+
{
41+
// Do nothing
42+
}
43+
44+
/**
45+
* Calls {@link #flattenMapProperties(Map, boolean)} with
46+
* <code>indexed</code> set to false.
47+
*
48+
* @param input Map of properties that may contain nested Maps.
49+
* @param <V> Type of values the {@link Map} contains.
50+
* @return Map of all properties indexed by their fully qualified names.
51+
* @see #flattenMapProperties(Map, boolean)
52+
*/
53+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input)
54+
{
55+
return flattenMapProperties(input, false);
56+
}
57+
58+
/**
59+
* <p>Converts a {@link Map} of objects to a flattened {@link Map} of
60+
* {@link String} values.</p>
61+
*
62+
* For example, with the given input:
63+
*
64+
* <pre><code>
65+
* Map&lt;String, Object&gt; application = Map.of(
66+
* "name", "My App",
67+
* "prefixes", List.of("&gt;", "$")
68+
* );
69+
*
70+
* Map&lt;String, Object&gt; map = Map.of("application", application);
71+
*
72+
* Map&lt;String, String&gt; result = MapUtils.flattenMapProperties(map);</code></pre>
73+
*
74+
* Will result in the following properties, assuming <code>indexed</code>
75+
* is <code>false</code>:
76+
*
77+
* <pre><code>
78+
* application.name=My App
79+
* application.prefixes=&gt;,$</code></pre>
80+
*
81+
* If <code>indexed</code> is <code>true</code>, the result would be:
82+
*
83+
* <pre><code>
84+
* application.name=My App
85+
* application.prefixes[0]=&gt;
86+
* application.prefixes[1]=$</code></pre>
87+
*
88+
*
89+
* @param input Map of properties that may contain nested Maps.
90+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
91+
* @param <V> Type of values the {@link Map} contains.
92+
* @return Map of all properties indexed by their fully qualified names.
93+
*/
94+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input, final boolean indexed)
95+
{
96+
final Map<String, String> result = new HashMap<>();
97+
flattenMapProperties(input, result, indexed);
98+
return result;
99+
}
100+
101+
/**
102+
* Calls {@link #flattenMapProperties(Map, Map, boolean, String)} with
103+
* parameter <code>prefix</code> as <code>null</code>, since when we begin
104+
* flattening the map, there is no prefix by default.
105+
*
106+
* @param input Map of properties that may contain nested Maps.
107+
* @param output Map that all properties are added to.
108+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
109+
* @param <V> Type of values the {@link Map} contains.
110+
* @see #flattenMapProperties(Map, Map, boolean, String)
111+
*/
112+
private static <V> void flattenMapProperties(final Map<String, V> input,
113+
final Map<String, String> output,
114+
final boolean indexed)
115+
{
116+
flattenMapProperties(input, output, indexed, null);
117+
}
118+
119+
/**
120+
* @param input Map of properties that may contain nested Maps.
121+
* @param output Map that all properties are added to.
122+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
123+
* @param prefix Name to prefix to any properties found on this level.
124+
* @param <V> Type of values the {@link Map} contains.
125+
*/
126+
private static <V> void flattenMapProperties(final Map<String, V> input,
127+
final Map<String, String> output,
128+
final boolean indexed,
129+
final String prefix)
130+
{
131+
input.forEach((key, value) ->
132+
{
133+
if (value == null)
134+
{
135+
return;
136+
}
137+
138+
final String k = (prefix == null) ? key : (prefix + '.' + key);
139+
140+
if (value instanceof Map)
141+
{
142+
flattenMapProperties((Map) value, output, indexed, k);
143+
}
144+
else if (value instanceof Iterable)
145+
{
146+
addIterable((Iterable) value, k, output, indexed);
147+
}
148+
else
149+
{
150+
output.put(k, (output.containsKey(k)) ? output.get(k) + "," + value : value.toString());
151+
}
152+
});
153+
}
154+
155+
/**
156+
* @param value Array of values that needs to be flattened.
157+
* @param key Property name for this value.
158+
* @param output Map that all properties are added to.
159+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
160+
* @param <V> Type of values the {@link Map} contains.
161+
*/
162+
private static <V> void addIterable(final Iterable<V> value,
163+
final String key,
164+
final Map<String, String> output,
165+
final boolean indexed)
166+
{
167+
final StringJoiner joiner = new StringJoiner(",");
168+
int index = 0;
169+
170+
for (final Object o : value)
171+
{
172+
if (o instanceof Map)
173+
{
174+
final Map map = (Map) o;
175+
176+
if (map.isEmpty())
177+
{
178+
continue;
179+
}
180+
181+
final String keyPrefix = (indexed) ? key + "[" + index++ + "]" : key;
182+
flattenMapProperties((Map) map, output, indexed, keyPrefix);
183+
}
184+
else
185+
{
186+
joiner.add(o.toString());
187+
}
188+
}
189+
190+
if (joiner.length() > 0)
191+
{
192+
output.put(key, joiner.toString());
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)