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

TEXT-217: Add SnakeCase Parsing #552

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
193 changes: 193 additions & 0 deletions src/main/java/org/apache/commons/text/CasedString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.text;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* Handles converting from one string case to another (e.g. camel case to snake case).
* @since 1.13.0
*/
public class CasedString {
/** The string of the cased format. */
private final String string;
/** The case of the string. */
private final StringCase stringCase;

/**
* A method to join camel string fragments together.
*/
private static final Function<String[], String> CAMEL_JOINER = a -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me a JOINER should be kept in it's specifc case enum.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to do this. Every attempt I made resulted in the enum not being able to locate the method.

StringBuilder sb = new StringBuilder(a[0].toLowerCase(Locale.ROOT));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should account for size 0 and null input, unless the framework guarantees otherwise, in which case that fact can go in the functions Javadoc.


for (int i = 1; i < a.length; i++) {
sb.append(WordUtils.capitalize(a[i].toLowerCase(Locale.ROOT)));
}
return sb.toString();
};

/**
* An enumeration of supported string cases. These cases tag strings as having a specific format.
*/
public enum StringCase {
/**
* Camel case tags strings like 'camelCase'. This conversion forces the first character to of the result to be
* lower case. Other than the upper case separating character all other characters are lower cased.
*/
CAMEL(Character::isUpperCase, true, CAMEL_JOINER),
/**
* Snake case tags strings like 'Snake_Case'. This conversion does not change the capitalization of any characters
* in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase},
* {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required.
*/
SNAKE(c -> c == '_', false, a -> String.join("_", a)),
/**
* Kebab case tags strings like 'kebab-case'. This conversion does not change the capitalization of any characters
* in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase},
* {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required.
*/
KEBAB(c -> c == '-', false, a -> String.join("-", a)),

/**
* Phrase case tags phrases of words like 'phrase case'. This conversion does not change the capitalization of any characters
* in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase},
* {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required.
*/
PHRASE(Character::isWhitespace, false, a -> String.join(" ", a)),

/**
* Dot case tags phrases of words like 'phrase.case'. This conversion does not change the capitalization of any characters
* in the string. If specific capitalization is required use {@code String.upperCase}, {@code String.upperCase},
* {@code WordUtils.capitalize()}, or {@code WordUtils.uncapitalize()} as required.
*/
DOT(c -> c == '.', false, a -> String.join(".", a));

/** The segment representation of a null String. */
private static final String[] NULL_STRING = new String[0];
/** The segment representation of an empty String. */
private static final String[] EMPTY_STRING = new String[]{""};

/** Tests for split position character. */
private final Predicate<Character> splitter;
/** If {@code true} split position character will be preserved in following segment. */
private final boolean preserveSplit;
/** A function to joining the segments into this case type. */
private final Function<String[], String> joiner;

/**
* Defines a String Case.
* @param splitter The predicate that determines when a new word in the cased string begins. This function will never receive
* {@code null} argument.
* @param preserveSplit if {@code true} the character that the splitter detected is preserved as the first character of the new word.
* @param joiner The function to merge a list of strings into the cased String.
*/
StringCase(final Predicate<Character> splitter, final boolean preserveSplit, final Function<String[], String> joiner) {
this.splitter = splitter;
this.preserveSplit = preserveSplit;
this.joiner = joiner;
}

/**
* Creates a cased string from a collection of segments.
* @param segments the segments to create the CasedString from. A {@code null} or zero length argument will result in an empty string.
* @return a string that is formatted for this Cased type.
*/
public String assemble(String[] segments) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does null input mean here?

return segments == null || segments.length == 0 ? null : this.joiner.apply(segments);
}

