Skip to content

Commit 0ee9153

Browse files
committed
URL converter
1 parent 7d6eba4 commit 0ee9153

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2017-2023 original 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 io.micronaut.aws.dynamodb.converters;
17+
18+
import io.micronaut.core.convert.ConversionContext;
19+
import io.micronaut.core.convert.TypeConverter;
20+
import jakarta.inject.Singleton;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
24+
25+
import java.net.MalformedURLException;
26+
import java.net.URL;
27+
import java.util.Optional;
28+
29+
/**
30+
* {@link TypeConverter} from {@link URL} to {@link AttributeValue} with {@link URL#URL(String)}
31+
* @author Sergio del Amo
32+
* @since 4.0.0
33+
*/
34+
@Singleton
35+
public class AttributeValueToURLTypeConverter implements TypeConverter<AttributeValue, URL> {
36+
private static final Logger LOG = LoggerFactory.getLogger(AttributeValueToURLTypeConverter.class);
37+
@Override
38+
public Optional<URL> convert(AttributeValue object, Class<URL> targetType, ConversionContext context) {
39+
if (object == null) {
40+
return Optional.empty();
41+
}
42+
String value = object.s();
43+
if (value == null) {
44+
return Optional.empty();
45+
}
46+
try {
47+
return Optional.of(new URL(value));
48+
} catch (MalformedURLException e) {
49+
LOG.warn("Malformed URL {}", value);
50+
return Optional.empty();
51+
}
52+
}
53+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017-2023 original 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 io.micronaut.aws.dynamodb.converters;
17+
18+
import io.micronaut.aws.dynamodb.utils.AttributeValueUtils;
19+
import io.micronaut.core.convert.ConversionContext;
20+
import io.micronaut.core.convert.TypeConverter;
21+
import jakarta.inject.Singleton;
22+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
23+
24+
import java.util.Optional;
25+
import java.net.URL;
26+
27+
/**
28+
* {@link TypeConverter} from {@link URL} to {@link AttributeValue} with {@link URL#toString()}.
29+
* @author Sergio del Amo
30+
* @since 4.0.0
31+
*/
32+
@Singleton
33+
public class URLToAttributeValueTypeConverter implements TypeConverter<URL, AttributeValue> {
34+
@Override
35+
public Optional<AttributeValue> convert(URL object, Class<AttributeValue> targetType, ConversionContext context) {
36+
return object != null ?
37+
Optional.of(AttributeValueUtils.s(object.toString())) :
38+
Optional.empty();
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.micronaut.aws.dynamodb.converters;
2+
3+
import io.micronaut.core.convert.ConversionService;
4+
import io.micronaut.core.type.Argument;
5+
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
6+
import org.junit.jupiter.api.Test;
7+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
8+
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
import java.util.Optional;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
@MicronautTest(startApplication = false)
19+
class URLToAttributeValueTypeConverterTest {
20+
@Test
21+
void localDateTimeToAttributeValueTypeConverterTest(ConversionService conversionService) throws MalformedURLException {
22+
assertNotNull(conversionService);
23+
URL value = null;
24+
assertFalse(conversionService.convert(value, Argument.of(AttributeValue.class)).isPresent());
25+
value = new URL("https://micronaut.io");
26+
Optional<AttributeValue> attributeValueOptional = conversionService.convert(value, Argument.of(AttributeValue.class));
27+
assertTrue(attributeValueOptional.isPresent());
28+
AttributeValue attributeValue = attributeValueOptional.get();
29+
assertEquals("https://micronaut.io" ,attributeValue.s());
30+
assertEquals(AttributeValue.Type.S, attributeValue.type());
31+
32+
Optional<URL> urlOptional = conversionService.convert(attributeValue, Argument.of(URL.class));
33+
assertTrue(urlOptional.isPresent());
34+
assertEquals(value, urlOptional.get());
35+
}
36+
}

0 commit comments

Comments
 (0)