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

Working with nested Product Types #289

Closed Akii closed 2 years ago

Akii commented 2 years ago

What is the best way to deal with nested Product Types? I've been following this tutorial here: https://docs.commercetools.com/tutorials/nested-types

The best way of accessing the nested type I came up with has been this attempt:

val taste = productVariant.withProductVariant(AttributesAccessor::of).asString("taste")
val nutrients = productVariant.withProductVariant(AttributesAccessor::of).get("nutrients")
val nutrientsValues = nutrients!!.value as List<List<AttributeImpl>>

println("The taste is $taste for these nutrients:")
nutrientsValues.forEach {
    println("   Code: ${it[1].value} and Quality: ${it[0].value}")
}

Is there a better way of accessing the nested type?

jenschude commented 2 years ago

Hi,

please see https://github.com/commercetools/commercetools-sdk-java-v2/pull/295/files

The change will be included in the next release, but could also be used on your side by copying the necessary methods

Akii commented 2 years ago

Hey, that looks awesome. Thanks for adding this!

jenschude commented 2 years ago

Just released 8.6.0 which includes the additions to the AttributesAccessor