Skip to content

Commit

Permalink
Merge pull request #365 from metafacture/282-variableFromElementValue
Browse files Browse the repository at this point in the history
Add new fix method to set variable with element value #282
  • Loading branch information
TobiasNx authored Oct 14, 2024
2 parents 4453635 + 93ee08e commit 82d36d3
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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 `$[<variableName>]` and assigns the value of the `<sourceField>`.

```perl
to_var("<sourceField>", "<variableName>")
```

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`
Expand Down
8 changes: 8 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ 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 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:

Expand Down Expand Up @@ -718,6 +725,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
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();
Expand Down
110 changes: 110 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
}
)
);
}

}

0 comments on commit 82d36d3

Please sign in to comment.