Skip to content

Commit

Permalink
Change add_field() Fix function to destructive behaviour for Catman…
Browse files Browse the repository at this point in the history
…du compatibility. (#116)

NOTE: `add_field()` and `set_field()` are now equivalent, but according to Catmandu's documentation `set_field()` "will not create the intermediate structures if they are missing".
  • Loading branch information
blackwinter committed Jun 6, 2023
1 parent b7c318f commit 5bc0378
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
add_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.addNested(params.get(0), new Value(params.get(1)));
record.set(params.get(0), new Value(params.get(1)));
}
},
array { // array-from-hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,9 @@ public void ifInCollectorCombine() {

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

private void shouldIterateOverListOfHashes(final String path, final int expectedCount) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"do list(path: '" + path + "', 'var': '$i')",
" add_field('trace', 'true')",
" add_field('trace.$append', 'true')",
"end",
"retain('trace')"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2416,9 +2416,10 @@ public void shouldMatchString() {
@Test
public void shouldTestMacroVariable() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('type')",
"do put_macro('test')",
" if str_contain('name', 'a$[var]')",
" add_field('type', 'Organization: $[var]')",
" add_field('type.$append', 'Organization: $[var]')",
" end",
"end",
"call_macro('test', 'var': 'm')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,48 @@ public void add() {
i.startRecord("3");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("my");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();

o.get().startRecord("2");
o.get().startEntity("my");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();

o.get().startRecord("3");
o.get().startEntity("my");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void addWithAppendInNewArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('my.name')",
"add_field('my.name.$append','patrick')",
"add_field('my.name.$append','nicolas')"
),
i -> {
i.startRecord("1");
i.endRecord();

i.startRecord("2");
i.startEntity("my");
i.literal("name", "max");
i.endEntity();
i.endRecord();

i.startRecord("3");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("my");
Expand All @@ -246,7 +288,6 @@ public void add() {

o.get().startRecord("2");
o.get().startEntity("my");
o.get().literal("name", "max");
o.get().literal("name", "patrick");
o.get().literal("name", "nicolas");
o.get().endEntity();
Expand Down Expand Up @@ -1992,8 +2033,9 @@ public void arrayFromHash() {
@Test
public void shouldCallMacro() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('test')",
"do put_macro('test')",
" add_field('test', '42')",
" add_field('test.$append', '42')",
"end",
"call_macro('test')",
"call_macro('test')"
Expand Down Expand Up @@ -2030,9 +2072,10 @@ public void shouldNotCallUnknownMacro() {
@Test
public void shouldCallMacroWithVariables() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('test')",
"put_vars(a: '1', b: '2')", // global variables
"do put_macro('test', b: '22', c: '33')", // "static" local variables
" add_field('test', '$[a]-$[b]-$[c]-$[d]')",
" add_field('test.$append', '$[a]-$[b]-$[c]-$[d]')",
"end",
"call_macro('test', c: '333', d: '444')", // "dynamic" local variables
"call_macro('test', b: '555', d: '666')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public void shouldDoNothing() {
public void shouldIncludeFixFile() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_field('trace', 'before include')",
"add_field('trace.$append', 'before include')",
"include('src/test/resources/org/metafacture/metafix/fixes/base.fix')",
"add_field('trace', 'after include')"
"add_field('trace.$append', 'after include')"
),
i -> {
i.startRecord("1");
Expand Down Expand Up @@ -281,13 +281,13 @@ public void shouldNotLeakVariablesFromIncludingFixFile() {
public void shouldIncludeFixFileInBind() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_field('trace', 'before bind')",
"add_field('trace.$append', 'before bind')",
"do list(path: 'data', 'var': '$i')",
" paste('trace.$append', '~before include', '$i')",
" include('src/test/resources/org/metafacture/metafix/fixes/var.fix')",
" paste('trace.$append', '~after include', '$i')",
"end",
"add_field('trace', 'after bind')"
"add_field('trace.$append', 'after bind')"
),
i -> {
i.startRecord("1");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unless exists("trace")
set_array("trace")
end
add_field("trace", "before include")
add_field("trace.$append", "before include")
include("./base.fix")
add_field("trace", "after include")
add_field("trace.$append", "after include")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set_array("trace")
add_field("trace", "before nested")
add_field("trace.$append", "before nested")
include("src/test/resources/org/metafacture/metafix/fixes/include.fix")
add_field("trace", "after nested")
add_field("trace.$append", "after nested")

This file was deleted.

0 comments on commit 5bc0378

Please sign in to comment.