Skip to content

Commit e6cca92

Browse files
committed
feat: added yaml configuration support
1 parent 149dd80 commit e6cca92

File tree

14 files changed

+1043
-4
lines changed

14 files changed

+1043
-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: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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 set
28+
* of properties.
29+
*
30+
* @since 2.0.0
31+
*/
32+
@Typed
33+
public abstract class MapUtils
34+
{
35+
/**
36+
* Don't construct this class. Only use the <code>static</code> methods.
37+
*/
38+
private MapUtils()
39+
{
40+
// Do nothing
41+
}
42+
43+
/**
44+
* Calls {@link #flattenMapProperties(Map, boolean)} with
45+
* <code>indexed=false</code>.
46+
*
47+
* @param input Map of properties that may contain nested Maps.
48+
* @param <V> Type of values the {@link Map} contains.
49+
* @return Map of all properties indexed by their fully qualified names.
50+
* @see #flattenMapProperties(Map, boolean)
51+
*/
52+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input)
53+
{
54+
return flattenMapProperties(input, false);
55+
}
56+
57+
/**
58+
* <p>Converts a {@link Map} of objects to a flattened {@link Map} of
59+
* {@link String} values.</p>
60+
*
61+
* For example, with the given input:
62+
*
63+
* <pre><code>
64+
* Map&lt;String, Object&gt; application = Map.of(
65+
* "name", "My App",
66+
* "prefixes", List.of("&gt;", "$")
67+
* );
68+
*
69+
* Map&lt;String, Object&gt; map = Map.of("application", application);
70+
* Map&lt;String, String&gt; result = MapUtils.flattenMapProperties(map);</code></pre>
71+
*
72+
* Will result in the following properties, assuming <code>indexed</code>
73+
* is <code>false</code>:
74+
*
75+
* <pre><code>
76+
* application.name=My App
77+
* application.prefixes=&gt;,$</code></pre>
78+
*
79+
* If <code>indexed</code> is <code>true</code>, the result would be:
80+
*
81+
* <pre><code>
82+
* application.name=My App
83+
* application.prefixes[0]=&gt;
84+
* application.prefixes[1]=$</code></pre>
85+
*
86+
*
87+
* @param input Map of properties that may contain nested Maps.
88+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
89+
* @param <V> Type of values the {@link Map} contains.
90+
* @return Map of all properties indexed by their fully qualified names.
91+
*/
92+
public static <V> Map<String, String> flattenMapProperties(final Map<String, V> input, final boolean indexed)
93+
{
94+
final Map<String, String> result = new HashMap<>();
95+
flattenMapProperties(input, result, indexed);
96+
return result;
97+
}
98+
99+
/**
100+
* Calls {@link #flattenMapProperties(Map, Map, boolean, String)} with
101+
* parameter <code>prefix</code> as <code>null</code>, since when we begin
102+
* flattening the map, there is no prefix by default.
103+
*
104+
* @param input Map of properties that may contain nested Maps.
105+
* @param output Map that all properties are added to.
106+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
107+
* @param <V> Type of values the {@link Map} contains.
108+
* @see #flattenMapProperties(Map, Map, boolean, String)
109+
*/
110+
private static <V> void flattenMapProperties(final Map<String, V> input,
111+
final Map<String, String> output,
112+
final boolean indexed)
113+
{
114+
flattenMapProperties(input, output, indexed, null);
115+
}
116+
117+
/**
118+
* @param input Map of properties that may contain nested Maps.
119+
* @param output Map that all properties are added to.
120+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
121+
* @param prefix Name to prefix to any properties found on this level.
122+
* @param <V> Type of values the {@link Map} contains.
123+
*/
124+
private static <V> void flattenMapProperties(final Map<String, V> input,
125+
final Map<String, String> output,
126+
final boolean indexed,
127+
final String prefix)
128+
{
129+
input.forEach((key, value) ->
130+
{
131+
if (value == null)
132+
{
133+
return;
134+
}
135+
136+
final String k = (prefix == null) ? key : (prefix + '.' + key);
137+
138+
if (value instanceof Map)
139+
{
140+
flattenMapProperties((Map) value, output, indexed, k);
141+
}
142+
else if (value instanceof Iterable)
143+
{
144+
addIterable((Iterable) value, k, output, indexed);
145+
}
146+
else
147+
{
148+
output.put(k, (output.containsKey(k)) ? output.get(k) + "," + value : value.toString());
149+
}
150+
});
151+
}
152+
153+
/**
154+
* @param value Array of values that needs to be flattened.
155+
* @param key Property name for this value.
156+
* @param output Map that all properties are added to.
157+
* @param indexed If arrays are converted to multiple properties, or a comma separated list.
158+
* @param <V> Type of values the {@link Map} contains.
159+
*/
160+
private static <V> void addIterable(final Iterable<V> value,
161+
final String key,
162+
final Map<String, String> output,
163+
final boolean indexed)
164+
{
165+
final StringJoiner joiner = new StringJoiner(",");
166+
int index = 0;
167+
168+
for (final Object o : value)
169+
{
170+
if (o instanceof Map)
171+
{
172+
final Map map = (Map) o;
173+
174+
if (map.isEmpty())
175+
{
176+
continue;
177+
}
178+
179+
final String keyPrefix = (indexed) ? key + "[" + index++ + "]" : key;
180+
flattenMapProperties((Map) map, output, indexed, keyPrefix);
181+
}
182+
else
183+
{
184+
joiner.add(o.toString());
185+
}
186+
}
187+
188+
if (joiner.length() > 0)
189+
{
190+
output.put(key, joiner.toString());
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)