/**
* Returns an array of each of the segments in this CasedString. Segments are defined as the strings between
* the separators in the CasedString. for the CAMEL case the segments are determined by the presence of a capital letter.
* @return the array of Strings that are segments of the cased string.
*/
public String[] getSegments(String string) {
if (string == null) {
return NULL_STRING;
}
if (string.isEmpty()) {
return EMPTY_STRING;
}
List<String> lst = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (char c : string.toCharArray()) {
if (splitter.test(c)) {
if (sb.length() > 0) {
lst.add(sb.toString());
sb.setLength(0);
}
if (preserveSplit) {
sb.append(c);
}
} else {
sb.append(c);
}
}
if (sb.length() > 0) {
lst.add(sb.toString());
}
return lst.toArray(new String[0]);
}
}

/**
* A representation of a cased string and the identified case of that string.
* @param stringCase The {@code StringCase} that the {@code string} argument is in.
* @param string The string.
*/
public CasedString(StringCase stringCase, String string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor should not let me construct illegal instances, for example " a b c " should not be an allowed camel case string (or a kebab, or a snake string). Otherwise, it's just GIGO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just GIGO. I expect that you know what the string looks like that you are starting with. So I guess it is not really "parsing" but converting. Perhaps it does not belong with TEXT-217 but should have its own ticket.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not acceptable IMO, when I create a BigInteger or a Duration, I get an exception when I feed the methods garbage. It should be the same here, fail-fast is always better in this case, otherwise, I could get all sorts of junk down the line.

I expect that you know what the string looks like that you are starting with.

This will not only be for user input, where typos could still happen of course.

When you start with an Open API or XML Schema, you hope the input is well-formed and valid, but even if so, the contents may be junky.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way that I can think of that will not produce errors down the line for somebody. The commitment here is that the string will be parsed into segments based upon the splitter rule, and it does that. Other than the SnakeCase the conversions do not change the case of the characters. If you want to validate the result then the external code should do that before or after the conversion.

I ended up developing this code because I needed to convert commons-cli based long kebab keys into Java class and variable names to convert the CLI interface into Ant and Maven build tools for the Rat project.

Perhaps it does not belong here, but I went looking for a lightweight conversion utility and found none.

I think that the "parser" moniker does not fit and that conversion is probably the correct term. So this is really not a fix for TEXT-217 as it does not "parse".

this.string = string == null ? null : stringCase.assemble(stringCase.getSegments(string.trim()));
this.stringCase = stringCase;
}

/**
* Returns an array of each of the segments in this CasedString. Segments are defined as the strings between
* the separators in the CasedString. for the CAMEL case the segments are determined by the presence of a capital letter.
* @return the array of Strings that are segments of the cased string.
*/
public String[] getSegments() {
return stringCase.getSegments(string);
}

/**
* Converts this cased string into a {@code String} of another format.
* The upper/lower case of the characters within the string are not modified.
* @param stringCase THe fomrat to convert to.
* @return the String current string represented in the new format.
*/
public String toCase(StringCase stringCase) {
if (stringCase == this.stringCase) {
return string;
}
return string == null ? null : stringCase.joiner.apply(getSegments());
}

