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

Commit

Permalink
Only treat field name as FixPath when actually given a path. (#170, 8…
Browse files Browse the repository at this point in the history
…c42874)

IOW, don't split field names received from metadata stream (`Metafix.addValue()`).
  • Loading branch information
blackwinter committed Apr 11, 2022
1 parent 15e2237 commit 1ecbcdf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 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 @@ -99,7 +99,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.add(params.get(0), new Value(params.get(1)));
record.addNested(params.get(0), new Value(params.get(1)));
}
},
array { // array-from-hash
Expand All @@ -111,8 +111,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
record.remove(field);

h.forEach((subField, value) -> {
record.add(field, new Value(subField));
record.add(field, value);
record.addNested(field, new Value(subField));
record.addNested(field, value);
});
})));
}
Expand All @@ -123,7 +123,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(newValue -> {
record.add(newName, newValue);
record.addNested(newName, newValue);
newValue.updatePathRename(newName);
}));
}
Expand Down Expand Up @@ -185,11 +185,11 @@ public void apply(final Metafix metafix, final Record record, final List<String>
});

if (!value.asHash().isEmpty()) {
record.add(field, value);
record.addNested(field, value);
}
else {
for (int i = 1; i <= m.groupCount(); i = i + 1) {
record.add(field, new Value(m.group(i)));
record.addNested(field, new Value(m.group(i)));
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions metafix/src/main/java/org/metafacture/metafix/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,14 @@ public void add(final String field, final Value newValue) {
super.add(field, newValue);
}
else {
final FixPath fixPath = new FixPath(field);
if (fixPath.size() > 1) {
fixPath.insertInto(this, InsertMode.APPEND, newValue);
}
else {
put(field, newValue);
}
put(field, newValue);
}
}

public void addNested(final String field, final Value newValue) {
new FixPath(field).insertInto(this, InsertMode.APPEND, newValue);
}

/**
* Sets a field/value pair to this record, replacing
* any previous association of the field with a value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2694,4 +2694,43 @@ public void shouldRenameArrayFieldWithAsterisk() {
);
}

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void shouldNotSplitLiteralName() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.startRecord("1");
i.literal("123. ", "foo");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("123. ", "foo");
o.get().endRecord();
}
);
}

@Test
public void shouldNotSplitEntityName() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.startRecord("1");
i.startEntity("123. ");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("123. ");
o.get().endEntity();
o.get().endRecord();
}
);
}

}

0 comments on commit 1ecbcdf

Please sign in to comment.