Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Add split_field() Fix function. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Jan 11, 2022
1 parent 0155cee commit 1900427
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -365,6 +366,21 @@ public void apply(final Metafix metafix, final Record record, final List<String>
});
}
},
split_field {
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transformField(params.get(0), v -> {
final String splitChar = params.size() > 1 ? params.get(1) : "\\s+";
final Pattern splitPattern = Pattern.compile(splitChar);

final UnaryOperator<Value> splitOperator = s ->
newArray(Arrays.stream(splitPattern.split(s.asString())).map(Value::new));

return v.isString() ? splitOperator.apply(v) : v.isArray() ?
newArray(v.asArray().stream().map(splitOperator)) : v.isHash() ?
Value.newHash(h -> v.asHash().forEach((f, s) -> h.put(f, splitOperator.apply(s)))) : null;
});
}
},
substring {
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transformFields(params, s -> s.substring(getInteger(params, 1), getInteger(params, 2) - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,88 @@ public void shouldSortFieldAndRemoveDuplicates() {
);
}

@Test
public void shouldSplitStringField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"split_field(date, '-')"
),
i -> {
i.startRecord("1");
i.literal("date", "1918-17-16");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("date");
o.get().literal("1", "1918");
o.get().literal("2", "17");
o.get().literal("3", "16");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldSplitArrayField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"split_field(date, '-')"
),
i -> {
i.startRecord("1");
i.literal("date", "1918-17-16");
i.literal("date", "2021-22-23");
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("date");
o.get().startEntity("1");
o.get().literal("1", "1918");
o.get().literal("2", "17");
o.get().literal("3", "16");
o.get().endEntity();
o.get().startEntity("2");
o.get().literal("1", "2021");
o.get().literal("2", "22");
o.get().literal("3", "23");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldSplitHashField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"split_field(date, '-')"
),
i -> {
i.startRecord("1");
i.startEntity("date");
i.literal("a", "1918-17-16");
i.literal("b", "2021-22-23");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("date");
o.get().startEntity("a");
o.get().literal("1", "1918");
o.get().literal("2", "17");
o.get().literal("3", "16");
o.get().endEntity();
o.get().startEntity("b");
o.get().literal("1", "2021");
o.get().literal("2", "22");
o.get().literal("3", "23");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

private void assertThrows(final Class<?> expectedClass, final String expectedMessage, final Executable executable) {
final Throwable exception = Assertions.assertThrows(MetafactureException.class, executable).getCause();
Assertions.assertSame(expectedClass, exception.getClass());
Expand Down

0 comments on commit 1900427

Please sign in to comment.