/**
* Returns the string representation provided in the constructor.
* @return the string representation.
*/
@Override
public String toString() {
return string;
}
}
158 changes: 158 additions & 0 deletions src/test/java/org/apache/commons/text/CasedStringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.text;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.text.CasedString.StringCase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class CasedStringTest {

private static String helloWorldValue(StringCase stringCase) {
switch (stringCase) {
case CAMEL:
return "helloWorld";
case KEBAB:
return "hello-World";
case PHRASE:
return "hello World";
case SNAKE:
return "hello_World";
case DOT:
return "hello.World";
default:
fail("Unsupported StringCase: " + stringCase);
}
return null; // keeps compiler happy
}

private static final CasedString CAMEL = new CasedString(StringCase.CAMEL, "aCamelString");
private static final CasedString PHRASE = new CasedString(StringCase.PHRASE, "A test PhrAse");
private static final CasedString KEBAB = new CasedString(StringCase.KEBAB, "A-kebAb-string");
private static final CasedString SNAKE = new CasedString(StringCase.SNAKE, "A_snaKE_string");
private static final CasedString DOT = new CasedString(StringCase.DOT, "A.dOt.string");
private static final CasedString ABCDEF = new CasedString(StringCase.PHRASE, "a b c @def");
/**
* tests the conversion from each Cased string type to every other type.
* @param underTest the CasedString being tested.
*/
@ParameterizedTest
@MethodSource("conversionProvider")
public void testCrossProductConversions(CasedString underTest) {
for (StringCase stringCase : StringCase.values()) {
assertEquals(helloWorldValue(stringCase), underTest.toCase(stringCase), () -> "failed converting to " + stringCase);
}
}
/* generates the hello world Cased String for every StringCase */
private static Stream<Arguments> conversionProvider() {
List<Arguments> lst = new ArrayList<>();
for (StringCase stringCase : StringCase.values()) {
lst.add(Arguments.of(new CasedString(stringCase, helloWorldValue(stringCase))));
}
return lst.stream();
}

@Test
public void testNullConstructor() {
for (StringCase stringCase : StringCase.values()) {
CasedString underTest = new CasedString(stringCase, null);
assertThat(underTest.toString()).isNull();
assertThat(underTest.getSegments()).isEmpty();
// test a null underTest can convert to all others types.
for (CasedString.StringCase otherCase : StringCase.values()) {
assertThat(underTest.toCase(otherCase)).isNull();
}
}
}

@Test
public void testToCamelCase() {
assertThat(new CasedString(StringCase.CAMEL, "").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.CAMEL, " ").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.CAMEL, "Tocamelcase").toString()).isEqualTo("tocamelcase");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the opposite of what I expect: It can't be used to create a Java method name. This hints that there are different kinds of camel cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cam across a reference that says camel case is lower case first character and pascal case is upper case first character. to create a java method name use

CasedString cString = new CasedString(StringCase.....
String javaClassName = WordUtils.capitalise(cCstring.toCase(StringCase.CAMEL));
String javaVarName = cString.toCase(StringCase.CAMEL));

Alternatively we could add PASCAL case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cam across a reference that says camel case is lower case first character and pascal case is upper case first character. to create a java method name use

CasedString cString = new CasedString(StringCase.....
String javaClassName = WordUtils.capitalise(cCstring.toCase(StringCase.CAMEL));
String javaVarName = cString.toCase(StringCase.CAMEL));

Alternatively we could add PASCAL case.

That's what I'm getting at, camel case is ambiguous depending on your context. In Java it means one thing for classes and another for method names. And then there is Pascal (it's not an acronym BTW).

Camel case is a way of writing phrases without spaces, where the first letter of each word is capitalized, except for the first letter of the entire compound word, which may be either upper or lower case.

From https://developer.mozilla.org/en-US/docs/Glossary/Camel_case

This tells me that StringCase.CAMEL does not define anything precise. StringCase.HIGHER_CAMEL, StringCase.LOWER_CAMEL lets you know what you get and it is one variation.

Another variation is what to do with words that are in all caps like acronyms. From the same URL:

Note that if the phrase contains acronyms (such as URI and HTML), camel casing practices vary. Some prefer to keep all of them capitalized, such as encodeURIComponent above. This may sometimes lead to ambiguity with multiple consecutive acronyms, such as XMLHTTPRequest. Others prefer to only capitalize the first letter, as XmlHttpRequest. The actual global variable, XMLHttpRequest, uses a mix of both.

If the input is "XMLHttpRequest", it would be odd to get back a camel string of "xMLHttpRequest" for LOWER_CAMEL, I'd expect "xmlHttpRequest".

All of this to say, that we need to define the behavior better IMO.

assertThat(new CasedString(StringCase.PHRASE, "\uD800\uDF00 \uD800\uDF02").toCase(StringCase.CAMEL)).isEqualTo("\uD800\uDF00\uD800\uDF02");
assertThat(ABCDEF.toCase(StringCase.CAMEL)).isEqualTo("aBC@def");
assertThat(CAMEL.toCase(StringCase.CAMEL)).isEqualTo("aCamelString");
assertThat(PHRASE.toCase(StringCase.CAMEL)).isEqualTo("aTestPhrase");
assertThat(KEBAB.toCase(StringCase.CAMEL)).isEqualTo("aKebabString");
assertThat(SNAKE.toCase(StringCase.CAMEL)).isEqualTo("aSnakeString");
assertThat(DOT.toCase(StringCase.CAMEL)).isEqualTo("aDotString");
assertThat(new CasedString(StringCase.PHRASE, "TO CAMEL CASE").toCase(StringCase.CAMEL)).isEqualTo("toCamelCase");
assertThat(new CasedString(StringCase.KEBAB, " to-CAMEL-cASE").toCase(StringCase.CAMEL)).isEqualTo("toCamelCase");
assertThat(new CasedString(StringCase.DOT, "To.Camel.Case").toCase(StringCase.CAMEL)).isEqualTo("toCamelCase");
}

