diff --git a/changelog/@unreleased/pr-1715.v2.yml b/changelog/@unreleased/pr-1715.v2.yml new file mode 100644 index 000000000..8bcf6e1f7 --- /dev/null +++ b/changelog/@unreleased/pr-1715.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Avoid recomputing valid regexes String in FieldNameValidator.validate() + links: + - https://github.com/palantir/conjure/pull/1715 diff --git a/conjure-core/src/main/java/com/palantir/conjure/defs/validator/FieldNameValidator.java b/conjure-core/src/main/java/com/palantir/conjure/defs/validator/FieldNameValidator.java index 822b9403f..de164ec68 100644 --- a/conjure-core/src/main/java/com/palantir/conjure/defs/validator/FieldNameValidator.java +++ b/conjure-core/src/main/java/com/palantir/conjure/defs/validator/FieldNameValidator.java @@ -16,8 +16,8 @@ package com.palantir.conjure.defs.validator; -import com.google.common.base.Preconditions; import com.palantir.conjure.CaseConverter; +import com.palantir.conjure.CaseConverter.Case; import com.palantir.conjure.spec.FieldName; import java.util.Arrays; import org.apache.commons.lang3.StringUtils; @@ -47,13 +47,14 @@ public static FieldName toCase(FieldName fieldName, CaseConverter.Case targetCas public static void validate(FieldName fieldName) { boolean matchesCamelCase = CaseConverter.CAMEL_CASE_PATTERN.matches(fieldName.get()); - Preconditions.checkArgument( - matchesCamelCase - || CaseConverter.KEBAB_CASE_PATTERN.matches(fieldName.get()) - || CaseConverter.SNAKE_CASE_PATTERN.matches(fieldName.get()), - "FieldName \"%s\" must follow one of the following patterns: %s", - fieldName, - Arrays.toString(CaseConverter.Case.values())); + if (!matchesCamelCase + && !CaseConverter.KEBAB_CASE_PATTERN.matches(fieldName.get()) + && !CaseConverter.SNAKE_CASE_PATTERN.matches(fieldName.get())) { + // using if-throw instead of Preconditions to avoid recomputing the `Arrays.toString()` every time + throw new IllegalArgumentException(String.format( + "FieldName \"%s\" must follow one of the following patterns: %s", + fieldName, Arrays.toString(Case.values()))); + } if (!matchesCamelCase) { log.warn( diff --git a/conjure-core/src/test/java/com/palantir/conjure/defs/validator/FieldNameValidatorTest.java b/conjure-core/src/test/java/com/palantir/conjure/defs/validator/FieldNameValidatorTest.java index 0b8aa1fe6..ce77fb534 100644 --- a/conjure-core/src/test/java/com/palantir/conjure/defs/validator/FieldNameValidatorTest.java +++ b/conjure-core/src/test/java/com/palantir/conjure/defs/validator/FieldNameValidatorTest.java @@ -40,7 +40,7 @@ public void testValidNames() { } @Test - public void testInvalidNames() throws Exception { + public void testInvalidNames() { for (String invalid : new String[] { "UpperCamelCase", "Upper-Kebab-Case", @@ -55,18 +55,20 @@ public void testInvalidNames() throws Exception { }) { assertThatThrownBy(() -> FieldNameValidator.validate(FieldName.of(invalid))) .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining( - String.format("FieldName \"%s\" must follow one of the following patterns", invalid)); + .hasMessageContainingAll( + String.format("FieldName \"%s\" must follow one of the following patterns: ", invalid), + "LOWER_CAMEL_CASE", + "[A-Z]"); } } @Test - public void capitalize_should_turn_camel_case_into_sensible_class_name() throws Exception { + public void capitalize_should_turn_camel_case_into_sensible_class_name() { assertThat(FieldNameValidator.capitalize(FieldName.of("myVariant"))).isEqualTo("MyVariant"); } @Test - public void capitalize_capture_unused_behavior() throws Exception { + public void capitalize_capture_unused_behavior() { assertThat(FieldNameValidator.capitalize(FieldName.of("my-variant"))).isEqualTo("My-variant"); assertThat(FieldNameValidator.capitalize(FieldName.of("my_variant"))).isEqualTo("My_variant"); }