Skip to content

Commit 809bdfd

Browse files
committed
Use common template in analyzer modules, set as option (#300)
See https://gitlab.com/oersi/oersi-etl/-/issues/238
1 parent 4b0a8f3 commit 809bdfd

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
*/
3131
@Description("Lists all paths found in the input records. These paths can be used in a Fix to address fields. Options: " +
3232
"count (output occurence frequency of each path, sorted by highest frequency first; default: true), " +
33+
"template (for formatting the internal triple structure; default: ${s}\t|\t${o} if count is true, else ${s})" +
3334
"index (output individual repeated subfields and array elements with index numbers instead of '*'; default: false)")
3435
@In(StreamReceiver.class)
3536
@Out(String.class)
3637
@FluxCommand("fix-list-paths")
3738
public class MetafixListPaths extends MetafixStreamAnalyzer {
3839

3940
public MetafixListPaths() {
40-
super("nothing()", Compare.PREDICATE, "${s}\t|\t${o}");
41+
super("nothing()", Compare.PREDICATE);
4142
setIndex(false);
4243
}
4344

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
* @author Fabian Steeg
3030
*/
3131
@Description("Lists all values found for the given path. The paths can be found using fix-list-paths. Options: " +
32-
"count (output occurence frequency of each value, sorted by highest frequency first; default: true)")
32+
"count (output occurence frequency of each value, sorted by highest frequency first; default: true)" +
33+
"template (for formatting the internal triple structure; default: ${s}\t|\t${o} if count is true, else ${s})")
3334
@In(StreamReceiver.class)
3435
@Out(String.class)
3536
@FluxCommand("fix-list-values")
3637
public class MetafixListValues extends MetafixStreamAnalyzer {
3738

3839
public MetafixListValues(final String path) {
39-
super(fix(path), Compare.OBJECT, "${o}\t|\t${s}");
40+
super(fix(path), Compare.OBJECT);
4041
}
4142

4243
private static String fix(final String path) {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
*/
3737
/* package-private */ class MetafixStreamAnalyzer extends DefaultStreamPipe<ObjectReceiver<String>> {
3838

39+
private static final String DEFAULT_COUNTED_TEMPLATE = "${s}\t|\t${o}";
40+
private static final String DEFAULT_UNCOUNTED_TEMPLATE = "${s}";
41+
3942
private Metafix fix;
4043
private boolean count = true;
4144
private Compare countBy;
42-
private String countedTemplate;
43-
private String uncountedTemplate = "${s}";
45+
private String template;
4446

45-
/* package-private */ MetafixStreamAnalyzer(final String fix, final Compare countBy,
46-
final String countedTemplate) {
47+
/* package-private */ MetafixStreamAnalyzer(final String fix, final Compare countBy) {
4748
try {
4849
this.fix = new Metafix(fix);
4950
this.fix.setRepeatedFieldsToEntities(true);
@@ -52,17 +53,17 @@
5253
throw new MetafactureException(e);
5354
}
5455
this.countBy = countBy;
55-
this.countedTemplate = countedTemplate;
5656
}
5757

5858
@Override
5959
protected void onSetReceiver() {
60+
template = template != null ? template : count ? DEFAULT_COUNTED_TEMPLATE : DEFAULT_UNCOUNTED_TEMPLATE;
6061
fix
6162
.setReceiver(new StreamFlattener())
6263
.setReceiver(new StreamToTriples())
6364
.setReceiver(tripleCount())
6465
.setReceiver(tripleSort())
65-
.setReceiver(new ObjectTemplate<>(count ? countedTemplate : uncountedTemplate))
66+
.setReceiver(new ObjectTemplate<>(template))
6667
.setReceiver(getReceiver());
6768
}
6869

@@ -123,6 +124,14 @@ public boolean getCount() {
123124
return this.count;
124125
}
125126

127+
public void setTemplate(final String template) {
128+
this.template = template;
129+
}
130+
131+
public String getTemplate() {
132+
return this.template;
133+
}
134+
126135
/* package-private */ Metafix getFix() {
127136
return this.fix;
128137
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private void processRecord() {
120120
}
121121

122122
private void verify(final String... result) throws MockitoAssertionError {
123+
lister.setTemplate(lister.getCount() ? "${o}\t|\t${s}" : "${s}");
123124
processRecord();
124125
try {
125126
final InOrder ordered = Mockito.inOrder(receiver);

0 commit comments

Comments
 (0)