Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Transformation is not applied for Java Target #3320

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/core/src/main/java/io/atlasmap/core/AtlasPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@
import io.atlasmap.v2.Field;
import io.atlasmap.v2.FieldGroup;

public class AtlasPath {
public class AtlasPath implements Cloneable {

public static final String PATH_SEPARATOR = "/";
public static final char PATH_SEPARATOR_CHAR = '/';
public static final String PATH_SEPARATOR_ESCAPED = "/";
public static final String PATH_ARRAY_START = "[";
public static final String PATH_ARRAY_END = "]";
public static final String PATH_ARRAY_SUFFIX = PATH_ARRAY_START + PATH_ARRAY_END;
public static final String PATH_LIST_START = "<";
public static final String PATH_LIST_END = ">";
public static final String PATH_LIST_SUFFIX = PATH_LIST_START + PATH_LIST_END;
public static final String PATH_MAP_START = "{";
public static final String PATH_MAP_END = "}";
public static final String PATH_MAP_SUFFIX = PATH_MAP_START + PATH_MAP_END;
public static final String PATH_ATTRIBUTE_PREFIX = "@";
public static final String PATH_NAMESPACE_SEPARATOR = ":";

Expand Down Expand Up @@ -170,6 +173,11 @@ public AtlasPath appendField(String fieldExpression) {
return this;
}

@Override
public AtlasPath clone() {
return new AtlasPath(this.toString());
}

public List<SegmentContext> getSegments(boolean includeRoot) {
if (includeRoot) {
return Collections.unmodifiableList(this.segmentContexts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private void processTargetFieldMapping(DefaultAtlasSession session, Mapping mapp
"Failed to write field value into target document: " + e.getMessage(),
AuditStatus.ERROR, null);
if (LOG.isDebugEnabled()) {
LOG.error(String.format("writeTargetValue()() failed for %s:%s",
LOG.error(String.format("writeTargetValue() failed for %s:%s",
targetField.getDocId(), targetField.getPath()), e);
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,24 +635,6 @@ private ActionProcessor findBestActionProcessor(List<ActionProcessor> processors
return processors.get(0);
}

private Object flattenList(Object value) {
if (value instanceof Iterable) {
List<Object> extractedValues = new ArrayList<>();
for (Object argument : (Iterable) value) {
if (argument instanceof Iterable) {
for (Object item : (Iterable) argument) {
extractedValues.add(item);
}
} else {
extractedValues.add(argument);
}
}
return extractedValues;
} else {
return value;
}
}

public Object buildAndProcessAction(ActionProcessor actionProcessor, Map<String, Object> actionParameters, List<Field> fields) {
List<Object> flattenedValues = new ArrayList<>();
for (Field item : fields) {
Expand Down Expand Up @@ -749,14 +731,23 @@ public Field processActions(AtlasInternalSession session, Field field) throws At
fieldGroup = AtlasModelFactory.createFieldGroupFrom(field, false);

// Make sure fieldGroup is of a collection type
if (fieldGroup.getCollectionType() == null || CollectionType.NONE.equals(fieldGroup.getCollectionType())) {
AtlasPath groupPath = new AtlasPath(fieldGroup.getPath());
if (!groupPath.hasCollection() || groupPath.isIndexedCollection()) {
fieldGroup.setCollectionType(CollectionType.ARRAY);
fieldGroup.setPath(fieldGroup.getPath() + "[]");
groupPath = new AtlasPath(groupPath.toString() + AtlasPath.PATH_ARRAY_SUFFIX);
fieldGroup.setPath(groupPath.toString());
} else if (fieldGroup.getCollectionType() == null || CollectionType.NONE.equals(fieldGroup.getCollectionType())) {
fieldGroup.setCollectionType(groupPath.getLastCollectionSegment().getCollectionType());
}

for (Object subValue : (List<?>) tmpSourceObject) {
List<?> tmpSourceList = (List<?>)tmpSourceObject;
for (int i=0; i<tmpSourceList.size(); i++) {
Object subValue = tmpSourceList.get(i);
Field subField = new SimpleField();
AtlasPath subPath = groupPath.clone();
subPath.setVacantCollectionIndex(i);
AtlasModelFactory.copyField(fieldGroup, subField, false);
subField.setPath(subPath.toString());
subField.setIndex(null);
subField.setValue(subValue);
subField.setFieldType(currentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,25 @@ public void testProcessPreviewSplit() throws AtlasException {
Mapping m = new Mapping();
Field source = new SimpleField();
source.setFieldType(FieldType.STRING);
source.setPath("/source");
source.setValue("one two three four");
source.setActions(new ArrayList<>());
Split action = new Split();
action.setDelimiter(" ");
source.getActions().add(action);
m.getInputField().add(source);
Field target1 = new SimpleField();
target1.setPath("/target1");
target1.setIndex(0);
target1.setFieldType(FieldType.STRING);
m.getOutputField().add(target1);
Field target2 = new SimpleField();
target2.setPath("/target2");
target2.setIndex(1);
target2.setFieldType(FieldType.STRING);
m.getOutputField().add(target2);
Field target3 = new SimpleField();
target3.setPath("/target3");
target3.setIndex(3);
target3.setFieldType(FieldType.STRING);
m.getOutputField().add(target3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.atlasmap.java.test.TargetContact;
import io.atlasmap.java.test.TargetFlatPrimitiveClass;
import io.atlasmap.java.test.TargetTestClass;
import io.atlasmap.java.test.TargetAddress;

public class JavaJavaCollectionTest extends AtlasMappingBaseTest {

Expand Down Expand Up @@ -305,6 +306,22 @@ public void testProcessCollectionFieldAction() throws Exception {
LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList(new String[] {"linkedList0", "linkedList1", "linkedList2"}));
source.setLinkedList(list);
ArrayList<SourceAddress> addressList = new ArrayList<>();
for (int i=0; i<3; i++) {
SourceAddress addr = new SourceAddress();
addr.setCity("city" + i);
addr.setState("state" + i);
addressList.add(addr);
}
source.setAddressList(addressList);
ArrayList<SourceContact> contactList = new ArrayList<>();
for (int i=0; i<3; i++) {
SourceContact ct = new SourceContact();
ct.setFirstName("first" + i);
ct.setLastName("last" + i);
contactList.add(ct);
}
source.setContactList(contactList);
AtlasSession session = context.createSession();
session.setSourceDocument("SourceCollectionsClass", source);
context.process(session);
Expand All @@ -314,5 +331,20 @@ public void testProcessCollectionFieldAction() throws Exception {
ArrayList<String> result = targetCollections.getArrayList();
assertEquals(1, result.size());
assertEquals("linkedList0#linkedList1#linkedList2", result.get(0));

List<TargetAddress> targetAddrList = targetCollections.getAddressList();
assertEquals(3, targetAddrList.size());
for (int i=0; i<3; i++) {
TargetAddress addr = targetAddrList.get(i);
assertEquals("city" + i, addr.getCity());
assertEquals("STATE" + i, addr.getState());
}
List<TargetContact> targetContactList = targetCollections.getContactList();
assertEquals(3, targetContactList.size());
for (int i=0; i<3; i++) {
TargetContact contact = targetContactList.get(i);
assertEquals("First" + i, contact.getFirstName());
assertEquals("last" + i, contact.getLastName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,101 @@
} ]
} ]
}
},
{
"jsonType" : "io.atlasmap.v2.Mapping",
"inputFieldGroup" : {
"jsonType" : "io.atlasmap.v2.FieldGroup",
"actions" : [ ],
"field" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "SourceCollectionsClass",
"path" : "/addressList<>/city",
"fieldType" : "STRING",
"name" : "city"
} ]
},
"inputField" : [ ],
"outputField" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "TargetCollectionsClass",
"path" : "/addressList<>/city",
"fieldType" : "STRING",
"name" : "city"
} ],
"id" : "mapping.219436"
}, {
"jsonType" : "io.atlasmap.v2.Mapping",
"inputFieldGroup" : {
"jsonType" : "io.atlasmap.v2.FieldGroup",
"actions" : [ ],
"field" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"actions" : [ {
"@type" : "Uppercase"
} ],
"docId" : "SourceCollectionsClass",
"path" : "/addressList<>/state",
"fieldType" : "STRING",
"name" : "state"
} ]
},
"inputField" : [ ],
"outputField" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "TargetCollectionsClass",
"path" : "/addressList<>/state",
"fieldType" : "STRING",
"name" : "state"
} ],
"id" : "mapping.370316"
}, {
"jsonType" : "io.atlasmap.v2.Mapping",
"inputFieldGroup" : {
"jsonType" : "io.atlasmap.v2.FieldGroup",
"actions" : [ ],
"field" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "SourceCollectionsClass",
"path" : "/contactList<>/firstName",
"fieldType" : "STRING",
"name" : "firstName"
} ]
},
"inputField" : [ ],
"outputField" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"actions" : [ {
"@type" : "Capitalize"
} ],
"docId" : "TargetCollectionsClass",
"path" : "/contactList<>/firstName",
"fieldType" : "STRING",
"name" : "firstName"
} ],
"id" : "mapping.217823"
}, {
"jsonType" : "io.atlasmap.v2.Mapping",
"inputFieldGroup" : {
"jsonType" : "io.atlasmap.v2.FieldGroup",
"actions" : [ ],
"field" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "SourceCollectionsClass",
"path" : "/contactList<>/lastName",
"fieldType" : "STRING",
"name" : "lastName"
} ]
},
"inputField" : [ ],
"outputField" : [ {
"jsonType" : "io.atlasmap.java.v2.JavaField",
"docId" : "TargetCollectionsClass",
"path" : "/contactList<>/lastName",
"fieldType" : "STRING",
"name" : "lastName"
} ],
"id" : "mapping.797532"
} ]
},
"name" : "JavaJavaFlatMapping"
Expand Down
30 changes: 1 addition & 29 deletions lib/model/src/main/java/io/atlasmap/v2/SimpleField.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,11 @@
*/
package io.atlasmap.v2;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.CLASS, property = "jsonType")
public class SimpleField extends Field implements Serializable {
public class SimpleField extends Field {

private static final long serialVersionUID = 1L;

protected String name;

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

}