Skip to content

Commit

Permalink
Add option "url_safe" to "to_base64" function (#384)
Browse files Browse the repository at this point in the history
- add test
- update README
  • Loading branch information
dr0i committed Nov 14, 2024
1 parent 3b3bf14 commit 9014b01
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,12 @@ to_json("<sourceField>"[, pretty: "<boolean>"][, error_string: "<errorValue>"])

Replaces the value with its Base64 encoding.

Options:

-`url_safe`: Whether to encode a URL. (Default: `false`)

```perl
to_base64("<sourceField>")
to_base64("<sourceField>"[, url_safe: "<boolean>"])
```

[Example in Playground](https://metafacture.org/playground/?example=to_base64)
Expand Down
8 changes: 7 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,13 @@ public void apply(final Metafix metafix, final Record record, final List<String>
to_base64 {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
final boolean urlSafe = getBoolean(options, "url_safe");
if (!urlSafe) {
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
}
else {
record.transform(params.get(0), s -> Base64.getUrlEncoder().encodeToString(s.getBytes()));
}
}
},
to_json {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4084,6 +4084,28 @@ public void shouldTransformStringToBase64() {
);
}

@Test
public void shouldTransformUrlToBase64() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"to_base64('data.title', url_safe:'true')"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.literal("title", "https://www.youtube.com/watch?v=daLgsPSvD9A");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().literal("title", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test // checkstyle-disable-line JavaNCSS
public void shouldCreateVariableFromLiteralValue() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

0 comments on commit 9014b01

Please sign in to comment.