Dynamodb attribute converter provider for enhanced type extending Hashmap
Asked Answered
A

1

9

I have a type which is extending HashMap<String, String>. As per the documentation here, it is possible to add a custom converter for the type. But it seems not working. The contents of the hashMap doesn't get converted, output looks like below;

"summary": {
  "en": null
},

Any idea how to convert Label and its fields along with it's hashmap's contents?

Parent

@DynamoDbBean(converterProviders = {
    CustomAttributeConverterProvider.class,
    DefaultAttributeConverterProvider.class})
public class Summary extends BaseEntry  {

  private @Valid Label summary = null;

}

Child

@DynamoDbBean(converterProviders = {
    CustomAttributeConverterProvider.class,
    DefaultAttributeConverterProvider.class})
public class Label extends HashMap<String, String>  {
  private @Valid String en = null;
}

HashMapAttributeConverter

public class HashMapAttributeConverter implements AttributeConverter<Map<String, String>> {
  private static AttributeConverter<Map<String, String>> mapConverter;

  /** Default constructor. */
  public HashMapAttributeConverter() {
    mapConverter =
        MapAttributeConverter.builder(EnhancedType.mapOf(String.class, String.class))
            .mapConstructor(HashMap::new)
            .keyConverter(StringStringConverter.create())
            .valueConverter(StringAttributeConverter.create())
            .build();
  }

  @Override
  public AttributeValue transformFrom(Map<String, String> input) {
    return mapConverter.transformFrom(input);
  }

  @Override
  public Map<String, String> transformTo(AttributeValue input) {
    return mapConverter.transformTo(input);
  }

  @Override
  public EnhancedType<Map<String, String>> type() {
    return mapConverter.type();
  }

  @Override
  public AttributeValueType attributeValueType() {
    return mapConverter.attributeValueType();
  }
}

CustomAttributeConverterProvider

public class CustomAttributeConverterProvider implements AttributeConverterProvider {

  private final List<AttributeConverter<?>> customConverters =
      Arrays.asList(new HashMapAttributeConverter());

  private final Map<EnhancedType<?>, AttributeConverter<?>> customConvertersMap;
  private final AttributeConverterProvider defaultProvider =
      DefaultAttributeConverterProvider.create();

  public CustomAttributeConverterProvider() {
    customConvertersMap =
        customConverters.stream().collect(Collectors.toMap(AttributeConverter::type, c -> c));
  }

  @Override
  public <T> AttributeConverter<T> converterFor(EnhancedType<T> enhancedType) {
    return (AttributeConverter<T>)
        customConvertersMap.computeIfAbsent(enhancedType, defaultProvider::converterFor);
  }
}
Alaynaalayne answered 15/1, 2021 at 20:0 Comment(0)
T
1
@Override
public <T> AttributeConverter<T> converterFor(EnhancedType<T> type) {
    // in this method you have to return only your converter  based on type
    // otherwise null should be returned. It will fix your issue. 
}
Talavera answered 9/5, 2022 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.