Skip to content

Commit 7baa222

Browse files
committed
pattern and regex converters
1 parent ad6ea94 commit 7baa222

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Set;
2121

2222
import org.springframework.beans.factory.ListableBeanFactory;
23+
import org.springframework.core.KotlinDetector;
2324
import org.springframework.core.convert.ConversionService;
2425
import org.springframework.core.convert.converter.Converter;
2526
import org.springframework.core.convert.converter.ConverterRegistry;
@@ -120,6 +121,10 @@ public static void addApplicationConverters(ConverterRegistry registry) {
120121
registry.addConverter(new NumberToDataSizeConverter());
121122
registry.addConverter(new StringToFileConverter());
122123
registry.addConverter(new InputStreamSourceToByteArrayConverter());
124+
registry.addConverter(new StringToPatternConverter());
125+
if (KotlinDetector.isKotlinPresent()) {
126+
registry.addConverter(StringToRegexConverter.INSTANCE);
127+
}
123128
registry.addConverterFactory(new LenientStringToEnumConverterFactory());
124129
registry.addConverterFactory(new LenientBooleanToEnumConverterFactory());
125130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.convert;
18+
19+
import org.springframework.core.convert.TypeDescriptor;
20+
import org.springframework.core.convert.converter.Converter;
21+
import org.springframework.core.convert.converter.GenericConverter;
22+
import org.springframework.util.ObjectUtils;
23+
24+
import java.util.Collections;
25+
import java.util.Set;
26+
import java.util.regex.Pattern;
27+
28+
/**
29+
* {@link Converter} to convert from a {@link String} to a {@link Pattern}
30+
*
31+
* @author Mikhael Sokolov
32+
* @see Pattern
33+
*/
34+
final class StringToPatternConverter implements GenericConverter {
35+
36+
@Override
37+
public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() {
38+
return Collections.singleton(new GenericConverter.ConvertiblePair(String.class, Pattern.class));
39+
}
40+
41+
@Override
42+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
43+
if (ObjectUtils.isEmpty(source)) return null;
44+
return Pattern.compile((String) source, Pattern.MULTILINE);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.convert
18+
19+
import org.springframework.core.convert.TypeDescriptor
20+
import org.springframework.core.convert.converter.Converter
21+
import org.springframework.core.convert.converter.GenericConverter
22+
23+
/**
24+
* [Converter] to convert from a [String] to a [Regex]
25+
*
26+
* @author Mikhael Sokolov
27+
* @see Regex
28+
*/
29+
internal object StringToRegexConverter : GenericConverter {
30+
override fun getConvertibleTypes(): Set<GenericConverter.ConvertiblePair> = setOf(GenericConverter.ConvertiblePair(String::class.java, Regex::class.java))
31+
override fun convert(str: Any?, p1: TypeDescriptor, p2: TypeDescriptor): Any? = (str as? String)?.ifEmpty { return null }?.toRegex(RegexOption.MULTILINE)
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/*
3+
* Copyright 2012-2020 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.springframework.boot.convert;
19+
20+
import org.junit.jupiter.params.provider.Arguments;
21+
import org.springframework.core.convert.ConversionService;
22+
23+
import java.util.regex.Pattern;
24+
import java.util.stream.Stream;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link StringToPatternConverter}.
30+
*
31+
* @author Mikhael Sokolov
32+
*/
33+
class StringToPatternConverterTests {
34+
35+
@ConversionServiceTest
36+
void convertWhenStringShouldReturnPattern(ConversionService conversionService) {
37+
assertThat(convert(conversionService, "([A-Z])\\w+").pattern()).isEqualTo(Pattern.compile("([A-Z])\\w+", Pattern.MULTILINE).pattern());
38+
assertThat(convert(conversionService, "(\\\\W)*(\\\\S)*").pattern()).isEqualTo(Pattern.compile("(\\\\W)*(\\\\S)*", Pattern.MULTILINE).pattern());
39+
}
40+
41+
@ConversionServiceTest
42+
void convertWhenEmptyShouldReturnNull(ConversionService conversionService) {
43+
assertThat(convert(conversionService, "")).isNull();
44+
}
45+
46+
private Pattern convert(ConversionService conversionService, String source) {
47+
return conversionService.convert(source, Pattern.class);
48+
}
49+
50+
@SuppressWarnings("unused")
51+
static Stream<? extends Arguments> conversionServices() {
52+
return ConversionServiceArguments.with(new StringToPatternConverter());
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.convert
17+
18+
import org.assertj.core.api.Assertions.assertThat
19+
import org.junit.jupiter.params.provider.Arguments
20+
import org.springframework.core.convert.ConversionService
21+
import java.util.stream.Stream
22+
23+
/**
24+
* Tests for [StringToRegexConverter].
25+
*
26+
* @author Mikhael Sokolov
27+
*/
28+
internal class StringToRegexConverterTests {
29+
30+
@ConversionServiceTest
31+
fun convertWhenStringShouldReturnPattern(conversionService: ConversionService) {
32+
assertThat(conversionService.convert("([A-Z])\\w+")?.pattern).isEqualTo(Regex("([A-Z])\\w+", RegexOption.MULTILINE).pattern)
33+
assertThat(conversionService.convert("(\\\\W)*(\\\\S)*")?.pattern).isEqualTo(Regex("(\\\\W)*(\\\\S)*", RegexOption.MULTILINE).pattern)
34+
}
35+
36+
@ConversionServiceTest
37+
fun convertWhenEmptyShouldReturnNull(conversionService: ConversionService) {
38+
assertThat(conversionService.convert("")).isNull()
39+
}
40+
41+
private fun ConversionService.convert(source: String): Regex? = convert(source, Regex::class.java)
42+
43+
companion object {
44+
@JvmStatic
45+
fun conversionServices(): Stream<out Arguments?> = ConversionServiceArguments.with(StringToRegexConverter)
46+
}
47+
}

0 commit comments

Comments
 (0)