io.sphere.sdk.products.attributes.Attribute Maven / Gradle / Ivy
package io.sphere.sdk.products.attributes;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.models.Reference;
@JsonDeserialize(as = AttributeImpl.class)
public interface Attribute {
String getName();
T getValue(final AttributeAccess access);
static Attribute of(final String name, final JsonNode jsonNode) {
return new AttributeImpl(name, jsonNode);
}
static Attribute of(final String name, final AttributeAccess access, final T value) {
return of(access.ofName(name), value);
}
static Attribute of(final NamedAttributeAccess namedAttributeAccess, final T value) {
final String name = namedAttributeAccess.getName();
//here is not the attributeMapper used to keep LocalizedEnum values which
//are transformed to just the key so the attribute could not be read anymore
final JsonNode jsonNode = SphereJsonUtils.toJsonNode(value);
if (value instanceof Reference && jsonNode instanceof ObjectNode) {
final Reference reference = (Reference) value;
if (reference.getObj() != null) {
((ObjectNode) jsonNode).replace("obj", SphereJsonUtils.toJsonNode(reference.getObj()));
}
}
return of(name, jsonNode);
}
}