Skip to content

Commit

Permalink
Converters - Add some shortcuts for JDK classes (#1284)
Browse files Browse the repository at this point in the history
It seems counter productive to throw so many NoSuchMethodExceptions for
these common uses cases.
  • Loading branch information
gsmet authored Jan 10, 2025
1 parent cd2e20d commit 2e4d51a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion implementation/src/main/java/io/smallrye/config/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config;

import static io.smallrye.config.common.utils.StringUtil.unquoted;

import java.io.File;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Array;
Expand All @@ -29,6 +29,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -187,6 +188,11 @@ private Converters() {
static final Converter<Path> PATH_CONVERTER = BuiltInConverter.of(18,
newEmptyValueConverter(Path::of));

static final Converter<File> FILE_CONVERTER = BuiltInConverter.of(19, newEmptyValueConverter(File::new));

static final Converter<URI> URI_CONVERTER = BuiltInConverter.of(20,
newTrimmingConverter(newEmptyValueConverter(URI::create)));

static final Map<Class<?>, Class<?>> PRIMITIVE_TYPES;

static final Map<Type, Converter<?>> ALL_CONVERTERS = new HashMap<>();
Expand Down Expand Up @@ -226,6 +232,9 @@ private Converters() {
ALL_CONVERTERS.put(Pattern.class, PATTERN_CONVERTER);

ALL_CONVERTERS.put(Path.class, PATH_CONVERTER);
ALL_CONVERTERS.put(File.class, FILE_CONVERTER);

ALL_CONVERTERS.put(URI.class, URI_CONVERTER);

Map<Class<?>, Class<?>> primitiveTypes = new HashMap<>(9);
primitiveTypes.put(byte.class, Byte.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
Expand Down Expand Up @@ -389,6 +391,26 @@ void nulls() {
assertThrows(NullPointerException.class, () -> convertNull(config, OptionalDouble.class));
}

@Test
void file() {
SmallRyeConfig config = buildConfig("file", "/test", "file.leading.space", " test");
assertEquals(new File("/test"), config.getValue("file", File.class));
assertEquals(new File(" test"), config.getValue("file.leading.space", File.class));
Converter<File> fileConverter = config.getConverterOrNull(File.class);
assertNotNull(fileConverter);
assertTrue(fileConverter.getClass().getName().contains("BuiltInConverter"));
}

@Test
void uri() {
SmallRyeConfig config = buildConfig("uri", "http://localhost", "uri.leading.space", " http://localhost");
assertEquals(URI.create("http://localhost"), config.getValue("uri", URI.class));
assertEquals(URI.create("http://localhost"), config.getValue("uri.leading.space", URI.class));
Converter<URI> uriConverter = config.getConverterOrNull(URI.class);
assertNotNull(uriConverter);
assertTrue(uriConverter.getClass().getName().contains("BuiltInConverter"));
}

@SafeVarargs
private static <T> T[] array(T... items) {
return items;
Expand Down

0 comments on commit 2e4d51a

Please sign in to comment.