Skip to content

Commit

Permalink
Generate all mappings binding code (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Dec 22, 2023
1 parent d71500b commit 51245a1
Show file tree
Hide file tree
Showing 27 changed files with 3,789 additions and 2,443 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static String unquoted(final String name, final int begin, final int end)
throw new StringIndexOutOfBoundsException("begin " + begin + ", end " + end + ", length " + name.length());
}

if (name.length() < 2) {
if (name.length() < 2 || name.length() <= begin) {
return name;
}

Expand All @@ -279,6 +279,16 @@ public static String unquoted(final String name, final int begin, final int end)
}
}

public static int index(final String name) {
if (name.charAt(name.length() - 1) == ']') {
int start = name.lastIndexOf('[');
if (start != -1 && isNumeric(name, start + 1, name.length() - 1)) {
return Integer.parseInt(name.substring(start + 1, name.length() - 1));
}
}
throw new IllegalArgumentException();
}

public static String unindexed(final String name) {
if (name.length() < 3) {
return name;
Expand All @@ -294,17 +304,31 @@ public static String unindexed(final String name) {
return name;
}

public static boolean isIndexed(final String name) {
if (name.length() < 3) {
return false;
}

if (name.charAt(name.length() - 1) == ']') {
int begin = name.lastIndexOf('[');
return begin != -1 && isNumeric(name, begin + 1, name.length() - 1);
}

return false;
}

public static String skewer(String camelHumps) {
return skewer(camelHumps, '-');
}

public static String skewer(String camelHumps, char separator) {
int end = camelHumps.length();
StringBuilder b = new StringBuilder();
if (camelHumps.isEmpty()) {
throw new IllegalArgumentException("Method seems to have an empty name");
return camelHumps;
}

int end = camelHumps.length();
StringBuilder b = new StringBuilder();

for (int i = 0; i < end; i++) {
char c = camelHumps.charAt(i);
if (Character.isLowerCase(c)) {
Expand Down Expand Up @@ -335,6 +359,8 @@ public static String skewer(String camelHumps, char separator) {
i = j;
} else if (Character.isDigit(c)) {
b.append(c);
} else if (c == '.' || c == '*' || c == '[' || c == ']') {
b.append(c);
} else {
if (i > 0) {
char last = camelHumps.charAt(i - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -100,8 +99,8 @@ void escapingSituations() {
void skewer() {
assertEquals("sigusr1", StringUtil.skewer("sigusr1"));

assertThrows(IllegalArgumentException.class, () -> StringUtil.skewer(""));
assertThrows(IllegalArgumentException.class, () -> StringUtil.skewer("", '.'));
assertEquals("", StringUtil.skewer(""));
assertEquals(".", StringUtil.skewer("."));

assertEquals("my-property", StringUtil.skewer("myProperty"));
assertEquals("my.property", StringUtil.skewer("myProperty", '.'));
Expand Down Expand Up @@ -138,6 +137,11 @@ void skewer() {
assertEquals("trend-breaker", StringUtil.skewer("TrendBreaker"));
assertEquals("making-life-difficult", StringUtil.skewer("MAKING_LifeDifficult"));
assertEquals("making-life-difficult", StringUtil.skewer("makingLifeDifficult"));

assertEquals("foo.bar", StringUtil.skewer("foo.bar"));
assertEquals("foo.bar-baz", StringUtil.skewer("foo.barBaz"));
assertEquals("foo.bar-baz[0]", StringUtil.skewer("foo.barBaz[0]"));
assertEquals("foo.bar-baz[*]", StringUtil.skewer("foo.barBaz[*]"));
}

@Test
Expand Down Expand Up @@ -186,10 +190,16 @@ void unquoted() {
assertEquals("", StringUtil.unquoted("\"\""));
assertEquals("a", StringUtil.unquoted("a"));
assertEquals("unquoted", StringUtil.unquoted("\"unquoted\""));
assertEquals("my.\"unquoted\"", StringUtil.unquoted("my.\"unquoted\""));
assertEquals("unquoted", StringUtil.unquoted("my.\"unquoted\"", 3, 13));
assertEquals("unquoted", StringUtil.unquoted("my.unquoted", 3, 11));
}

@Test
void index() {
assertEquals(0, StringUtil.index("foo[0]"));
}

@Test
void unIndexed() {
assertEquals("", StringUtil.unindexed(""));
Expand Down
23 changes: 20 additions & 3 deletions implementation/src/main/java/io/smallrye/config/ConfigMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Function;

import io.smallrye.config.common.utils.StringUtil;

/**
* This annotation may be placed in interfaces to group configuration properties with a common prefix.
Expand Down Expand Up @@ -50,14 +53,28 @@ enum NamingStrategy {
/**
* The method name is used as is to map the configuration property.
*/
VERBATIM,
VERBATIM(name -> name),
/**
* The method name is derived by replacing case changes with a dash to map the configuration property.
*/
KEBAB_CASE,
KEBAB_CASE(name -> {
return StringUtil.skewer(name, '-');
}),
/**
* The method name is derived by replacing case changes with an underscore to map the configuration property.
*/
SNAKE_CASE
SNAKE_CASE(name -> {
return StringUtil.skewer(name, '_');
});

private final Function<String, String> function;

private NamingStrategy(Function<String, String> function) {
this.function = function;
}

public String apply(final String name) {
return function.apply(name);
}
}
}
Loading

0 comments on commit 51245a1

Please sign in to comment.