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

Commit

Permalink
Add flatten() Fix function. (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Jul 12, 2022
1 parent bec6763 commit e044f86
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ Only keeps field values that match the regular expression pattern.
filter("<sourceField>", "<regexp>")
```

##### `flatten`

Flattens a nested array field.

```perl
flatten("<sourceField>")
```

##### `index`

Returns the index position of a substring in a field and replaces the field value with this number.
Expand Down
8 changes: 8 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,14 @@ public void apply(final Metafix metafix, final Record record, final List<String>
);
}
},
flatten {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(newArray(flatten(a.stream()))))
);
}
},
index {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,11 @@ default Stream<Value> unique(final Stream<Value> stream) {
return stream.filter(set::add);
}

default Stream<Value> flatten(final Stream<Value> stream) {
return stream.flatMap(v -> v.extractType((m, c) -> m
.ifArray(a -> c.accept(flatten(a.stream())))
.orElse(w -> c.accept(Stream.of(w)))
));
}

}
150 changes: 150 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,156 @@ public void shouldFilterArrayObjectValues() {
);
}

@Test
public void shouldFlattenFlatArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"flatten('flat')"
),
i -> {
i.startRecord("1");
i.literal("flat", "1");
i.literal("flat", "2");
i.literal("flat", "3");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("flat", "1");
o.get().literal("flat", "2");
o.get().literal("flat", "3");
o.get().endRecord();
}
);
}

@Test
public void shouldFlattenFlatHash() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"flatten('flat')"
),
i -> {
i.startRecord("1");
i.startEntity("flat");
i.literal("a", "1");
i.literal("a", "2");
i.literal("a", "3");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("flat");
o.get().literal("a", "1");
o.get().literal("a", "2");
o.get().literal("a", "3");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldFlattenFlatString() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"flatten('flat')"
),
i -> {
i.startRecord("1");
i.literal("flat", "1");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("flat", "1");
o.get().endRecord();
}
);
}

@Test
public void shouldFlattenNestedArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"flatten('deep[]')"
),
i -> {
i.startRecord("1");
i.startEntity("deep[]");
i.literal("1", "1");
i.startEntity("2[]");
i.literal("1", "2");
i.literal("2", "3");
i.endEntity();
i.startEntity("3[]");
i.startEntity("1[]");
i.literal("1", "4");
i.literal("2", "5");
i.endEntity();
i.literal("2", "6");
i.endEntity();
i.literal("4", "7");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("deep[]");
o.get().literal("1", "1");
o.get().literal("2", "2");
o.get().literal("3", "3");
o.get().literal("4", "4");
o.get().literal("5", "5");
o.get().literal("6", "6");
o.get().literal("7", "7");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldFlattenNestedArrayWithHashes() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"flatten('deep[]')"
),
i -> {
i.startRecord("1");
i.startEntity("deep[]");
i.literal("1", "1");
i.startEntity("2");
i.literal("a", "2");
i.literal("a", "3");
i.endEntity();
i.startEntity("3[]");
i.startEntity("1");
i.literal("a", "4");
i.literal("a", "5");
i.endEntity();
i.literal("2", "6");
i.endEntity();
i.literal("4", "7");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("deep[]");
o.get().literal("1", "1");
o.get().startEntity("2");
o.get().literal("a", "2");
o.get().literal("a", "3");
o.get().endEntity();
o.get().startEntity("3");
o.get().literal("a", "4");
o.get().literal("a", "5");
o.get().endEntity();
o.get().literal("4", "6");
o.get().literal("5", "7");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldGetFirstIndexOfSubstring() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

This file was deleted.

0 comments on commit e044f86

Please sign in to comment.