Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new fix method to set variable with element value #282 #365

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `$[<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)

TobiasNx marked this conversation as resolved.
Show resolved Hide resolved
[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 @@ -123,6 +123,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 @@ -687,6 +694,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 -> {
}
)
);
}

}
Loading