Skip to content

Commit 4dd061b

Browse files
authored
Merge pull request #313 from metafacture/116-destructiveCopyFieldMoveField
Change `copy_field()`/`move_field()` Fix functions to destructive behaviour for Catmandu compatibility.
2 parents ef44f8c + 2427f2c commit 4dd061b

File tree

10 files changed

+241
-51
lines changed

10 files changed

+241
-51
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,16 @@ public void apply(final Metafix metafix, final Record record, final List<String>
164164
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
165165
final String oldName = params.get(0);
166166
final String newName = params.get(1);
167-
Value.asList(record.get(oldName), a -> a.forEach(oldValue -> {
168-
record.addNested(newName, oldValue); // we're actually aliasing
169-
}));
167+
168+
final Value oldValue = record.get(oldName);
169+
if (!Value.isNull(oldValue)) {
170+
oldValue.matchType()
171+
.ifArray(a -> {
172+
record.remove(newName);
173+
a.forEach(v -> record.addNested(newName, v));
174+
})
175+
.orElse(v -> record.set(newName, v));
176+
}
170177
}
171178
},
172179
format {

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ public void doList() {
5454
i.literal("name", " A University");
5555
i.literal("name", "Max ");
5656
i.endRecord();
57+
}, o -> {
58+
o.get().startRecord("1");
59+
o.get().literal("author", "MAX");
60+
o.get().endRecord();
61+
});
62+
}
63+
64+
@Test
65+
public void doListExplicitAppend() {
66+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
67+
"set_array('author')",
68+
"do list('path': 'name', 'var': 'n')",
69+
" upcase('n')",
70+
" trim('n')",
71+
" copy_field('n', 'author.$append')",
72+
"end",
73+
"remove_field('name')"),
74+
i -> {
75+
i.startRecord("1");
76+
i.literal("name", " A University");
77+
i.literal("name", "Max ");
78+
i.endRecord();
5779
}, o -> {
5880
o.get().startRecord("1");
5981
o.get().literal("author", "A UNIVERSITY");
@@ -180,6 +202,30 @@ public void doListPathWithDots() {
180202
i.literal("name", "Max ");
181203
i.endEntity();
182204
i.endRecord();
205+
}, o -> {
206+
o.get().startRecord("1");
207+
o.get().literal("author", "MAX");
208+
o.get().endRecord();
209+
});
210+
}
211+
212+
@Test
213+
public void doListPathWithDotsExplicitAppend() {
214+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
215+
"set_array('author')",
216+
"do list('path': 'some.name', 'var': 'n')",
217+
" upcase('n')",
218+
" trim('n')",
219+
" copy_field('n', 'author.$append')",
220+
"end",
221+
"remove_field('some')"),
222+
i -> {
223+
i.startRecord("1");
224+
i.startEntity("some");
225+
i.literal("name", " A University");
226+
i.literal("name", "Max ");
227+
i.endEntity();
228+
i.endRecord();
183229
}, o -> {
184230
o.get().startRecord("1");
185231
o.get().literal("author", "A UNIVERSITY");
@@ -239,6 +285,32 @@ public void doListEntitesToLiterals() {
239285
i.literal("name", "Max ");
240286
i.endEntity();
241287
i.endRecord();
288+
}, o -> {
289+
o.get().startRecord("1");
290+
o.get().literal("author", "MAX");
291+
o.get().endRecord();
292+
});
293+
}
294+
295+
@Test
296+
public void doListEntitesToLiteralsExplicitAppend() {
297+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
298+
"set_array('author')",
299+
"do list('path': 'creator', 'var': 'c')",
300+
" upcase('c.name')",
301+
" trim('c.name')",
302+
" copy_field('c.name', 'author.$append')",
303+
"end",
304+
"remove_field('creator')"),
305+
i -> {
306+
i.startRecord("1");
307+
i.startEntity("creator");
308+
i.literal("name", " A University");
309+
i.endEntity();
310+
i.startEntity("creator");
311+
i.literal("name", "Max ");
312+
i.endEntity();
313+
i.endRecord();
242314
}, o -> {
243315
o.get().startRecord("1");
244316
o.get().literal("author", "A UNIVERSITY");
@@ -401,6 +473,32 @@ public void doListIndexedArrayOfObjects() {
401473
i.endEntity();
402474
i.endEntity();
403475
i.endRecord();
476+
}, o -> {
477+
o.get().startRecord("1");
478+
o.get().literal("author", "Max");
479+
o.get().endRecord();
480+
});
481+
}
482+
483+
@Test
484+
public void doListIndexedArrayOfObjectsExplicitAppend() {
485+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
486+
"set_array('author')",
487+
"do list('path': 'name[]', 'var': 'n')",
488+
" copy_field('n.name', 'author.$append')",
489+
"end",
490+
"remove_field('name[]')"),
491+
i -> {
492+
i.startRecord("1");
493+
i.startEntity("name[]");
494+
i.startEntity("1");
495+
i.literal("name", "A University");
496+
i.endEntity();
497+
i.startEntity("2");
498+
i.literal("name", "Max");
499+
i.endEntity();
500+
i.endEntity();
501+
i.endRecord();
404502
}, o -> {
405503
o.get().startRecord("1");
406504
o.get().literal("author", "A University");

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

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,30 @@ public void shouldLookupDeduplicatedInternalArrayWithAsterisk() {
169169
}
170170

171171
@Test
172-
public void shouldLookupCopiedInternalArrayWithAsterisk() {
172+
public void shouldNotLookupCopiedInternalArrayWithAsterisk() {
173+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
174+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
175+
"set_array('data', 'Aloha')",
176+
"set_array('title')",
177+
"copy_field('data', 'title')",
178+
LOOKUP + " Aloha: Alohaeha)"
179+
),
180+
i -> {
181+
i.startRecord("1");
182+
i.endRecord();
183+
},
184+
o -> {
185+
}
186+
)
187+
);
188+
}
189+
190+
@Test
191+
public void shouldLookupCopiedInternalArrayWithAsteriskExplicitAppend() {
173192
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
174193
"set_array('data', 'Aloha')",
175194
"set_array('title')",
176-
"copy_field('data', 'title')",
195+
"copy_field('data', 'title.$append')",
177196
LOOKUP + " Aloha: Alohaeha)"
178197
),
179198
i -> {
@@ -190,12 +209,32 @@ public void shouldLookupCopiedInternalArrayWithAsterisk() {
190209
}
191210

192211
@Test
193-
public void shouldLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
212+
public void shouldNotLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
213+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
214+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
215+
"set_array('data', 'Aloha', 'Aloha')",
216+
"uniq('data')",
217+
"set_array('title')",
218+
"copy_field('data', 'title')",
219+
LOOKUP + " Aloha: Alohaeha)"
220+
),
221+
i -> {
222+
i.startRecord("1");
223+
i.endRecord();
224+
},
225+
o -> {
226+
}
227+
)
228+
);
229+
}
230+
231+
@Test
232+
public void shouldLookupCopiedDeduplicatedInternalArrayWithAsteriskExplicitAppend() {
194233
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
195234
"set_array('data', 'Aloha', 'Aloha')",
196235
"uniq('data')",
197236
"set_array('title')",
198-
"copy_field('data', 'title')",
237+
"copy_field('data', 'title.$append')",
199238
LOOKUP + " Aloha: Alohaeha)"
200239
),
201240
i -> {
@@ -212,10 +251,10 @@ public void shouldLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
212251
}
213252

