-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DynamoDb Converters v2 Improvements (#263)
* DynamoDb Converters v2 Improvements - added `@ConvertedJson` to mirror `DynamoDBTypeConvertedJson` functionality - always present `EmptySafeStringSetConverter` to allow having sets initialized * DynamoDb Converters v2 Improvements - added `@ConvertedJson` to mirror `DynamoDBTypeConvertedJson` functionality - always present `EmptySafeStringSetConverter` to allow having sets initialized
- Loading branch information
Showing
17 changed files
with
464 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...c/main/java/com/agorapulse/micronaut/amazon/awssdk/dynamodb/annotation/ConvertedJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2018-2024 Agorapulse. | ||
* | ||
* 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 com.agorapulse.micronaut.amazon.awssdk.dynamodb.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Specifies that the property is persisted as JSON string. Requires micronaut-jackson-databind to be on the classpath. | ||
* | ||
*/ | ||
@Inherited | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD}) | ||
public @interface ConvertedJson { | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
.../agorapulse/micronaut/amazon/awssdk/dynamodb/convert/ConvertedJsonAttributeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2018-2024 Agorapulse. | ||
* | ||
* 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 com.agorapulse.micronaut.amazon.awssdk.dynamodb.convert; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import io.micronaut.jackson.ObjectMapperFactory; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Converter which converts objects to JSON strings. | ||
* @param <T> the type of the object | ||
*/ | ||
public class ConvertedJsonAttributeConverter<T> implements AttributeConverter<T> { | ||
|
||
private static final ObjectWriter OBJECT_WRITER; | ||
private static final ObjectReader OBJECT_READER; | ||
|
||
static { | ||
ObjectMapper mapper = new ObjectMapperFactory().objectMapper(null, null); | ||
OBJECT_READER = mapper.reader(); | ||
OBJECT_WRITER = mapper.writer(); | ||
} | ||
|
||
private final Class<T> type; | ||
|
||
public ConvertedJsonAttributeConverter(Class<T> type) { | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public AttributeValue transformFrom(T input) { | ||
try { | ||
return AttributeValue.fromS(OBJECT_WRITER.writeValueAsString(input)); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalArgumentException("Cannot write value as JSON: " + input, e); | ||
} | ||
} | ||
|
||
@Override | ||
public T transformTo(AttributeValue input) { | ||
try { | ||
return OBJECT_READER.readValue(input.s(), type); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Cannot read value: " + input.s(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public EnhancedType<T> type() { | ||
return EnhancedType.of(type); | ||
} | ||
|
||
@Override | ||
public AttributeValueType attributeValueType() { | ||
return AttributeValueType.S; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../com/agorapulse/micronaut/amazon/awssdk/dynamodb/convert/EmptySafeStringSetConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2018-2024 Agorapulse. | ||
* | ||
* 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 com.agorapulse.micronaut.amazon.awssdk.dynamodb.convert; | ||
|
||
import io.micronaut.core.util.CollectionUtils; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
import software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Set string datatype in dynamo does not allow empty values. | ||
* Without this custom converter if an empty set is present in entity an error will occur : 'dynamodb an string set may not be empty' | ||
* @see <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html">...</a> | ||
*/ | ||
public class EmptySafeStringSetConverter implements AttributeConverter<Set<String>> { | ||
@Override | ||
public AttributeValue transformFrom(Set<String> set) { | ||
return CollectionUtils.isEmpty(set) | ||
? AttributeValues.nullAttributeValue() | ||
: AttributeValue.fromSs(set.stream().toList()); | ||
} | ||
|
||
@Override | ||
public Set<String> transformTo(AttributeValue rawValue) { | ||
return new HashSet<>(rawValue.ss()); | ||
} | ||
|
||
@Override | ||
public EnhancedType<Set<String>> type() { | ||
return EnhancedType.setOf(String.class); | ||
} | ||
|
||
@Override | ||
public AttributeValueType attributeValueType() { | ||
return AttributeValueType.SS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...sdk-dynamodb/src/test/groovy/com/agorapulse/micronaut/amazon/awssdk/dynamodb/Options.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2018-2024 Agorapulse. | ||
* | ||
* 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 com.agorapulse.micronaut.amazon.awssdk.dynamodb; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
|
||
import java.util.Objects; | ||
|
||
@Introspected | ||
public class Options { | ||
|
||
private String one; | ||
private String two; | ||
|
||
public String getTwo() { | ||
return two; | ||
} | ||
|
||
public void setTwo(String two) { | ||
this.two = two; | ||
} | ||
|
||
public String getOne() { | ||
return one; | ||
} | ||
|
||
public void setOne(String one) { | ||
this.one = one; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
Options options = (Options) o; | ||
|
||
return Objects.equals(one, options.one) && Objects.equals(two, options.two); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(one, two); | ||
} | ||
|
||
} |
Oops, something went wrong.