Skip to content

Commit

Permalink
hopefully a little cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrissman authored and Marek Paterczyk committed Jun 7, 2017
1 parent 2556dde commit a545d98
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,16 @@ private void translate(ArrayField field, JsonNodeCursor cursor, Object target) {
do {
items.add(fromJson(arrayElement.getType(), cursor.getCurrentNode()));
} while (cursor.nextSibling());
translateSimpleArray(field, items, target);
translate(field, items, target);
}
else if(arrayElement instanceof ObjectArrayElement){
List<JsonNode> items = new ArrayList<>();
List<Object> items = new ArrayList<>();
do {
items.add(cursor.getCurrentNode());
Object item = createInstanceFor(arrayElement.getFullPath());
translate((ObjectArrayElement) arrayElement, cursor.getCurrentNode(), item);
items.add(item);
} while (cursor.nextSibling());
translateObjectArray(field, items, target);
translate(field, items, target);
}
else{
throw Error.get(CrudConstants.ERR_UNSUPPORTED_FEATURE + arrayElement.getClass().getName(), field.getFullPath().toString());
Expand All @@ -163,20 +165,16 @@ protected void translate(ObjectField field, JsonNodeCursor cursor, Object target
cursor.parent();
}

protected void translate(final ObjectArrayElement objectArrayElement, final List<JsonNode> items, List<Object> target) {
items.forEach(item -> {
JsonNodeCursor cursor = new JsonNodeCursor(objectArrayElement.getFullPath(), item);
protected void translate(final ObjectArrayElement objectArrayElement, JsonNode node, Object target) {
JsonNodeCursor cursor = new JsonNodeCursor(objectArrayElement.getFullPath(), node);

if(!cursor.firstChild()){
throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString());
}
if(!cursor.firstChild()){
throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, cursor.getCurrentPath().toString());
}

Object o = createInstanceFor(objectArrayElement.getFullPath());
do {
translate(cursor, o);
} while (cursor.nextSibling());
target.add(o);
});
do {
translate(cursor, target);
} while (cursor.nextSibling());
}

