Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for arrays to the Generic Binding #679

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class PropertiesUtil {

private static final Pattern ARRAY_PATTERN = Pattern.compile("\\[([^\\]]+)\\]");
timonback marked this conversation as resolved.
Show resolved Hide resolved

public static Map<String, Object> toMap(String[] propertyStrings) {
return convertPropertiesToNestedMap(buildPropertiesFrom((propertyStrings)));
}
Expand All @@ -39,17 +43,33 @@ private static Map<String, Object> convertPropertiesToNestedMap(Properties prope
Map<String, Object> mapNode = bindingData;
while (path.size() > 1) {
String pathElement = path.get(0);
if (!mapNode.containsKey(pathElement)) {
mapNode.put(pathElement, new HashMap<>());
}

mapNode.computeIfAbsent(pathElement, k -> new HashMap<>());
timonback marked this conversation as resolved.
Show resolved Hide resolved
mapNode = (Map<String, Object>) mapNode.get(pathElement);

path.pop();
}

mapNode.put(path.get(0), properties.get(propertyName));
var value = parseValue(properties.get(propertyName));
mapNode.put(path.get(0), value);
}
return bindingData;
}

private static Object parseValue(Object input) {
if (input instanceof String inputString) {
Matcher matcher = ARRAY_PATTERN.matcher(inputString);

// Check if the pattern matches the input string
if (matcher.find()) {
// Extract the key and values
String[] values = matcher.group(1).split(",");
for (int i = 0; i < values.length; i++) {
values[i] = values[i].trim();
}
return Arrays.asList(values);
timonback marked this conversation as resolved.
Show resolved Hide resolved
}
}

return input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void testClassWithoutAnnotation() {
List<ProcessedOperationBinding> result = getProcessedOperationBindings(ClassWithoutAnnotation.class);

// then
assertThat(result).hasSize(0);
assertThat(result).isEmpty();
}

@Test
Expand All @@ -41,12 +41,11 @@ void testClassWithAnnotationHasABinding() {
}

private List<ProcessedOperationBinding> getProcessedOperationBindings(Class<?> testClass) {
List<ProcessedOperationBinding> result = Arrays.stream(testClass.getDeclaredMethods())
return Arrays.stream(testClass.getDeclaredMethods())
.map((m) -> m.getAnnotationsByType(AsyncGenericOperationBinding.class))
.flatMap(Arrays::stream)
.map(processor::mapToOperationBinding)
.toList();
return result;
}

private static class ClassWithoutAnnotation {
Expand Down Expand Up @@ -101,6 +100,30 @@ void twoPropertiesTest() {
assertThat(result).isEqualTo(Map.of("key1", "value1", "key2", "value2"));
}

@Test
void arrayPropertyTest() {
// given
String[] strings = {"key=[value1, value2, value3]"};
sam0r040 marked this conversation as resolved.
Show resolved Hide resolved

// when
Map<String, Object> result = PropertiesUtil.toMap(strings);

// then
assertThat(result).isEqualTo(Map.of("key", List.of("value1", "value2", "value3")));
}

@Test
void simpleMapPropertyTest() {
// given
String[] strings = {"map.key1=value1", "map.key2=value2", "map.key3=value3"};

// when
Map<String, Object> result = PropertiesUtil.toMap(strings);

// then
assertThat(result).isEqualTo(Map.of("map", Map.of("key1", "value1", "key2", "value2", "key3", "value3")));
}

@Test
void nestedPropertyTest() {
// given
Expand Down Expand Up @@ -136,5 +159,17 @@ void yamlSyntaxDoesWorkAsWell() {
// then
assertThat(result).isEqualTo(Map.of("key", "value"));
}

@Test
void yamlSyntaxArrayPropertyTest() {
// given
String[] strings = {"key: [value1, value2, value3]"};
timonback marked this conversation as resolved.
Show resolved Hide resolved

// when
Map<String, Object> result = PropertiesUtil.toMap(strings);

// then
assertThat(result).isEqualTo(Map.of("key", List.of("value1", "value2", "value3")));
}
}
}