Skip to content

Commit

Permalink
Merge pull request #391 from rapiddweller/5-enrich-entity-with-parts-…
Browse files Browse the repository at this point in the history
…objects-list-subentities-when-iterating-over-json-file

Enrich entity with part while iterating json file
  • Loading branch information
ake2l committed Apr 25, 2023
2 parents 01ca5bc + d112a5e commit 0aa7a06
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.rapiddweller.benerator.factory.BeneratorExceptionFactory;
import com.rapiddweller.benerator.wrapper.ProductWrapper;
import com.rapiddweller.common.ArrayUtil;
import com.rapiddweller.model.data.ComplexTypeDescriptor;
import com.rapiddweller.model.data.Entity;

import java.util.Collection;
Expand Down Expand Up @@ -58,9 +59,12 @@ public boolean execute(BeneratorContext context) {
ProductWrapper<?> wrapper = context.getCurrentProduct();
if (wrapper != null) {
Object part = ((Entity) wrapper.unwrap()).getComponent(partName);
if (part != null) {
applyToPart(part, context);
// Init part and add into currentProduct
if (part == null) {
part = new Entity((ComplexTypeDescriptor) null, null);
((Entity)context.getCurrentProduct().unwrap()).setComponent(partName, part);
}
applyToPart(part, context);
}
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/rapiddweller/model/data/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public Entity(String name, DescriptorProvider descriptorProvider, Object... comp
public Entity(ComplexTypeDescriptor descriptor, Object... componentKeyValuePairs) {
this.descriptor = descriptor;
this.components = BeneratorFactory.getInstance().createComponentMap();
for (int i = 0; i < componentKeyValuePairs.length; i += 2) {
setComponent((String) componentKeyValuePairs[i], componentKeyValuePairs[i + 1]);
if (componentKeyValuePairs != null) {
for (int i = 0; i < componentKeyValuePairs.length; i += 2) {
setComponent((String) componentKeyValuePairs[i], componentKeyValuePairs[i + 1]);
}
}
}

Expand Down

0 comments on commit 0aa7a06

Please sign in to comment.