Skip to content

Commit 4484682

Browse files
committed
Reuse getReferencedValue instead of additional switch
1 parent 5d3168e commit 4484682

File tree

2 files changed

+93
-39
lines changed

2 files changed

+93
-39
lines changed

metafix/src/main/java/org/metafacture/metafix/FixPath.java

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ private FixPath(final String[] path) {
6666
/*package-private*/ Value findIn(final Array array) {
6767

6868
final Value result;
69-
if (path.length > 0) {
69+
if (path.length == 0) {
70+
result = new Value(array);
71+
}
72+
else {
7073
final String currentSegment = path[0];
71-
final ReservedField reservedField = ReservedField.fromString(currentSegment);
7274
if (currentSegment.equals(ASTERISK)) {
7375
result = Value.newArray(resultArray -> array.forEach(v -> {
7476
final Value findInValue = findInValue(v, tail(path));
@@ -80,23 +82,10 @@ private FixPath(final String[] path) {
8082
}
8183
}));
8284
}
83-
else if (reservedField != null) {
84-
switch (reservedField) {
85-
case $first:
86-
result = findInValue(array.get(0), tail(path));
87-
break;
88-
case $last:
89-
result = findInValue(array.get(array.size() - 1), tail(path));
90-
break;
91-
default:
92-
result = null;
93-
break;
94-
}
95-
}
96-
else if (Value.isNumber(currentSegment)) {
97-
final int index = Integer.parseInt(currentSegment) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
98-
if (index >= 0 && index < array.size()) {
99-
result = findInValue(array.get(index), tail(path));
85+
else if (isReference(currentSegment)) {
86+
final Value referencedValue = getReferencedValue(array, currentSegment);
87+
if (referencedValue != null) {
88+
result = findInValue(referencedValue, tail(path));
10089
}
10190
else {
10291
result = null;
@@ -107,9 +96,6 @@ else if (Value.isNumber(currentSegment)) {
10796
result = Value.newArray(a -> array.forEach(v -> a.add(findInValue(v, path))));
10897
}
10998
}
110-
else {
111-
result = new Value(array);
112-
}
11399
return result;
114100

115101
}
@@ -354,24 +340,27 @@ private boolean isReference(final String field) {
354340
// TODO replace switch, extract to method on array?
355341
private Value getReferencedValue(final Array array, final String field) {
356342
Value referencedValue = null;
357-
final ReservedField reservedField = ReservedField.fromString(field);
358-
if (reservedField == null && Value.isNumber(field)) {
359-
return array.get(Integer.valueOf(field) - 1);
343+
if (Value.isNumber(field)) {
344+
final int index = Integer.valueOf(field) - 1;
345+
return 0 <= index && index < array.size() ? array.get(index) : null;
360346
}
361-
switch (reservedField) {
362-
case $first:
363-
referencedValue = array.get(0);
364-
break;
365-
case $last:
366-
referencedValue = array.get(array.size() - 1);
367-
break;
368-
case $append:
369-
referencedValue = Value.newHash(); // TODO: append non-hash?
370-
array.add(referencedValue);
371-
referencedValue.updatePathAppend(String.valueOf(array.size()), "");
372-
break;
373-
default:
374-
break;
347+
final ReservedField reservedField = ReservedField.fromString(field);
348+
if (reservedField != null) {
349+
switch (reservedField) {
350+
case $first:
351+
referencedValue = array.get(0);
352+
break;
353+
case $last:
354+
referencedValue = array.get(array.size() - 1);
355+
break;
356+
case $append:
357+
referencedValue = Value.newHash(); // TODO: append non-hash?
358+
array.add(referencedValue);
359+
referencedValue.updatePathAppend(String.valueOf(array.size()), "");
360+
break;
361+
default:
362+
break;
363+
}
375364
}
376365
return referencedValue;
377366
}

metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,4 +1069,69 @@ public void shouldIncludeLocationAndTextInProcessExceptionInBody() {
10691069
);
10701070
}
10711071

1072+
@Test
1073+
public void ifOnNonExistingIndexInArray() {
1074+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1075+
"if exists('animals[].2')",
1076+
" copy_field('animals[].2', 'animals2')",
1077+
"end"
1078+
),
1079+
i -> {
1080+
i.startRecord("1");
1081+
i.startEntity("animals[]");
1082+
i.literal("1", "dog");
1083+
i.literal("2", "elefant");
1084+
i.endEntity();
1085+
i.endRecord();
1086+
i.startRecord("2");
1087+
i.startEntity("animals[]");
1088+
i.literal("1", "dog");
1089+
i.endEntity();
1090+
i.endRecord();
1091+
},
1092+
o -> {
1093+
o.get().startRecord("1");
1094+
o.get().startEntity("animals[]");
1095+
o.get().literal("1", "dog");
1096+
o.get().literal("2", "elefant");
1097+
o.get().endEntity();
1098+
o.get().literal("animals2", "elefant");
1099+
o.get().endRecord();
1100+
o.get().startRecord("2");
1101+
o.get().startEntity("animals[]");
1102+
o.get().literal("1", "dog");
1103+
o.get().endEntity();
1104+
o.get().endRecord();
1105+
}
1106+
);
1107+
}
1108+
1109+
@Test
1110+
public void ifOnNonExistingIndexInRepeatedField() {
1111+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1112+
"if exists('animal.2')",
1113+
" copy_field('animal.2', 'animal2')",
1114+
"end"
1115+
),
1116+
i -> {
1117+
i.startRecord("1");
1118+
i.literal("animal", "dog");
1119+
i.literal("animal", "elefant");
1120+
i.endRecord();
1121+
i.startRecord("2");
1122+
i.literal("animal", "dog");
1123+
i.endRecord();
1124+
},
1125+
o -> {
1126+
o.get().startRecord("1");
1127+
o.get().literal("animal", "dog");
1128+
o.get().literal("animal", "elefant");
1129+
o.get().literal("animal2", "elefant");
1130+
o.get().endRecord();
1131+
o.get().startRecord("2");
1132+
o.get().literal("animal", "dog");
1133+
o.get().endRecord();
1134+
}
1135+
);
1136+
}
10721137
}

0 commit comments

Comments
 (0)