简体   繁体   中英

DynamoDB mapping List of Enum

Mapping an enum class in to DynamoDB object is really simple by using Custom Marshall. But how to map a List of Enum?

Enum class

public enum Transport {
SMS,EMAIL,ALL;
}

DynamoDB mapper

public class Campaign{
   private List<Transport> transport;

   @DynamoDBAttribute(attributeName = "transport")
   public List<Transport> getTransport() {
       return transport;
   }

   public void setTransport(List<Transport> transport) {
      this.transport = transport;
   }
}

DynamoDBMarshaller is deprecated. Use DynamoDBTypeConverter instead.

Example:

Enum class

public static enum ValidationFailure {
    FRAUD, GENERAL_ERROR
}

DynamoDBTable class

 @DynamoDBTable(tableName = "receipt")
public class Receipt {

    private Long id;
    private List<ValidationFailure> validation;

    @DynamoDBHashKey(attributeName = "id")
    public Long getId() {
        return id;
    }

    @DynamoDBTypeConverted(converter = ValidationConverter.class)
    public List<ValidationFailure> getValidation() {
        return validation;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setValidation(List<ValidationFailure> validation) {
        this.validation = validation;
    }
}

Convertor:

public class ValidationConverter implements DynamoDBTypeConverter<List<String>, List<ValidationFailure>> {

@Override
public List<String> convert(List<ValidationFailure> object) {
    List<String> result = new ArrayList<String>();
    if (object != null) {
        object.stream().forEach(e -> result.add(e.name()));
    }
    return result;
}

@Override
public List<ValidationFailure> unconvert(List<String> object) {
    List<ValidationFailure> result = new ArrayList<ValidationFailure>();
    if (object != null) {
        object.stream().forEach(e -> result.add(ValidationFailure.valueOf(e)));
    }
    return result;
}
}

It's working for me, I have used the Set

@DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.SS)
var roles: MutableSet<Employee.Role>? = null

I think the same approach would work for List with DynamoDBAttributeType.L

I found the answer myself. I create a custom marshall like below.

public class TransportMarshaller implements DynamoDBMarshaller<List<Transport>> {

@Override
public String marshall(List<Transport> transports) {
    List<String>transportMap=new ArrayList<>();
    for(Transport transport:transports){
        transportMap.add(transport.name());
    }
    return transportMap.toString().replaceAll("\\[|\\]", "");//Save as comma separate value for the purpose of easiness to unmarshall
}

@Override
public List<Transport> unmarshall(Class<List<Transport>> aClass, String s) {
    List<String>map= Arrays.asList(s.split("\\s*,\\s*")); //split from comma and parse to List
    List<Transport>transports=new ArrayList<>();
    for (String st:map){
        transports.add(Transport.valueOf(st));
    }
    return transports;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM