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

Add PropertyNamingStrategy.LOWER_DOT_CASE for dot-delimited names #2241

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -66,6 +66,13 @@ public class PropertyNamingStrategy
*/
public static final PropertyNamingStrategy KEBAB_CASE = new KebabCaseStrategy();

/**
* Naming convention widely used as configuration properties name, where words are in
* lower-case letters, separated by dots.
* See {@link LowerCaseWithDotsStrategy} for details.
*/
public static final PropertyNamingStrategy LOWER_CASE_WITH_DOTS = new LowerCaseWithDotsStrategy();

/*
/**********************************************************
/* API
Expand Down Expand Up @@ -246,7 +253,8 @@ public static class SnakeCaseStrategy extends PropertyNamingStrategyBase
@Override
public String translate(String input)
{
if (input == null) return input; // garbage in, garbage out
if (input == null)
return input; // garbage in, garbage out
int length = input.length();
StringBuilder result = new StringBuilder(length * 2);
int resultLength = 0;
Expand Down Expand Up @@ -343,41 +351,58 @@ public String translate(String input) {
public static class KebabCaseStrategy extends PropertyNamingStrategyBase
{
@Override
public String translate(String input)
{
if (input == null) return input; // garbage in, garbage out
int length = input.length();
if (length == 0) {
return input;
}
public String translate(String input){
return translateLowerCaseWithSeparator(input, '-');
}
}

StringBuilder result = new StringBuilder(length + (length >> 1));
/**
* Naming strategy similar to {@link KebabCaseStrategy}, but instead of hyphens
* as separators, uses dots. Naming convention widely used as configuration properties name.
*/
public static class LowerCaseWithDotsStrategy extends PropertyNamingStrategyBase {
@Override
public String translate(String input){
return translateLowerCaseWithSeparator(input, '.');
}
}

int upperCount = 0;
static String translateLowerCaseWithSeparator(String input, char separator){
if (input == null) {
return input; // garbage in, garbage out
}

for (int i = 0; i < length; ++i) {
char ch = input.charAt(i);
char lc = Character.toLowerCase(ch);

if (lc == ch) { // lower-case letter means we can get new word
// but need to check for multi-letter upper-case (acronym), where assumption
// is that the last upper-case char is start of a new word
if (upperCount > 1) {
// so insert hyphen before the last character now
result.insert(result.length() - 1, '-');
}
upperCount = 0;
} else {
// Otherwise starts new word, unless beginning of string
if ((upperCount == 0) && (i > 0)) {
result.append('-');
}
++upperCount;
int length = input.length();
if (length == 0) {
return input;
}

StringBuilder result = new StringBuilder(length + (length >> 1));

int upperCount = 0;

for (int i = 0; i < length; ++i) {
char ch = input.charAt(i);
char lc = Character.toLowerCase(ch);

if (lc == ch) { // lower-case letter means we can get new word
// but need to check for multi-letter upper-case (acronym), where assumption
// is that the last upper-case char is start of a new word
if (upperCount > 1) {
// so insert hyphen before the last character now
result.insert(result.length() - 1, separator);
}
upperCount = 0;
} else {
// Otherwise starts new word, unless beginning of string
if ((upperCount == 0) && (i > 0)) {
result.append(separator);
}
result.append(lc);
++upperCount;
}
return result.toString();
result.append(lc);
}
return result.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,40 @@ public void testSimpleKebabCase() throws Exception
assertEquals("Billy", result.firstName);
}

/*
/**********************************************************
/* Test methods for LOWER_CASE_WITH_DOTS
/**********************************************************
*/

public void testLowerCaseWithDotsStrategyStandAlone()
{
assertEquals("some.value",
PropertyNamingStrategy.LOWER_CASE_WITH_DOTS.nameForField(null, null, "someValue"));
assertEquals("some.value",
PropertyNamingStrategy.LOWER_CASE_WITH_DOTS.nameForField(null, null, "SomeValue"));
assertEquals("url",
PropertyNamingStrategy.LOWER_CASE_WITH_DOTS.nameForField(null, null, "URL"));
assertEquals("url.stuff",
PropertyNamingStrategy.LOWER_CASE_WITH_DOTS.nameForField(null, null, "URLStuff"));
assertEquals("some.url.stuff",
PropertyNamingStrategy.LOWER_CASE_WITH_DOTS.nameForField(null, null, "SomeURLStuff"));
}

public void testSimpleLowerCaseWithDots() throws Exception
{
final FirstNameBean input = new FirstNameBean("Bob");
ObjectMapper m = jsonMapperBuilder()
.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DOTS)
.build();

assertEquals(aposToQuotes("{'first.name':'Bob'}"), m.writeValueAsString(input));

FirstNameBean result = m.readValue(aposToQuotes("{'first.name':'Billy'}"),
FirstNameBean.class);
assertEquals("Billy", result.firstName);
}

/*
/**********************************************************
/* Test methods, other
Expand Down