Skip to content

Commit

Permalink
Add generic support for set (#232)
Browse files Browse the repository at this point in the history
* Add generic support for set

* Fix codenarc issues
  • Loading branch information
ludovic-faure-agora authored May 27, 2024
1 parent 313b440 commit 834b5c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -299,6 +300,9 @@ private static <T> EnhancedType<T> convertTypeToEnhancedType(
if (List.class.equals(type.getType())) {
EnhancedType<?> enhancedType = convertTypeToEnhancedType(type.getTypeParameters()[0], metaTableSchemaCache, attributeConfiguration, beanContext);
return (EnhancedType<T>) EnhancedType.listOf(enhancedType);
} else if (Set.class.equals(type.getType())) {
EnhancedType<?> enhancedType = convertTypeToEnhancedType(type.getTypeParameters()[0], metaTableSchemaCache, attributeConfiguration, beanContext);
return (EnhancedType<T>) EnhancedType.setOf(enhancedType);
} else if (Map.class.equals(type.getType())) {
EnhancedType<?> keyType = convertTypeToEnhancedType(type.getTypeVariable("K").orElseThrow(() -> new IllegalArgumentException("Missing key type")), metaTableSchemaCache, attributeConfiguration, beanContext);
EnhancedType<?> valueType = convertTypeToEnhancedType(type.getTypeVariable("V").orElseThrow(() -> new IllegalArgumentException("Missing value type")), metaTableSchemaCache, attributeConfiguration, beanContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

@Introspected
@DynamoDbBean
Expand All @@ -33,8 +34,8 @@ public class Person {
Integer age;
Map<String, Address> addresses;
List<PhoneNumber> phoneNumbers;

List<String> hobbies;
Set<String> favoriteColors;

@DynamoDbPartitionKey
public Integer getId() {
Expand Down Expand Up @@ -93,6 +94,14 @@ public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}

public Set<String> getFavoriteColors() {
return favoriteColors;
}

public void setFavoriteColors(Set<String> favoriteColors) {
this.favoriteColors = favoriteColors;
}

// CHECKSTYLE:OFF
@Override
public String toString() {
Expand All @@ -104,6 +113,7 @@ public String toString() {
", addresses=" + addresses +
", phoneNumbers=" + phoneNumbers +
", hobbies=" + hobbies +
", favoriteColors=" + favoriteColors +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package com.agorapulse.micronaut.amazon.awssdk.dynamodb.schema

import com.agorapulse.micronaut.amazon.awssdk.dynamodb.Address
import com.agorapulse.micronaut.amazon.awssdk.dynamodb.DynamoDBEntity
import com.agorapulse.micronaut.amazon.awssdk.dynamodb.DynamoDBEntityMapProperty
import com.agorapulse.micronaut.amazon.awssdk.dynamodb.DynamoDBEntityNoRange
import com.agorapulse.micronaut.amazon.awssdk.dynamodb.Person
import com.agorapulse.micronaut.amazon.awssdk.dynamodb.PhoneNumber
import io.micronaut.context.BeanContext
import software.amazon.awssdk.enhanced.dynamodb.internal.mapper.MetaTableSchemaCache
import spock.lang.IgnoreIf
Expand All @@ -45,7 +47,26 @@ class BeanIntrospectionTableSchemaSpec extends Specification {
when:
BeanIntrospectionTableSchema<Person> schema = BeanIntrospectionTableSchema.create(Person, context, cache)
then:
schema.attributeNames().size() == 7
schema.attributeNames().size() == 8
when:
schema.itemToMap(new Person(
id: 1,
firstName: 'John',
lastName: 'Doe',
age: 42,
addresses: [
main: new Address(
street: 'Main Street',
city: 'Springfield',
zipCode: '12345'
)
],
phoneNumbers: [new PhoneNumber(type: 'home', number: '123456789')],
hobbies: ['reading', 'coding'],
favoriteColors: ['red', 'green'],
), true)
then:
noExceptionThrown()
}

void 'read table schema for groovy class'() {
Expand Down

0 comments on commit 834b5c1

Please sign in to comment.