Skip to content

String to Pattern and String to Regex converters #23706

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

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 @@ -20,6 +20,7 @@
import java.util.Set;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.KotlinDetector;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
Expand Down Expand Up @@ -120,6 +121,10 @@ public static void addApplicationConverters(ConverterRegistry registry) {
registry.addConverter(new NumberToDataSizeConverter());
registry.addConverter(new StringToFileConverter());
registry.addConverter(new InputStreamSourceToByteArrayConverter());
registry.addConverter(new StringToPatternConverter());
if (KotlinDetector.isKotlinPresent()) {
registry.addConverter(StringToRegexConverter.INSTANCE);
}
registry.addConverterFactory(new LenientStringToEnumConverterFactory());
registry.addConverterFactory(new LenientBooleanToEnumConverterFactory());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.convert;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.Set;
import java.util.regex.Pattern;

/**
* {@link Converter} to convert from a {@link String} to a {@link Pattern}
*
* @author Mikhael Sokolov
* @see Pattern
*/
final class StringToPatternConverter implements GenericConverter {

@Override
public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new GenericConverter.ConvertiblePair(String.class, Pattern.class));
}

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (ObjectUtils.isEmpty(source)) return null;
return Pattern.compile((String) source, Pattern.MULTILINE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.convert

import org.springframework.core.convert.TypeDescriptor
import org.springframework.core.convert.converter.Converter
import org.springframework.core.convert.converter.GenericConverter

/**
* [Converter] to convert from a [String] to a [Regex]
*
* @author Mikhael Sokolov
* @see Regex
*/
internal object StringToRegexConverter : GenericConverter {
override fun getConvertibleTypes(): Set<GenericConverter.ConvertiblePair> = setOf(GenericConverter.ConvertiblePair(String::class.java, Regex::class.java))
override fun convert(str: Any?, p1: TypeDescriptor, p2: TypeDescriptor): Any? = (str as? String)?.ifEmpty { return null }?.toRegex(RegexOption.MULTILINE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.convert;

import org.junit.jupiter.params.provider.Arguments;
import org.springframework.core.convert.ConversionService;

import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link StringToPatternConverter}.
*
* @author Mikhael Sokolov
*/
class StringToPatternConverterTests {

@ConversionServiceTest
void convertWhenStringShouldReturnPattern(ConversionService conversionService) {
assertThat(convert(conversionService, "([A-Z])\\w+").pattern()).isEqualTo(Pattern.compile("([A-Z])\\w+", Pattern.MULTILINE).pattern());
assertThat(convert(conversionService, "(\\\\W)*(\\\\S)*").pattern()).isEqualTo(Pattern.compile("(\\\\W)*(\\\\S)*", Pattern.MULTILINE).pattern());
}

@ConversionServiceTest
void convertWhenEmptyShouldReturnNull(ConversionService conversionService) {
assertThat(convert(conversionService, "")).isNull();
}

private Pattern convert(ConversionService conversionService, String source) {
return conversionService.convert(source, Pattern.class);
}

@SuppressWarnings("unused")
static Stream<? extends Arguments> conversionServices() {
return ConversionServiceArguments.with(new StringToPatternConverter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.convert

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.provider.Arguments
import org.springframework.core.convert.ConversionService
import java.util.stream.Stream

/**
* Tests for [StringToRegexConverter].
*
* @author Mikhael Sokolov
*/
internal class StringToRegexConverterTests {

@ConversionServiceTest
fun convertWhenStringShouldReturnPattern(conversionService: ConversionService) {
assertThat(conversionService.convert("([A-Z])\\w+")?.pattern).isEqualTo(Regex("([A-Z])\\w+", RegexOption.MULTILINE).pattern)
assertThat(conversionService.convert("(\\\\W)*(\\\\S)*")?.pattern).isEqualTo(Regex("(\\\\W)*(\\\\S)*", RegexOption.MULTILINE).pattern)
}

@ConversionServiceTest
fun convertWhenEmptyShouldReturnNull(conversionService: ConversionService) {
assertThat(conversionService.convert("")).isNull()
}

private fun ConversionService.convert(source: String): Regex? = convert(source, Regex::class.java)

companion object {
@JvmStatic
fun conversionServices(): Stream<out Arguments?> = ConversionServiceArguments.with(StringToRegexConverter)
}
}