Skip to content

Commit

Permalink
Merge pull request #299 from metafacture/294-entityMemberName
Browse files Browse the repository at this point in the history
Add `entityMemberName` option to Metafix
  • Loading branch information
fsteeg authored May 4, 2023
2 parents 4b67fbe + 935f05f commit 847bc0e
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 5 deletions.
15 changes: 12 additions & 3 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013, 2019 Deutsche Nationalbibliothek and others
* Copyright 2013, 2023 Deutsche Nationalbibliothek and others
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,6 +87,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
private Strictness strictness = DEFAULT_STRICTNESS;
private String fixFile;
private String recordIdentifier;
private String entityMemberName = "%d";
private boolean repeatedFieldsToEntities;
private boolean strictnessHandlesProcessExceptions;
private int entityCount;
Expand Down Expand Up @@ -235,7 +236,7 @@ private void emit(final String field, final Value value) {

for (int i = 0; i < array.size(); ++i) {
final Value currentValue = array.get(i);
final String fieldName = isMulti ? String.valueOf(i + 1) : field;
final String fieldName = isMulti ? String.format(entityMemberName, i + 1) : field;

currentValue.matchType()
.ifArray(a -> emit(isMulti ? fieldName + ARRAY_MARKER : fieldName, currentValue))
Expand Down Expand Up @@ -395,6 +396,14 @@ public boolean getRepeatedFieldsToEntities() {
return repeatedFieldsToEntities;
}

public void setEntityMemberName(final String entityMemberName) {
this.entityMemberName = entityMemberName;
}

public String getEntityMemberName() {
return entityMemberName;
}

public enum Strictness {

/**
Expand Down Expand Up @@ -440,5 +449,5 @@ protected void log(final MetafactureException exception, final BiConsumer<String
}

}

}

136 changes: 134 additions & 2 deletions metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Fabian Steeg, hbz
* Copyright 2021, 2023 Fabian Steeg, hbz
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.metafacture.metafix;
package org.metafacture.metafix; // checkstyle-disable-line JavaNCSS

import org.metafacture.framework.StreamReceiver;
import org.metafacture.metamorph.api.MorphBuildException;
Expand Down Expand Up @@ -2721,6 +2721,138 @@ public void dontEmitEntityForSingleField() {
);
}

@Test
public void setRepeatedFieldsToEntitiesAndSetEntityMemberName() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.setRepeatedFieldsToEntities(true);
i.setEntityMemberName("*");

i.startRecord("1");
i.literal("name", "max");
i.literal("name", "mo");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("name");
o.get().literal("*", "max");
o.get().literal("*", "mo");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void setRepeatedFieldsToEntitiesAndSetEntityMemberNameWithNumericalSubfield() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.setRepeatedFieldsToEntities(true);
i.setEntityMemberName("*");

i.startRecord("1");
i.startEntity("1001 ");
i.literal("1", "max");
i.literal("1", "mo");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("1001 ");
o.get().startEntity("1");
o.get().literal("*", "max");
o.get().literal("*", "mo");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

@Test
public void setRepeatedFieldsToEntitiesWithNumericalSubfields() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.setRepeatedFieldsToEntities(true);

i.startRecord("1");
i.startEntity("1001 ");
i.literal("1", "max");
i.literal("1", "mo");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("1001 ");
o.get().startEntity("1");
o.get().literal("1", "max");
o.get().literal("2", "mo");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

@Test
public void setEntityMemberNameNoArrayMarkerOrEntity() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.setEntityMemberName("*"); // no arrays or entities, no effect

i.startRecord("1");
i.startEntity("1001 ");
i.literal("1", "max");
i.literal("1", "mo");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("1001 ");
o.get().literal("1", "max");
o.get().literal("1", "mo");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void setEntityMemberNameWithArrayMarker() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.setEntityMemberName("*");

i.startRecord("1");
i.startEntity("1001 []");
i.literal("1", "max");
i.literal("1", "mo");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("1001 []");
o.get().literal("*", "max");
o.get().literal("*", "mo");
o.get().endEntity();
o.get().endRecord();
}
);
}

public void shouldNotAccessArrayImplicitly() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

0 comments on commit 847bc0e

Please sign in to comment.