diff --git a/README.md b/README.md index 287a9602..4f42f1ce 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,22 @@ put_vars( [Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+put_vars+{") +##### `to_var` + +Defines a single global variable that can be referenced with `$[]` and assigns the value of the ``. + +```perl +to_var("", "") +``` + +Options: + +- `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) + +[Example in Playground](https://metafacture.org/playground/?example=to_var) + +[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+to_var+{") + #### Record-level functions ##### `add_field` diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 4ae2ac32..da8ce7b3 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -123,6 +123,13 @@ public void apply(final Metafix metafix, final Record record, final List metafix.getVars().putAll(options); } }, + to_var { + @Override + public void apply(final Metafix metafix, final Record record, final List params, final Map options) { + final Value value = record.get(params.get(0)); + metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.asString()); + } + }, // RECORD-LEVEL METHODS: @@ -687,6 +694,7 @@ public void apply(final Metafix metafix, final Record record, final List private static final String FILEMAP_SEPARATOR_OPTION = "sep_char"; private static final String FILEMAP_DEFAULT_SEPARATOR = ","; + private static final String DEFAULT_OPTION = "default"; private static final String ERROR_STRING_OPTION = "error_string"; private static final Random RANDOM = new Random(); diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java index 8eca36ae..8b167257 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java @@ -4084,4 +4084,114 @@ public void shouldTransformStringToBase64() { ); } + @Test // checkstyle-disable-line JavaNCSS + public void shouldCreateVariableFromLiteralValue() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "to_var('data.title', 'testVar')", + "add_field('testResult', 'This is a $[testVar]')" + ), + i -> { + i.startRecord("1"); + i.startEntity("data"); + i.literal("title", "test"); + i.endEntity(); + i.endRecord(); + i.startRecord("2"); + i.endRecord(); + i.startRecord("3"); + i.startEntity("data"); + i.literal("title", "final-test"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("data"); + o.get().literal("title", "test"); + o.get().endEntity(); + o.get().literal("testResult", "This is a test"); + o.get().endRecord(); + o.get().startRecord("2"); + o.get().literal("testResult", "This is a "); + o.get().endRecord(); + o.get().startRecord("3"); + o.get().startEntity("data"); + o.get().literal("title", "final-test"); + o.get().endEntity(); + o.get().literal("testResult", "This is a final-test"); + o.get().endRecord(); + } + ); + } + + @Test + public void shouldCreateVariableFromLiteralValueWithDefault() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "to_var('data.title', 'testVar', 'default': 'n/a')", + "add_field('testResult', 'This is a $[testVar]')" + ), + i -> { + i.startRecord("1"); + i.startEntity("data"); + i.literal("title", "test"); + i.endEntity(); + i.endRecord(); + i.startRecord("2"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("data"); + o.get().literal("title", "test"); + o.get().endEntity(); + o.get().literal("testResult", "This is a test"); + o.get().endRecord(); + o.get().startRecord("2"); + o.get().literal("testResult", "This is a n/a"); + o.get().endRecord(); + } + ); + } + + @Test + public void shouldNotCreateVariableFromArrayValue() { + MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () -> + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "to_var('data.title', 'testVar')" + ), + i -> { + i.startRecord("1"); + i.startEntity("data"); + i.literal("title", "test1"); + i.literal("title", "test2"); + i.endEntity(); + i.endRecord(); + }, + o -> { + } + ) + ); + } + + @Test + public void shouldNotCreateVariableFromHashValue() { + MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Hash", () -> + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "to_var('data.title', 'testVar')" + ), + i -> { + i.startRecord("1"); + i.startEntity("data"); + i.startEntity("title"); + i.literal("key", "value"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, + o -> { + } + ) + ); + } + }