protected void translate(ReferenceField field, JsonNode node, Object target){
Expand All @@ -185,9 +183,7 @@ protected void translate(ReferenceField field, JsonNode node, Object target){

protected abstract void translate(SimpleField field, JsonNode node, Object target);

protected abstract void translateSimpleArray(ArrayField field, List<Object> items, Object target);

protected abstract void translateObjectArray(ArrayField field, List<JsonNode> items, Object target);
protected abstract void translate(ArrayField field, List<Object> items, Object target);

protected abstract Object createInstanceFor(Path path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

Expand Down Expand Up @@ -37,7 +39,9 @@ protected void translate(SimpleField field, JsonNode node, Object target) {
if (target instanceof SingleFieldObject) {
((SingleFieldObject<String>) target).value = fromJson(field.getType(), node).toString();
}
throw new RuntimeException("Unexpected");
else {
throw new RuntimeException("Unexpected");
}
}

};
Expand All @@ -58,14 +62,16 @@ public void testTranslateSimpleArrayField() throws Exception {

@SuppressWarnings("unchecked")
@Override
protected void translateSimpleArray(ArrayField field, List<Object> items, Object target) {
protected void translate(ArrayField field, List<Object> items, Object target) {
if (target instanceof SingleFieldObject) {
List<String> translatedItems = new ArrayList<>();
items.forEach(item -> translatedItems.add(item.toString()));

((SingleFieldObject<String[]>) target).value = translatedItems.toArray(new String[0]);
}
throw new RuntimeException("Unexpected");
else {
throw new RuntimeException("Unexpected");
}
}

};
Expand Down Expand Up @@ -99,7 +105,9 @@ protected void translate(SimpleField field, JsonNode node, Object target) {

thing.value.value = fromJson(field.getType(), node).toString();
}
throw new RuntimeException("Unexpected");
else {
throw new RuntimeException("Unexpected");
}
}

};
Expand All @@ -116,34 +124,32 @@ protected void translate(SimpleField field, JsonNode node, Object target) {
public void testTranslateObjectArrayField() throws Exception{
ObjectArrayElement oae = new ObjectArrayElement();
oae.getFields().addNew(new SimpleField("uid", StringType.TYPE));
oae.getFields().addNew(new SimpleField("name", StringType.TYPE));
EntityMetadata md = fakeEntityMetadata("fakeMetadata",
new ArrayField("objArr", oae));

FakeTranslatorFromJson<SingleFieldObject<List<SingleFieldObject<String>>>> translator = new FakeTranslatorFromJson<SingleFieldObject<List<SingleFieldObject<String>>>>(md) {
FakeTranslatorFromJson<SingleFieldObject<List<Map<String, String>>>> translator = new FakeTranslatorFromJson<SingleFieldObject<List<Map<String, String>>>>(md) {

@SuppressWarnings("unchecked")
@Override
protected void translateObjectArray(ArrayField field, List<JsonNode> items, Object target) {
if (target instanceof SingleFieldObject) {
@SuppressWarnings("unchecked")
SingleFieldObject<List<Object>> thing = (SingleFieldObject<List<Object>>) target;

if (thing.value == null) {
thing.value = new ArrayList<>();
}

translate((ObjectArrayElement) field.getElement(), items, thing.value);
}
else {
throw new RuntimeException("Unexpected");
protected void translate(SimpleField field, JsonNode node, Object target) {
switch(field.getFullPath().toString()) {
case "objArr.*.uid":
((Map<String, String>) target).put("uid", fromJson(field.getType(), node).toString());
break;
case "objArr.*.name":
((Map<String, String>) target).put("name", fromJson(field.getType(), node).toString());
break;
default:
throw new RuntimeException("Unexpected");
}
}

@SuppressWarnings("unchecked")
@Override
protected void translate(SimpleField field, JsonNode node, Object target) {
Path p = field.getFullPath();
if (p.toString().startsWith("objArr.*.uid")) {
((SingleFieldObject<String>) target).value = fromJson(field.getType(), node).toString();
protected void translate(ArrayField field, List<Object> items, Object target) {
if (field.getFullPath().toString().equals("objArr")) {
((SingleFieldObject<List<?>>) target).value = items;
}
else {
throw new RuntimeException("Unexpected");
Expand All @@ -152,20 +158,27 @@ protected void translate(SimpleField field, JsonNode node, Object target) {

@Override
protected Object createInstanceFor(Path path) {
if (path.toString().equals("objArr.*")) {
return new SingleFieldObject<String>();
switch (path.toString()) {
case "objArr.*":
return new HashMap<>();
default:
throw new RuntimeException("Unexpected");
}
throw new RuntimeException("Unexpected");

}

};

SingleFieldObject<List<SingleFieldObject<String>>> response = new SingleFieldObject<>();
translator.translate(new JsonDoc(json("{\"objArr\":[{\"uid\":\"fake value1\"},{\"uid\":\"fake value2\"}]}")), response);
SingleFieldObject<List<Map<String, String>>> response = new SingleFieldObject<>();
translator.translate(new JsonDoc(json("{\"objArr\":[{\"uid\":\"fake value1\",\"name\":\"name1\"},{\"uid\":\"fake value2\",\"name\":\"name2\"}]}")), response);

assertNotNull(response);
assertNotNull(response.value);
assertEquals(2, response.value.size());
assertEquals("fake value1", response.value.get(0).get("uid"));
assertEquals("name1", response.value.get(0).get("name"));
assertEquals("fake value2", response.value.get(1).get("uid"));
assertEquals("name2", response.value.get(1).get("name"));
}

protected EntityMetadata fakeEntityMetadata(String name, Field... fields) {
Expand Down Expand Up @@ -196,12 +209,7 @@ protected void translate(SimpleField field, JsonNode node, Object target) {
}

@Override
protected void translateSimpleArray(ArrayField field, List<Object> items, Object target) {
throw new RuntimeException("Method was not expected to be called");
}

@Override
protected void translateObjectArray(ArrayField field, List<JsonNode> items, Object target) {
protected void translate(ArrayField field, List<Object> items, Object target) {
throw new RuntimeException("Method was not expected to be called");
}

Expand Down

0 comments on commit a545d98

Please sign in to comment.