Skip to content

Commit

Permalink
Merge pull request #313 from metafacture/116-destructiveCopyFieldMove…
Browse files Browse the repository at this point in the history
…Field

Change `copy_field()`/`move_field()` Fix functions to destructive behaviour for Catmandu compatibility.
  • Loading branch information
blackwinter authored Jun 15, 2023
2 parents ef44f8c + 2427f2c commit 4dd061b
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 51 deletions.
13 changes: 10 additions & 3 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ public void apply(final Metafix metafix, final Record record, final List<String>
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(oldValue -> {
record.addNested(newName, oldValue); // we're actually aliasing
}));

final Value oldValue = record.get(oldName);
if (!Value.isNull(oldValue)) {
oldValue.matchType()
.ifArray(a -> {
record.remove(newName);
a.forEach(v -> record.addNested(newName, v));
})
.orElse(v -> record.set(newName, v));
}
}
},
format {
Expand Down
98 changes: 98 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public void doList() {
i.literal("name", " A University");
i.literal("name", "Max ");
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "MAX");
o.get().endRecord();
});
}

@Test
public void doListExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"do list('path': 'name', 'var': 'n')",
" upcase('n')",
" trim('n')",
" copy_field('n', 'author.$append')",
"end",
"remove_field('name')"),
i -> {
i.startRecord("1");
i.literal("name", " A University");
i.literal("name", "Max ");
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "A UNIVERSITY");
Expand Down Expand Up @@ -180,6 +202,30 @@ public void doListPathWithDots() {
i.literal("name", "Max ");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "MAX");
o.get().endRecord();
});
}

@Test
public void doListPathWithDotsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"do list('path': 'some.name', 'var': 'n')",
" upcase('n')",
" trim('n')",
" copy_field('n', 'author.$append')",
"end",
"remove_field('some')"),
i -> {
i.startRecord("1");
i.startEntity("some");
i.literal("name", " A University");
i.literal("name", "Max ");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "A UNIVERSITY");
Expand Down Expand Up @@ -239,6 +285,32 @@ public void doListEntitesToLiterals() {
i.literal("name", "Max ");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "MAX");
o.get().endRecord();
});
}

@Test
public void doListEntitesToLiteralsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"do list('path': 'creator', 'var': 'c')",
" upcase('c.name')",
" trim('c.name')",
" copy_field('c.name', 'author.$append')",
"end",
"remove_field('creator')"),
i -> {
i.startRecord("1");
i.startEntity("creator");
i.literal("name", " A University");
i.endEntity();
i.startEntity("creator");
i.literal("name", "Max ");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "A UNIVERSITY");
Expand Down Expand Up @@ -401,6 +473,32 @@ public void doListIndexedArrayOfObjects() {
i.endEntity();
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "Max");
o.get().endRecord();
});
}

@Test
public void doListIndexedArrayOfObjectsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n.name', 'author.$append')",
"end",
"remove_field('name[]')"),
i -> {
i.startRecord("1");
i.startEntity("name[]");
i.startEntity("1");
i.literal("name", "A University");
i.endEntity();
i.startEntity("2");
i.literal("name", "Max");
i.endEntity();
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("author", "A University");
Expand Down
103 changes: 91 additions & 12 deletions metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,30 @@ public void shouldLookupDeduplicatedInternalArrayWithAsterisk() {
}

@Test
public void shouldLookupCopiedInternalArrayWithAsterisk() {
public void shouldNotLookupCopiedInternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha')",
"set_array('title')",
"copy_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
i.startRecord("1");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldLookupCopiedInternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha')",
"set_array('title')",
"copy_field('data', 'title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -190,12 +209,32 @@ public void shouldLookupCopiedInternalArrayWithAsterisk() {
}

@Test
public void shouldLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
public void shouldNotLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha', 'Aloha')",
"uniq('data')",
"set_array('title')",
"copy_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
i.startRecord("1");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldLookupCopiedDeduplicatedInternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha', 'Aloha')",
"uniq('data')",
"set_array('title')",
"copy_field('data', 'title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -212,10 +251,10 @@ public void shouldLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
}

@Test
public void shouldLookupCopiedExternalArrayWithAsterisk() {
public void shouldLookupCopiedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"copy_field('data', 'title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -233,11 +272,11 @@ public void shouldLookupCopiedExternalArrayWithAsterisk() {
}

@Test
public void shouldLookupCopiedDeduplicatedExternalArrayWithAsterisk() {
public void shouldLookupCopiedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"copy_field('data', 'title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -256,11 +295,32 @@ public void shouldLookupCopiedDeduplicatedExternalArrayWithAsterisk() {
}

@Test
public void shouldLookupMovedDeduplicatedExternalArrayWithAsterisk() {
public void shouldNotLookupMovedDeduplicatedExternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"move_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
i.startRecord("1");
i.literal("data", "Aloha");
i.literal("data", "Aloha");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldLookupMovedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"move_field('data', 'title')",
"move_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -278,10 +338,29 @@ public void shouldLookupMovedDeduplicatedExternalArrayWithAsterisk() {
}

@Test
public void shouldLookupMovedExternalArrayWithAsterisk() {
public void shouldNotLookupMovedExternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"move_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
i.startRecord("1");
i.literal("data", "Aloha");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldLookupMovedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"move_field('data', 'title')",
"move_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand Down
Loading

0 comments on commit 4dd061b

Please sign in to comment.