Skip to content

Commit 46d1ef5

Browse files
committed
Implement optional default value for to_var() Fix function. (#365)
1 parent a52dc9b commit 46d1ef5

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
127127
@Override
128128
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
129129
final Value value = record.get(params.get(0));
130-
metafix.getVars().put(params.get(1), Value.isNull(value) ? "" : value.toString());
130+
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.toString());
131131
}
132132
},
133133

@@ -694,6 +694,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
694694
private static final String FILEMAP_SEPARATOR_OPTION = "sep_char";
695695
private static final String FILEMAP_DEFAULT_SEPARATOR = ",";
696696

697+
private static final String DEFAULT_OPTION = "default";
697698
private static final String ERROR_STRING_OPTION = "error_string";
698699

699700
private static final Random RANDOM = new Random();

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,8 +4087,8 @@ public void shouldTransformStringToBase64() {
40874087
@Test // checkstyle-disable-line JavaNCSS
40884088
public void shouldCreateVariableFromLiteralValue() {
40894089
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4090-
"to_var('data.title','testVar')",
4091-
"add_field('testResult','This is a $[testVar]')"
4090+
"to_var('data.title', 'testVar')",
4091+
"add_field('testResult', 'This is a $[testVar]')"
40924092
),
40934093
i -> {
40944094
i.startRecord("1");
@@ -4151,4 +4151,33 @@ public void shouldCreateVariableFromLiteralValue() {
41514151
);
41524152
}
41534153

4154+
@Test
4155+
public void shouldCreateVariableFromLiteralValueWithDefault() {
4156+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4157+
"to_var('data.title', 'testVar', 'default': 'n/a')",
4158+
"add_field('testResult', 'This is a $[testVar]')"
4159+
),
4160+
i -> {
4161+
i.startRecord("1");
4162+
i.startEntity("data");
4163+
i.literal("title", "test");
4164+
i.endEntity();
4165+
i.endRecord();
4166+
i.startRecord("2");
4167+
i.endRecord();
4168+
},
4169+
o -> {
4170+
o.get().startRecord("1");
4171+
o.get().startEntity("data");
4172+
o.get().literal("title", "test");
4173+
o.get().endEntity();
4174+
o.get().literal("testResult", "This is a test");
4175+
o.get().endRecord();
4176+
o.get().startRecord("2");
4177+
o.get().literal("testResult", "This is a n/a");
4178+
o.get().endRecord();
4179+
}
4180+
);
4181+
}
4182+
41544183
}

0 commit comments

Comments
 (0)