Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 82d36d3

Browse files
authored
Merge pull request #365 from metafacture/282-variableFromElementValue
Add new fix method to set variable with element value #282
2 parents 4453635 + 93ee08e commit 82d36d3

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,22 @@ put_vars(
285285

286286
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+put_vars+{")
287287

288+
##### `to_var`
289+
290+
Defines a single global variable that can be referenced with `$[<variableName>]` and assigns the value of the `<sourceField>`.
291+
292+
```perl
293+
to_var("<sourceField>", "<variableName>")
294+
```
295+
296+
Options:
297+
298+
- `default`: Default value if source field does not exist. The option needs to be written in quotation marks because it is a reserved word in Java. (Default: Empty string)
299+
300+
[Example in Playground](https://metafacture.org/playground/?example=to_var)
301+
302+
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+to_var+{")
303+
288304
#### Record-level functions
289305

290306
##### `add_field`

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ public void apply(final Metafix metafix, final Record record, final List<String>
154154
metafix.getVars().putAll(options);
155155
}
156156
},
157+
to_var {
158+
@Override
159+
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
160+
final Value value = record.get(params.get(0));
161+
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.asString());
162+
}
163+
},
157164

158165
// RECORD-LEVEL METHODS:
159166

@@ -718,6 +725,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
718725
private static final String FILEMAP_SEPARATOR_OPTION = "sep_char";
719726
private static final String FILEMAP_DEFAULT_SEPARATOR = ",";
720727

728+
private static final String DEFAULT_OPTION = "default";
721729
private static final String ERROR_STRING_OPTION = "error_string";
722730

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

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,4 +4084,114 @@ public void shouldTransformStringToBase64() {
40844084
);
40854085
}
40864086

4087+
@Test // checkstyle-disable-line JavaNCSS
4088+
public void shouldCreateVariableFromLiteralValue() {
4089+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4090+
"to_var('data.title', 'testVar')",
4091+
"add_field('testResult', 'This is a $[testVar]')"
4092+
),
4093+
i -> {
4094+
i.startRecord("1");
4095+
i.startEntity("data");
4096+
i.literal("title", "test");
4097+
i.endEntity();
4098+
i.endRecord();
4099+
i.startRecord("2");
4100+
i.endRecord();
4101+
i.startRecord("3");
4102+
i.startEntity("data");
4103+
i.literal("title", "final-test");
4104+
i.endEntity();
4105+
i.endRecord();
4106+
},
4107+
o -> {
4108+
o.get().startRecord("1");
4109+
o.get().startEntity("data");
4110+
o.get().literal("title", "test");
4111+
o.get().endEntity();
4112+
o.get().literal("testResult", "This is a test");
4113+
o.get().endRecord();
4114+
o.get().startRecord("2");
4115+
o.get().literal("testResult", "This is a ");
4116+
o.get().endRecord();
4117+
o.get().startRecord("3");
4118+
o.get().startEntity("data");
4119+
o.get().literal("title", "final-test");
4120+
o.get().endEntity();
4121+
o.get().literal("testResult", "This is a final-test");
4122+
o.get().endRecord();
4123+
}
4124+
);
4125+
}
4126+
4127+
@Test
4128+
public void shouldCreateVariableFromLiteralValueWithDefault() {
4129+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4130+
"to_var('data.title', 'testVar', 'default': 'n/a')",
4131+
"add_field('testResult', 'This is a $[testVar]')"
4132+
),
4133+
i -> {
4134+
i.startRecord("1");
4135+
i.startEntity("data");
4136+
i.literal("title", "test");
4137+
i.endEntity();
4138+
i.endRecord();
4139+
i.startRecord("2");
4140+
i.endRecord();
4141+
},
4142+
o -> {
4143+
o.get().startRecord("1");
4144+
o.get().startEntity("data");
4145+
o.get().literal("title", "test");
4146+
o.get().endEntity();
4147+
o.get().literal("testResult", "This is a test");
4148+
o.get().endRecord();
4149+
o.get().startRecord("2");
4150+
o.get().literal("testResult", "This is a n/a");
4151+
o.get().endRecord();
4152+
}
4153+
);
4154+
}
4155+
4156+
@Test
4157+
public void shouldNotCreateVariableFromArrayValue() {
4158+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () ->
4159+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4160+
"to_var('data.title', 'testVar')"
4161+
),
4162+
i -> {
4163+
i.startRecord("1");
4164+
i.startEntity("data");
4165+
i.literal("title", "test1");
4166+
i.literal("title", "test2");
4167+
i.endEntity();
4168+
i.endRecord();
4169+
},
4170+
o -> {
4171+
}
4172+
)
4173+
);
4174+
}
4175+
4176+
@Test
4177+
public void shouldNotCreateVariableFromHashValue() {
4178+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Hash", () ->
4179+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4180+
"to_var('data.title', 'testVar')"
4181+
),
4182+
i -> {
4183+
i.startRecord("1");
4184+
i.startEntity("data");
4185+
i.startEntity("title");
4186+
i.literal("key", "value");
4187+
i.endEntity();
4188+
i.endEntity();
4189+
i.endRecord();
4190+
},
4191+
o -> {
4192+
}
4193+
)
4194+
);
4195+
}
4196+
40874197
}

0 commit comments

Comments
 (0)