Skip to content

Commit

Permalink
Change further use of set_array to add_array and set_hash to set_hash #…
Browse files Browse the repository at this point in the history
…374

Additional preliminary work for #309 when the set-fixes change their functionality.
  • Loading branch information
TobiasNx committed Oct 16, 2024
1 parent 00838b6 commit aea03a8
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 97 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ Options:

#### Record-level functions

##### `add_array`

Creates a new array (with optional values).

```perl
add_array("<targetFieldName>")
add_array("<targetFieldName>", "<value_1>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_array)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_array+{")

##### `add_field`

Creates a field with a defined value.
Expand All @@ -315,6 +328,19 @@ add_field("<targetFieldName>", "<fieldValue>")

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_field+{")

##### `add_hash`

Creates a new hash (with optional values).

```perl
add_hash("<targetFieldName>")
add_hash("<targetFieldName>", "subfieldName": "<subfieldValue>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_hash)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_hash+{")

##### `array`

Converts a hash/object into an array.
Expand Down Expand Up @@ -1027,7 +1053,7 @@ E.g.:
```perl
# "ccm:university":["https://ror.org/0304hq317"]
# "ccm:university_DISPLAYNAME":["Gottfried Wilhelm Leibniz Universität Hannover"]
set_array("sourceOrga[]")
add_array("sourceOrga[]")
do list_as(orgId: "ccm:university[]", orgName: "ccm:university_DISPLAYNAME[]")
copy_field(orgId, "sourceOrga[].$append.id")
copy_field(orgName, "sourceOrga[].$last.name")
Expand Down
32 changes: 16 additions & 16 deletions metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void doList() {
@Test
public void doListExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'name', 'var': 'n')",
" upcase('n')",
" trim('n')",
Expand Down Expand Up @@ -212,7 +212,7 @@ public void doListPathWithDots() {
@Test
public void doListPathWithDotsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'some.name', 'var': 'n')",
" upcase('n')",
" trim('n')",
Expand All @@ -237,7 +237,7 @@ public void doListPathWithDotsExplicitAppend() {
@Test
public void doListWithAppendAndLast() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" copy_field('c.name', 'author[].$append.name')",
" add_field('author[].$last.type', 'Default')",
Expand Down Expand Up @@ -295,7 +295,7 @@ public void doListEntitesToLiterals() {
@Test
public void doListEntitesToLiteralsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'creator', 'var': 'c')",
" upcase('c.name')",
" trim('c.name')",
Expand All @@ -322,7 +322,7 @@ public void doListEntitesToLiteralsExplicitAppend() {
@Test
public void doListEntitesToEntities() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" copy_field('c.name', 'author[].$append.name')",
" if all_contain('c.name', 'University')",
Expand Down Expand Up @@ -359,7 +359,7 @@ public void doListEntitesToEntities() {
@Test
public void wildcardForNestedEntities() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" if any_match('c.role.*.roleTerm.*.value','aut|cre')",
" copy_field('c.name', 'author[].$append.name')",
Expand Down Expand Up @@ -430,7 +430,7 @@ public void doListIndexedArray() {
@Test
public void doListIndexedArrayToArrayOfObjects() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n', 'author[].$append.name')",
"end",
Expand Down Expand Up @@ -483,7 +483,7 @@ public void doListIndexedArrayOfObjects() {
@Test
public void doListIndexedArrayOfObjectsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n.name', 'author.$append')",
"end",
Expand All @@ -510,7 +510,7 @@ public void doListIndexedArrayOfObjectsExplicitAppend() {
@Test
public void doListIndexedArrayOfObjectsToArrayOfObjects() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n.name', 'author[].$append.name')",
"end",
Expand Down Expand Up @@ -670,7 +670,7 @@ public void ifInCollectorCombine() {

private void shouldIterateOverList(final String path, final int expectedCount) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_array('trace')",
"do list(path: '" + path + "', 'var': '$i')",
" add_field('trace.$append', 'true')",
"end",
Expand Down Expand Up @@ -716,7 +716,7 @@ public void shouldIterateOverListWithWildcard() {

private void shouldIterateOverListOfHashes(final String path, final int expectedCount) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_array('trace')",
"do list(path: '" + path + "', 'var': '$i')",
" add_field('trace.$append', 'true')",
"end",
Expand Down Expand Up @@ -771,7 +771,7 @@ public void shouldIterateOverListOfHashesWithWildcard() {
// See https://github.com/metafacture/metafacture-fix/issues/119
public void shouldPerformComplexOperationWithPathWildcard() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('coll[]')",
"add_array('coll[]')",
"do list(path: 'feld?', 'var': '$i')",
" add_field('coll[].$append.feldtest', 'true')",
" copy_field('$i.a.value', 'coll[].$last.a')",
Expand Down Expand Up @@ -847,7 +847,7 @@ public void shouldPerformComplexOperationWithPathWildcard() {
@Test
public void shouldDoListAsWithSingleList() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]')",
" copy_field(orgId, 'sourceOrga[].$append.id')",
"end"
Expand Down Expand Up @@ -881,7 +881,7 @@ public void shouldDoListAsWithSingleList() {
@Test
public void shouldDoListAsWithMultipleLists() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]', orgName: 'ccm:university_DISPLAYNAME[]', orgLoc: 'ccm:university_LOCATION[]')",
" copy_field(orgId, 'sourceOrga[].$append.id')",
" copy_field(orgName, 'sourceOrga[].$last.name')",
Expand Down Expand Up @@ -937,9 +937,9 @@ public void shouldDoListAsWithMultipleLists() {
@Test // checkstyle-disable-line JavaNCSS
public void shouldDoListAsWithMultipleListsOfDifferentSizes() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]', orgName: 'ccm:university_DISPLAYNAME[]', orgLoc: 'ccm:university_LOCATION[]')",
" set_hash('sourceOrga[].$append')",
" add_hash('sourceOrga[].$append')",
" copy_field(orgId, 'sourceOrga[].$last.id')",
" copy_field(orgName, 'sourceOrga[].$last.name')",
" copy_field(orgLoc, 'sourceOrga[].$last.location')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ public void shouldReportArrayEntityAsArray() {
@Test
public void shouldReportEmptyArrayAsArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array(foo)",
"add_array(foo)",
"if is_array(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -1767,7 +1767,7 @@ public void shouldReportEmptyArrayEntityAsEmpty() {
@Test
public void shouldReportEmptyArrayAsEmpty() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array(foo)",
"add_array(foo)",
"if is_empty(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -2127,7 +2127,7 @@ public void shouldReportHashAsObject() {
@Test
public void shouldReportEmptyHashAsObject() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_hash(foo)",
"add_hash(foo)",
"if is_object(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -2499,7 +2499,7 @@ public void shouldMatchString() {
@Test
public void shouldTestMacroVariable() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('type')",
"add_array('type')",
"do put_macro('test')",
" if str_contain('name', 'a$[var]')",
" add_field('type.$append', 'Organization: $[var]')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void inlineDotNotationNested() {
@Test
public void shouldLookupInternalArrayWithAsterisk() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title', 'Aloha')",
"add_array('title', 'Aloha')",
LOOKUP + " Aloha: Alohaeha)"
),
i -> {
Expand All @@ -162,7 +162,7 @@ public void shouldLookupInternalArrayWithAsterisk() {
@Test
public void shouldLookupDeduplicatedInternalArrayWithAsterisk() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title', 'Aloha', 'Aloha')",
"add_array('title', 'Aloha', 'Aloha')",
"uniq('title')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -182,8 +182,8 @@ public void shouldLookupDeduplicatedInternalArrayWithAsterisk() {
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')",
"add_array('data', 'Aloha')",
"add_array('title')",
"copy_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -200,8 +200,8 @@ public void shouldNotLookupCopiedInternalArrayWithAsterisk() {
@Test
public void shouldLookupCopiedInternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha')",
"set_array('title')",
"add_array('data', 'Aloha')",
"add_array('title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -222,9 +222,9 @@ public void shouldLookupCopiedInternalArrayWithAsteriskExplicitAppend() {
public void shouldNotLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha', 'Aloha')",
"add_array('data', 'Aloha', 'Aloha')",
"uniq('data')",
"set_array('title')",
"add_array('title')",
"copy_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -241,9 +241,9 @@ public void shouldNotLookupCopiedDeduplicatedInternalArrayWithAsterisk() {
@Test
public void shouldLookupCopiedDeduplicatedInternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('data', 'Aloha', 'Aloha')",
"add_array('data', 'Aloha', 'Aloha')",
"uniq('data')",
"set_array('title')",
"add_array('title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -263,7 +263,7 @@ public void shouldLookupCopiedDeduplicatedInternalArrayWithAsteriskExplicitAppen
@Test
public void shouldLookupCopiedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"add_array('title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -285,7 +285,7 @@ public void shouldLookupCopiedExternalArrayWithAsteriskExplicitAppend() {
public void shouldLookupCopiedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"add_array('title')",
"copy_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -309,7 +309,7 @@ public void shouldNotLookupMovedDeduplicatedExternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"add_array('title')",
"move_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -329,7 +329,7 @@ public void shouldNotLookupMovedDeduplicatedExternalArrayWithAsterisk() {
public void shouldLookupMovedDeduplicatedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uniq('data')",
"set_array('title')",
"add_array('title')",
"move_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -351,7 +351,7 @@ public void shouldLookupMovedDeduplicatedExternalArrayWithAsteriskExplicitAppend
public void shouldNotLookupMovedExternalArrayWithAsterisk() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"add_array('title')",
"move_field('data', 'title')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand All @@ -369,7 +369,7 @@ public void shouldNotLookupMovedExternalArrayWithAsterisk() {
@Test
public void shouldLookupMovedExternalArrayWithAsteriskExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('title')",
"add_array('title')",
"move_field('data', 'title.$append')",
LOOKUP + " Aloha: Alohaeha)"
),
Expand Down Expand Up @@ -994,9 +994,9 @@ public void shouldLookupInCopiedNestedArraysCreatedWithPrepend() {
private void shouldLookupInCopiedNestedArraysCreatedWith(final String reservedField) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"put_map('rswk-indicator', s: 'SubjectHeading')",
"set_array('subject[]')",
"set_array('subject[]." + reservedField + ".componentList[]')",
"set_array('subject[].$last.componentList[]." + reservedField + ".type[]')",
"add_array('subject[]')",
"add_array('subject[]." + reservedField + ".componentList[]')",
"add_array('subject[].$last.componentList[]." + reservedField + ".type[]')",
"do list(path: 'D', 'var': '$i')",
" copy_field('$i', 'subject[].$last.componentList[].$last.type[]." + reservedField + "')",
"end",
Expand Down Expand Up @@ -1247,7 +1247,7 @@ public void shouldExplicitLookupRdfUrlWithRedirection() {
@Test // Scenario 1
public void shouldLookupInExternalRdfMapGetObjectOfSubjectWithTargetedPredicateOfSpecificLanguage() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('prefLabel', 'https://w3id.org/kim/hochschulfaechersystematik/n4')",
"add_array('prefLabel', 'https://w3id.org/kim/hochschulfaechersystematik/n4')",
"put_rdfmap('" + RDF_MAP + "', 'rdfmap', target: 'skos:prefLabel', select_language: 'de')",
"lookup('prefLabel.*', 'rdfmap')"
),
Expand All @@ -1266,7 +1266,7 @@ public void shouldLookupInExternalRdfMapGetObjectOfSubjectWithTargetedPredicateO
@Test // Scenario 2
public void shouldLookupInExternalRdfMapGetSubjectWithTargetedPredicateOfSpecificLanguage() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('id', 'Mathematics, Natural Sciences')",
"add_array('id', 'Mathematics, Natural Sciences')",
"put_rdfmap('" + RDF_MAP + "', 'rdfmap', target: 'skos:prefLabel', select_language: 'en')",
"lookup('id.*', 'rdfmap')"
),
Expand Down Expand Up @@ -1408,7 +1408,7 @@ public void shouldLookupRdfDefinedPropertyToSubject() {

private void shouldLookupInExternalRdfMapGetObjectWithTargetedPredicateOfSpecificLanguage(final String target) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('prefLabel', 'Mathematics, Natural Sciences')",
"add_array('prefLabel', 'Mathematics, Natural Sciences')",
"put_rdfmap('" + RDF_MAP + "', 'rdfmap', target: '" + target + "', select_language: 'de')",
"lookup('prefLabel.*', 'rdfmap')"
),
Expand Down
Loading

0 comments on commit aea03a8

Please sign in to comment.