Skip to content

Commit

Permalink
Avoid recomputing valid regexes String in FieldNameValidator.validate…
Browse files Browse the repository at this point in the history
…() (#1715)

Avoid recomputing valid regexes String in FieldNameValidator.validate()
  • Loading branch information
ash211 authored Jan 13, 2025
1 parent 2de77eb commit 77d2962
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1715.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Avoid recomputing valid regexes String in FieldNameValidator.validate()
links:
- https://github.com/palantir/conjure/pull/1715
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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");
}
Expand Down

0 comments on commit 77d2962

Please sign in to comment.