Skip to content

Commit

Permalink
Account for missing values in to_var() Fix function. (#282)
Browse files Browse the repository at this point in the history
Also, test edge cases.
  • Loading branch information
blackwinter committed Sep 24, 2024
1 parent 726d0fa commit 22e0482
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
9 changes: 2 additions & 7 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,11 @@ public void apply(final Metafix metafix, final Record record, final List<String>
metafix.getVars().putAll(options);
}
},

to_var {
@Override

public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String fieldName = params.get(0);
final String variableName = params.get(1);
final String variableValue = record.get(fieldName).toString();

metafix.getVars().put(variableName, variableValue);
final Value value = record.get(params.get(0));
metafix.getVars().put(params.get(1), Value.isNull(value) ? "" : value.toString());
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4098,11 +4098,26 @@ public void shouldCreateVariableFromLiteralValue() {
i.endRecord();
i.startRecord("2");
i.startEntity("data");
i.literal("title", "second-test");
i.literal("title", "test1");
i.literal("title", "test2");
i.endEntity();
i.endRecord();
i.startRecord("3");
i.startEntity("data");
i.startEntity("title");
i.literal("key", "value");
i.endEntity();
i.endEntity();
i.endRecord();
i.startRecord("4");
i.endRecord();
i.startRecord("5");
i.startEntity("data");
i.literal("title", "final-test");
i.endEntity();
i.endRecord();
},
o -> {
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().literal("title", "test");
Expand All @@ -4111,9 +4126,26 @@ public void shouldCreateVariableFromLiteralValue() {
o.get().endRecord();
o.get().startRecord("2");
o.get().startEntity("data");
o.get().literal("title", "second-test");
o.get().literal("title", "test1");
o.get().literal("title", "test2");
o.get().endEntity();
o.get().literal("testResult", "This is a [test1, test2]");
o.get().endRecord();
o.get().startRecord("3");
o.get().startEntity("data");
o.get().startEntity("title");
o.get().literal("key", "value");
f.apply(2).endEntity();
o.get().literal("testResult", "This is a {key=value}");
o.get().endRecord();
o.get().startRecord("4");
o.get().literal("testResult", "This is a ");
o.get().endRecord();
o.get().startRecord("5");
o.get().startEntity("data");
o.get().literal("title", "final-test");
o.get().endEntity();
o.get().literal("testResult", "This is a second-test");
o.get().literal("testResult", "This is a final-test");
o.get().endRecord();
}
);
Expand Down

0 comments on commit 22e0482

Please sign in to comment.