Skip to content

Commit c3b404b

Browse files
committed
Change set_* functions to not create intermediate structures. (#631)
Mirroring Catmandu's behaviour: "will not create the intermediate structures if they are missing".
1 parent 5e5f881 commit c3b404b

File tree

29 files changed

+175
-40
lines changed

29 files changed

+175
-40
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,21 +642,41 @@ retain("<sourceField_1>"[, ...])
642642
643643
##### `set_array`
644644
645-
_Currently alias for [`add_array`](#add_array)._
645+
Creates a new array (with optional values), provided that the intermediate structures (i.e. parent fields) exist.
646646
647-
We advise you to use [`add_array`](#add_array) instead of `set_array` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)
647+
```perl
648+
set_array("<targetFieldName>")
649+
set_array("<targetFieldName>", "<value_1>"[, ...])
650+
```
651+
652+
[Example in Playground](https://metafacture.org/playground/?example=set_array)
653+
654+
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_array+{")
648655
649656
##### `set_field`
650657
651-
_Currently alias for [`add_field`](#add_field)._
658+
Creates a field with a defined value, provided that the intermediate structures (i.e. parent fields) exist.
659+
660+
```perl
661+
set_field("<targetFieldName>", "<fieldValue>")
662+
```
663+
664+
[Example in Playground](https://metafacture.org/playground/?example=set_field)
652665
653-
We advise you to use [`add_field`](#add_field) instead of `set_field` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)
666+
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_field+{")
654667
655668
##### `set_hash`
656669
657-
_Currently alias for [`add_hash`](#add_hash)._
670+
Creates a new hash (with optional values), provided that the intermediate structures (i.e. parent fields) exist.
671+
672+
```perl
673+
set_hash("<targetFieldName>")
674+
set_hash("<targetFieldName>", "subfieldName": "<subfieldValue>"[, ...])
675+
```
676+
677+
[Example in Playground](https://metafacture.org/playground/?example=set_hash)
658678
659-
We advise you to use [`add_hash`](#add_hash) instead of `set_hash` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)
679+
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_hash+{")
660680
661681
##### `timestamp`
662682

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,25 @@ public void apply(final Metafix metafix, final Record record, final List<String>
408408
set_array {
409409
@Override
410410
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
411-
add_array.apply(metafix, record, params, options);
411+
if (parentFieldExists(record, params.get(0))) {
412+
add_array.apply(metafix, record, params, options);
413+
}
412414
}
413415
},
414416
set_field {
415417
@Override
416418
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
417-
add_field.apply(metafix, record, params, options);
419+
if (parentFieldExists(record, params.get(0))) {
420+
add_field.apply(metafix, record, params, options);
421+
}
418422
}
419423
},
420424
set_hash {
421425
@Override
422426
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
423-
add_hash.apply(metafix, record, params, options);
427+
if (parentFieldExists(record, params.get(0))) {
428+
add_hash.apply(metafix, record, params, options);
429+
}
424430
}
425431
},
426432
timestamp {

metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,17 @@ default Stream<Value> flatten(final Stream<Value> stream) {
194194
));
195195
}
196196

197+
/**
198+
* Checks whether the given field's parent field exists in the record.
199+
*
200+
* @param record the record
201+
* @param field the field
202+
*
203+
* @return true if the given field's parent field exists in the record
204+
*/
205+
default boolean parentFieldExists(final Record record, final String field) {
206+
final int index = field.lastIndexOf(Value.FIELD_PATH_SEPARATOR);
207+
return index < 1 || record.containsPath(field.substring(0, index));
208+
}
209+
197210
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,9 @@ public void shouldLookupInCopiedNestedArraysCreatedWithPrepend() {
994994
private void shouldLookupInCopiedNestedArraysCreatedWith(final String reservedField) {
995995
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
996996
"put_map('rswk-indicator', s: 'SubjectHeading')",
997-
"set_array('subject[]')",
998-
"set_array('subject[]." + reservedField + ".componentList[]')",
999-
"set_array('subject[].$last.componentList[]." + reservedField + ".type[]')",
997+
"add_array('subject[]')",
998+
"add_array('subject[]." + reservedField + ".componentList[]')",
999+
"add_array('subject[].$last.componentList[]." + reservedField + ".type[]')",
10001000
"do list(path: 'D', 'var': '$i')",
10011001
" copy_field('$i', 'subject[].$last.componentList[].$last.type[]." + reservedField + "')",
10021002
"end",

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,11 +2818,9 @@ public void addFieldWithReplaceAllArray() {
28182818
private void addFieldWithReplaceAllArray(final boolean replaceAll) {
28192819
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
28202820
"do list(path: 'contribution[]', 'var': '$i')",
2821-
" set_array('$i.agent.altLabel[]')",
28222821
" add_field('$i.agent.altLabel[].$append', 'contribution')",
28232822
"end",
28242823
"do list(path: 'subject[]', 'var': '$i')",
2825-
" set_array('$i.altLabel[]')",
28262824
" add_field('$i.altLabel[].$append', 'subject')",
28272825
"end",
28282826
replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "",
@@ -2875,10 +2873,10 @@ public void setArrayWithReplaceAll() {
28752873
private void setArrayWithReplaceAll(final boolean replaceAll) {
28762874
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
28772875
"do list(path: 'contribution[]', 'var': '$i')",
2878-
" set_array('$i.agent.altLabel[]', 'contribution')",
2876+
" add_array('$i.agent.altLabel[]', 'contribution')",
28792877
"end",
28802878
"do list(path: 'subject[]', 'var': '$i')",
2881-
" set_array('$i.altLabel[]', 'subject')",
2879+
" add_array('$i.altLabel[]', 'subject')",
28822880
"end",
28832881
replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "",
28842882
replaceAll ? "replace_all('subject[].*.altLabel[].*', 't', '')" : ""
@@ -2930,11 +2928,9 @@ public void copyFieldWithReplaceAllArray() {
29302928
private void copyFieldWithReplaceAllArray(final boolean replaceAll) {
29312929
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
29322930
"do list(path: 'contribution[]', 'var': '$i')",
2933-
" set_array('$i.agent.altLabel[]')",
29342931
" copy_field('$i.label', '$i.agent.altLabel[].$append')",
29352932
"end",
29362933
"do list(path: 'subject[]', 'var': '$i')",
2937-
" set_array('$i.altLabel[]')",
29382934
" copy_field('$i.label', '$i.altLabel[].$append')",
29392935
"end",
29402936
replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "",
@@ -2987,11 +2983,9 @@ public void pasteWithReplaceAll() {
29872983
private void pasteWithReplaceAll(final boolean replaceAll) {
29882984
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
29892985
"do list(path: 'contribution[]', 'var': '$i')",
2990-
" set_array('$i.agent.altLabel[]')",
29912986
" paste('$i.agent.altLabel[].$append', '$i.label', '~!')",
29922987
"end",
29932988
"do list(path: 'subject[]', 'var': '$i')",
2994-
" set_array('$i.altLabel[]')",
29952989
" paste('$i.altLabel[].$append', '$i.label', '~!')",
29962990
"end",
29972991
replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', ' !', '')" : "",

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ public void entitiesPassThroughRepeatNestedEntity() {
164164
}
165165

166166
@Test
167-
public void setEmpty() {
167+
public void addEmpty() {
168168
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
169-
"set_field('my.nested.name','patrick')",
170-
"set_field('your.nested.name','nicolas')"),
169+
"add_field('my.nested.name','patrick')",
170+
"add_field('your.nested.name','nicolas')"),
171171
i -> {
172172
i.startRecord("1");
173173
i.endRecord();
@@ -185,6 +185,20 @@ public void setEmpty() {
185185
});
186186
}
187187

188+
@Test
189+
public void setEmpty() {
190+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
191+
"set_field('my.nested.name','patrick')",
192+
"set_field('your.nested.name','nicolas')"),
193+
i -> {
194+
i.startRecord("1");
195+
i.endRecord();
196+
}, (o, f) -> {
197+
o.get().startRecord("1");
198+
o.get().endRecord();
199+
});
200+
}
201+
188202
@Test
189203
public void setExisting() {
190204
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromXml/toJson/lookupInDeeplyNestedArrayOfObjects/test.fix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
put_map("rswk-indicator", s: "SubjectHeading")
22

33
if exists("6890?")
4-
set_array("subject[].$append.type[]", "ComplexSubject")
4+
add_array("subject[].$append.type[]", "ComplexSubject")
55
set_array("subject[].$last.label")
66
set_array("subject[].$last.componentList[]")
77
do list(path: "6890?", "var": "$i")
8-
set_array("subject[].$last.componentList[].$append.type[]")
8+
add_array("subject[].$last.componentList[].$append.type[]")
99
do list(path: "$i.D", "var": "$k")
1010
copy_field("$k", "subject[].$last.componentList[].$last.type[].$append")
1111
end

metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpper/test.fix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
do list(path: "700[01] ", "var": "$i")
2-
set_hash("contribution[].$append.agent")
2+
add_hash("contribution[].$append.agent")
33
copy_field("$i.a", "contribution[].$last.agent.label")
44
end
55

metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpperStrictnessHandlesProcessExceptions/test.fix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
do list(path: "700[01] ", "var": "$i")
2-
set_hash("contribution[].$append.agent")
2+
add_hash("contribution[].$append.agent")
33
copy_field("$i.a", "contribution[].$last.agent.label")
44
end
55

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"animals" : [ {
3+
"name" : "Jake",
4+
"type" : "dog"
5+
}, {
6+
"name" : "Blacky",
7+
"type" : "bird"
8+
} ],
9+
"animals_2" : [ {
10+
"name" : "Jake",
11+
"type" : "dog"
12+
}, {
13+
"name" : "Blacky",
14+
"type" : "bird"
15+
}, {
16+
"test" : [ "test", "test", "test" ]
17+
} ]
18+
}

0 commit comments

Comments
 (0)