Skip to content

Commit 1741ca8

Browse files
committed
Add entityMemberName option to Metafix (#294)
e.g. to support outputting array indexes as wildcards when listing available paths. Implemented like `arrayName` in `JsonDecoder`. Added tests in a new class due to NCSS checkstyle warning when expanding `MetafixRecordTest`.
1 parent 4b67fbe commit 1741ca8

File tree

2 files changed

+185
-3
lines changed

2 files changed

+185
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013, 2019 Deutsche Nationalbibliothek and others
2+
* Copyright 2013, 2023 Deutsche Nationalbibliothek and others
33
*
44
* Licensed under the Apache License, Version 2.0 the "License";
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
8888
private String fixFile;
8989
private String recordIdentifier;
9090
private boolean repeatedFieldsToEntities;
91+
private String entityMemberName = "%d";
9192
private boolean strictnessHandlesProcessExceptions;
9293
private int entityCount;
9394

@@ -235,7 +236,7 @@ private void emit(final String field, final Value value) {
235236

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

240241
currentValue.matchType()
241242
.ifArray(a -> emit(isMulti ? fieldName + ARRAY_MARKER : fieldName, currentValue))
@@ -395,6 +396,14 @@ public boolean getRepeatedFieldsToEntities() {
395396
return repeatedFieldsToEntities;
396397
}
397398

399+
public void setEntityMemberName(final String entityMemberName) {
400+
this.entityMemberName = entityMemberName;
401+
}
402+
403+
public String getEntityMemberName() {
404+
return entityMemberName;
405+
}
406+
398407
public enum Strictness {
399408

400409
/**
@@ -440,5 +449,4 @@ protected void log(final MetafactureException exception, final BiConsumer<String
440449
}
441450

442451
}
443-
444452
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2023 Fabian Steeg, hbz
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix;
18+
19+
import org.metafacture.framework.StreamReceiver;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.Mock;
24+
import org.mockito.junit.jupiter.MockitoExtension;
25+
26+
import java.util.Arrays;
27+
28+
/**
29+
* Tests Metafix custom configuration options.
30+
*
31+
* @author Fabian Steeg
32+
*/
33+
@ExtendWith(MockitoExtension.class)
34+
public class MetafixConfigTest {
35+
36+
@Mock
37+
private StreamReceiver streamReceiver;
38+
39+
public MetafixConfigTest() {
40+
}
41+
42+
@Test
43+
public void setRepeatedFieldsToEntitiesAndSetEntityMemberName() {
44+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
45+
"nothing()"
46+
),
47+
i -> {
48+
i.setRepeatedFieldsToEntities(true);
49+
i.setEntityMemberName("*");
50+
51+
i.startRecord("1");
52+
i.literal("name", "max");
53+
i.literal("name", "mo");
54+
i.endRecord();
55+
},
56+
o -> {
57+
o.get().startRecord("1");
58+
o.get().startEntity("name");
59+
o.get().literal("*", "max");
60+
o.get().literal("*", "mo");
61+
o.get().endEntity();
62+
o.get().endRecord();
63+
}
64+
);
65+
}
66+
67+
@Test
68+
public void setRepeatedFieldsToEntitiesAndSetEntityMemberNameWithNumericalSubfield() {
69+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
70+
"nothing()"
71+
),
72+
i -> {
73+
i.setRepeatedFieldsToEntities(true);
74+
i.setEntityMemberName("*");
75+
76+
i.startRecord("1");
77+
i.startEntity("1001 ");
78+
i.literal("1", "max");
79+
i.literal("1", "mo");
80+
i.endEntity();
81+
i.endRecord();
82+
},
83+
(o, f) -> {
84+
o.get().startRecord("1");
85+
o.get().startEntity("1001 ");
86+
o.get().startEntity("1");
87+
o.get().literal("*", "max");
88+
o.get().literal("*", "mo");
89+
f.apply(2).endEntity();
90+
o.get().endRecord();
91+
}
92+
);
93+
}
94+
95+
@Test
96+
public void setRepeatedFieldsToEntitiesWithNumericalSubfields() {
97+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
98+
"nothing()"
99+
),
100+
i -> {
101+
i.setRepeatedFieldsToEntities(true);
102+
103+
i.startRecord("1");
104+
i.startEntity("1001 ");
105+
i.literal("1", "max");
106+
i.literal("1", "mo");
107+
i.endEntity();
108+
i.endRecord();
109+
},
110+
(o, f) -> {
111+
o.get().startRecord("1");
112+
o.get().startEntity("1001 ");
113+
o.get().startEntity("1");
114+
o.get().literal("1", "max");
115+
o.get().literal("2", "mo");
116+
f.apply(2).endEntity();
117+
o.get().endRecord();
118+
}
119+
);
120+
}
121+
122+
@Test
123+
public void setEntityMemberNameNoArrayMarkerOrEntity() {
124+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
125+
"nothing()"
126+
),
127+
i -> {
128+
i.setEntityMemberName("*"); // no arrays or entities, no effect
129+
130+
i.startRecord("1");
131+
i.startEntity("1001 ");
132+
i.literal("1", "max");
133+
i.literal("1", "mo");
134+
i.endEntity();
135+
i.endRecord();
136+
},
137+
o -> {
138+
o.get().startRecord("1");
139+
o.get().startEntity("1001 ");
140+
o.get().literal("1", "max");
141+
o.get().literal("1", "mo");
142+
o.get().endEntity();
143+
o.get().endRecord();
144+
}
145+
);
146+
}
147+
148+
@Test
149+
public void setEntityMemberNameWithArrayMarker() {
150+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
151+
"nothing()"
152+
),
153+
i -> {
154+
i.setEntityMemberName("*");
155+
156+
i.startRecord("1");
157+
i.startEntity("1001 []");
158+
i.literal("1", "max");
159+
i.literal("1", "mo");
160+
i.endEntity();
161+
i.endRecord();
162+
},
163+
o -> {
164+
o.get().startRecord("1");
165+
o.get().startEntity("1001 []");
166+
o.get().literal("*", "max");
167+
o.get().literal("*", "mo");
168+
o.get().endEntity();
169+
o.get().endRecord();
170+
}
171+
);
172+
}
173+
174+
}

0 commit comments

Comments
 (0)