@Test
public void testToPhraseTest() {
assertThat(new CasedString(StringCase.PHRASE, "").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.PHRASE, " ").toString()).isEqualTo("");
assertThat(ABCDEF.toCase(StringCase.PHRASE)).isEqualTo("a b c @def");
assertThat(CAMEL.toCase(StringCase.PHRASE)).isEqualTo("a Camel String");
assertThat(PHRASE.toCase(StringCase.PHRASE)).isEqualTo("A test PhrAse");
assertThat(KEBAB.toCase(StringCase.PHRASE)).isEqualTo("A kebAb string");
assertThat(SNAKE.toCase(StringCase.PHRASE)).isEqualTo("A snaKE string");
assertThat(DOT.toCase(StringCase.PHRASE)).isEqualTo("A dOt string");
}

@Test
public void testToKebabTest() {
assertThat(new CasedString(StringCase.KEBAB, "").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.KEBAB, " ").toString()).isEqualTo("");
assertThat(ABCDEF.toCase(StringCase.KEBAB)).isEqualTo("a-b-c-@def");
assertThat(CAMEL.toCase(StringCase.KEBAB)).isEqualTo("a-Camel-String");
assertThat(PHRASE.toCase(StringCase.KEBAB)).isEqualTo("A-test-PhrAse");
assertThat(KEBAB.toCase(StringCase.KEBAB)).isEqualTo("A-kebAb-string");
assertThat(SNAKE.toCase(StringCase.KEBAB)).isEqualTo("A-snaKE-string");
assertThat(DOT.toCase(StringCase.KEBAB)).isEqualTo("A-dOt-string");
}

@Test
public void testToSnakeTest() {
assertThat(new CasedString(StringCase.SNAKE, "").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.SNAKE, " ").toString()).isEqualTo("");
assertThat(ABCDEF.toCase(StringCase.SNAKE)).isEqualTo("a_b_c_@def");
assertThat(CAMEL.toCase(StringCase.SNAKE)).isEqualTo("a_Camel_String");
assertThat(PHRASE.toCase(StringCase.SNAKE)).isEqualTo("A_test_PhrAse");
assertThat(KEBAB.toCase(StringCase.SNAKE)).isEqualTo("A_kebAb_string");
assertThat(SNAKE.toCase(StringCase.SNAKE)).isEqualTo("A_snaKE_string");
assertThat(DOT.toCase(StringCase.SNAKE)).isEqualTo("A_dOt_string");
}

@Test
public void testToDotTest() {
assertThat(new CasedString(StringCase.DOT, "").toString()).isEqualTo("");
assertThat(new CasedString(StringCase.DOT, " ").toString()).isEqualTo("");
assertThat(ABCDEF.toCase(StringCase.DOT)).isEqualTo("a.b.c.@def");
assertThat(CAMEL.toCase(StringCase.DOT)).isEqualTo("a.Camel.String");
assertThat(PHRASE.toCase(StringCase.DOT)).isEqualTo("A.test.PhrAse");
assertThat(KEBAB.toCase(StringCase.DOT)).isEqualTo("A.kebAb.string");
assertThat(SNAKE.toCase(StringCase.DOT)).isEqualTo("A.snaKE.string");
assertThat(DOT.toCase(StringCase.DOT)).isEqualTo("A.dOt.string");
}
}
Loading