214253
@Test
215-
public void shouldLookupCopiedExternalArrayWithAsterisk() {
254+
public void shouldLookupCopiedExternalArrayWithAsteriskExplicitAppend() {
216255
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
217256
"set_array('title')",
218-
"copy_field('data', 'title')",
257+
"copy_field('data', 'title.$append')",
219258
LOOKUP + " Aloha: Alohaeha)"
220259
),
221260
i -> {
@@ -233,11 +272,11 @@ public void shouldLookupCopiedExternalArrayWithAsterisk() {
233272
}
234273

235274
@Test
236-
public void shouldLookupCopiedDeduplicatedExternalArrayWithAsterisk() {
275+
public void shouldLookupCopiedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
237276
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
238277
"uniq('data')",
239278
"set_array('title')",
240-
"copy_field('data', 'title')",
279+
"copy_field('data', 'title.$append')",
241280
LOOKUP + " Aloha: Alohaeha)"
242281
),
243282
i -> {
@@ -256,11 +295,32 @@ public void shouldLookupCopiedDeduplicatedExternalArrayWithAsterisk() {
256295
}
257296

258297
@Test
259-
public void shouldLookupMovedDeduplicatedExternalArrayWithAsterisk() {
298+
public void shouldNotLookupMovedDeduplicatedExternalArrayWithAsterisk() {
299+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
300+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
301+
"uniq('data')",
302+
"set_array('title')",
303+
"move_field('data', 'title')",
304+
LOOKUP + " Aloha: Alohaeha)"
305+
),
306+
i -> {
307+
i.startRecord("1");
308+
i.literal("data", "Aloha");
309+
i.literal("data", "Aloha");
310+
i.endRecord();
311+
},
312+
o -> {
313+
}
314+
)
315+
);
316+
}
317+
318+
@Test
319+
public void shouldLookupMovedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
260320
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
261321
"uniq('data')",
262322
"set_array('title')",
263-
"move_field('data', 'title')",
323+
"move_field('data', 'title.$append')",
264324
LOOKUP + " Aloha: Alohaeha)"
265325
),
266326
i -> {
@@ -278,10 +338,29 @@ public void shouldLookupMovedDeduplicatedExternalArrayWithAsterisk() {
278338
}
279339

280340
@Test
281-
public void shouldLookupMovedExternalArrayWithAsterisk() {
341+
public void shouldNotLookupMovedExternalArrayWithAsterisk() {
342+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
343+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
344+
"set_array('title')",
345+
"move_field('data', 'title')",
346+
LOOKUP + " Aloha: Alohaeha)"
347+
),
348+
i -> {
349+
i.startRecord("1");
350+
i.literal("data", "Aloha");
351+
i.endRecord();
352+
},
353+
o -> {
354+
}
355+
)
356+
);
357+
}
358+
359+
@Test
360+
public void shouldLookupMovedExternalArrayWithAsteriskExplicitAppend() {
282361
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
283362
"set_array('title')",
284-
"move_field('data', 'title')",
363+
"move_field('data', 'title.$append')",
285364
LOOKUP + " Aloha: Alohaeha)"
286365
),
287366
i -> {

0 commit comments

Comments
 (0)