parent
eeac38b70b
commit
641b72db65
|
@ -21,6 +21,7 @@ import java.util.Locale;
|
||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
|
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value object that represents the data that can be used by a {@link SanitizingFunction}.
|
* Value object that represents the data that can be used by a {@link SanitizingFunction}.
|
||||||
|
@ -38,7 +39,7 @@ public final class SanitizableData {
|
||||||
|
|
||||||
private final @Nullable PropertySource<?> propertySource;
|
private final @Nullable PropertySource<?> propertySource;
|
||||||
|
|
||||||
private final @Nullable String key;
|
private final String key;
|
||||||
|
|
||||||
private @Nullable String lowerCaseKey;
|
private @Nullable String lowerCaseKey;
|
||||||
|
|
||||||
|
@ -50,7 +51,8 @@ public final class SanitizableData {
|
||||||
* @param key the data key
|
* @param key the data key
|
||||||
* @param value the data value
|
* @param value the data value
|
||||||
*/
|
*/
|
||||||
public SanitizableData(@Nullable PropertySource<?> propertySource, @Nullable String key, @Nullable Object value) {
|
public SanitizableData(@Nullable PropertySource<?> propertySource, String key, @Nullable Object value) {
|
||||||
|
Assert.notNull(key, "'key' must not be null");
|
||||||
this.propertySource = propertySource;
|
this.propertySource = propertySource;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
@ -69,7 +71,7 @@ public final class SanitizableData {
|
||||||
* Return the key of the data.
|
* Return the key of the data.
|
||||||
* @return the data key
|
* @return the data key
|
||||||
*/
|
*/
|
||||||
public @Nullable String getKey() {
|
public String getKey() {
|
||||||
return this.key;
|
return this.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,9 +80,9 @@ public final class SanitizableData {
|
||||||
* @return the key as a lowercase value
|
* @return the key as a lowercase value
|
||||||
* @since 3.5.0
|
* @since 3.5.0
|
||||||
*/
|
*/
|
||||||
public @Nullable String getLowerCaseKey() {
|
public String getLowerCaseKey() {
|
||||||
String result = this.lowerCaseKey;
|
String result = this.lowerCaseKey;
|
||||||
if (result == null && this.key != null) {
|
if (result == null) {
|
||||||
result = this.key.toLowerCase(Locale.getDefault());
|
result = this.key.toLowerCase(Locale.getDefault());
|
||||||
this.lowerCaseKey = result;
|
this.lowerCaseKey = result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -399,13 +399,12 @@ public interface SanitizingFunction {
|
||||||
Assert.notNull(predicate, "'predicate' must not be null");
|
Assert.notNull(predicate, "'predicate' must not be null");
|
||||||
Assert.notNull(value, "'value' must not be null");
|
Assert.notNull(value, "'value' must not be null");
|
||||||
String lowerCaseValue = value.toLowerCase(Locale.getDefault());
|
String lowerCaseValue = value.toLowerCase(Locale.getDefault());
|
||||||
return (data) -> nullSafeTest(data.getLowerCaseKey(),
|
return (data) -> predicate.test(data.getLowerCaseKey(), lowerCaseValue);
|
||||||
(lowerCaseKey) -> predicate.test(lowerCaseKey, lowerCaseValue));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate<SanitizableData> onKey(Predicate<String> predicate) {
|
private Predicate<SanitizableData> onKey(Predicate<String> predicate) {
|
||||||
Assert.notNull(predicate, "'predicate' must not be null");
|
Assert.notNull(predicate, "'predicate' must not be null");
|
||||||
return (data) -> nullSafeTest(data.getKey(), predicate);
|
return (data) -> predicate.test(data.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate<SanitizableData> onValue(Predicate<Object> predicate) {
|
private Predicate<SanitizableData> onValue(Predicate<Object> predicate) {
|
||||||
|
|
|
@ -136,7 +136,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, "springx").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "springx").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -148,7 +147,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
|
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "atest").has(sanitizedValue());
|
assertThatApplyingToKey(function, "atest").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "bootx").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "bootx").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -160,7 +158,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
|
assertThatApplyingToKey(function, "boot").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "beet").has(sanitizedValue());
|
assertThatApplyingToKey(function, "beet").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "spring").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "spring").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -171,7 +168,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "XtestX").has(sanitizedValue());
|
assertThatApplyingToKey(function, "XtestX").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "YY").has(sanitizedValue());
|
assertThatApplyingToKey(function, "YY").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "xy").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "xy").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -182,7 +178,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "SPRING").has(sanitizedValue());
|
assertThatApplyingToKey(function, "SPRING").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "BOOT").has(sanitizedValue());
|
assertThatApplyingToKey(function, "BOOT").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -192,7 +187,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
|
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "xspring").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -204,7 +198,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "BO").has(sanitizedValue());
|
assertThatApplyingToKey(function, "BO").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "SPRING").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -213,7 +206,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToKey(function, "spring").has(sanitizedValue());
|
assertThatApplyingToKey(function, "spring").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
|
assertThatApplyingToKey(function, "spin").has(sanitizedValue());
|
||||||
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
assertThatApplyingToKey(function, "boot").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -223,7 +215,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToValue(function, "SPRING").has(sanitizedValue());
|
assertThatApplyingToValue(function, "SPRING").has(sanitizedValue());
|
||||||
assertThatApplyingToValue(function, "boot").has(sanitizedValue());
|
assertThatApplyingToValue(function, "boot").has(sanitizedValue());
|
||||||
assertThatApplyingToValue(function, "other").has(unsanitizedValue());
|
assertThatApplyingToValue(function, "other").has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -281,7 +272,6 @@ class SanitizingFunctionTests {
|
||||||
assertThatApplyingToValue(function, "spin").has(sanitizedValue());
|
assertThatApplyingToValue(function, "spin").has(sanitizedValue());
|
||||||
assertThatApplyingToValue(function, "boot").has(unsanitizedValue());
|
assertThatApplyingToValue(function, "boot").has(unsanitizedValue());
|
||||||
assertThatApplyingToValue(function, 123).has(unsanitizedValue());
|
assertThatApplyingToValue(function, 123).has(unsanitizedValue());
|
||||||
assertThatApplyingToKey(function, null).has(unsanitizedValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue