commercetools / commercetools-sdk-java-v2

The e-commerce SDK from commercetools for Java.
https://commercetools.github.io/commercetools-sdk-java-v2/javadoc/index.html
Apache License 2.0
34 stars 15 forks source link

Suggestion: Easier handling for nullable attributes (and custom fields?) #258

Closed pintomau closed 2 years ago

pintomau commented 2 years ago

Hi!

As discussed in #233 , it was suggested to use productVariant.withProductVariant(AttributeAccessor::asMap) and then a variant of attributes.get("attrName").withAttribute(AttributeAccessor::asType).

However, this makes it a bit hard to handle nullable attributes, because we'll need constant null checking or provide defaults, which makes asMap a bit limited.

Suggestion: additionally provide an abstraction (wrapping the map?) that more easily handles nullable parameters.

It should be trivial to create one on user code, but it feels like this is something the SDK should make it easier to handle? At least this feels a bit harder to work with than SDK1.

To exemplify what I'm thinking, a possible solution that eases such cases:

interface NiceAdapter {

  @Nullable
  Attribute getAttribute(String attrName);

  // doWith would only be applied if attributes[attrName] != null, otherwise return null
  // AttributeAccessor methods could be reused
  @Nullable
  default <T> T withAttributeIfPresent(String attrName, Function<Attribute, T> doWith) {
     Attribute attr = getAttribute(attrName);
     if (null == attr) return null;

    return doWith.apply(attr);
  }
}

and usage:


var attributes = productVariant.withProductVariant(AttributeAccessor::asNiceAdapter);

var attribute = attributes.withAttributeIfPresent("nullableAttribute", attr -> AttributeAccessor::asType);

// handle nullable attribute or not, if I know attr is never null

Thanks

jenschude commented 2 years ago

Thanks for the suggestion.

Added an AttributesAccessor and refactored the CustomFieldsAccessor to be more null safe

https://github.com/commercetools/commercetools-sdk-java-v2/blob/main/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/product/AttributesAccessor.java

https://github.com/commercetools/commercetools-sdk-java-v2/blob/main/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/type/CustomFieldsAccessor.java

Usage can be seen here

https://github.com/commercetools/commercetools-sdk-java-v2/blob/cd1129dd4df335789617198a50f3965b5fdad969/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/AttributesTest.java#L323-L332

https://github.com/commercetools/commercetools-sdk-java-v2/blob/cd1129dd4df335789617198a50f3965b5fdad969/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/CustomFieldsTest.java#L156-L166

pintomau commented 2 years ago

Thanks. I'll take a look asap.