Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Replace existing values in set_array and set_hash (#102, #111)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Feb 28, 2022
1 parent 425dc49 commit 1a3278a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
12 changes: 6 additions & 6 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ public void apply(final Metafix metafix, final Record record, final List<String>
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String joinChar = options.get("join_char");
new FixPath(params.get(0)).replaceIn(record, params.subList(1, params.size()).stream()
new FixPath(params.get(0)).replaceIn(record, new Value(params.subList(1, params.size()).stream()
.filter(f -> literalString(f) || new FixPath(f).findIn(record) != null)
.map(f -> literalString(f) ? new Value(f.substring(1)) : Value.asList(new FixPath(f).findIn(record), null).asArray().get(0))
.map(Value::asString).collect(Collectors.joining(joinChar != null ? joinChar : " ")));
.map(Value::asString).collect(Collectors.joining(joinChar != null ? joinChar : " "))));
}

private boolean literalString(final String s) {
Expand All @@ -211,7 +211,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final String field = params.get(0);
final int max = getInteger(params, 1);

new FixPath(field).replaceIn(record, String.valueOf(RANDOM.nextInt(max)));
new FixPath(field).replaceIn(record, new Value(String.valueOf(RANDOM.nextInt(max))));
}
},
reject {
Expand Down Expand Up @@ -268,21 +268,21 @@ 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 field = params.get(0);
final Value newValue = newArray(params.subList(1, params.size()).stream().map(Value::new));
new FixPath(field).insertInto(record, null, newValue);
new FixPath(field).replaceIn(record, newValue);
}
},
set_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).replaceIn(record, params.get(1));
new FixPath(params.get(0)).replaceIn(record, new Value(params.get(1)));
}
},
set_hash {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String field = params.get(0);
final Value newValue = Value.newHash(h -> options.forEach((f, v) -> h.put(f, new Value(v))));
new FixPath(field).insertInto(record, null, newValue);
new FixPath(field).replaceIn(record, newValue);
}
},
vacuum {
Expand Down
4 changes: 2 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ private Value findInValue(final Value value, final String[] p) {
);
}

public Value replaceIn(final Hash hash, final String newValue) {
return new FixPath(path).insertInto(hash, InsertMode.REPLACE, new Value(newValue));
public Value replaceIn(final Hash hash, final Value newValue) {
return new FixPath(path).insertInto(hash, InsertMode.REPLACE, newValue);
}

public Value appendIn(final Hash hash, final String newValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,30 @@ public void setArray() {
});
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/111
public void setArrayReplaceExisting() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('foo[]','a','b','c')"),
i -> {
i.startRecord("1");
i.startEntity("foo[]");
i.literal("1", "A");
i.literal("2", "B");
i.literal("3", "C");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().startEntity("foo[]");
o.get().literal("1", "a");
o.get().literal("2", "b");
o.get().literal("3", "c");
o.get().endEntity();
o.get().endRecord();
});
}

@Test
public void setHash() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand All @@ -1677,6 +1701,28 @@ public void setHash() {
});
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/111
public void setHashReplaceExisting() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_hash('foo','a': 'b','c': 'd')"),
i -> {
i.startRecord("1");
i.startEntity("foo");
i.literal("a", "B");
i.literal("c", "D");
i.endEntity();
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().startEntity("foo");
o.get().literal("a", "b");
o.get().literal("c", "d");
o.get().endEntity();
o.get().endRecord();
});
}

@Test
public void paste() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

0 comments on commit 1a3278a

Please sign in to comment.