Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Add option "url_safe" to "to_base64" function #386

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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`)
dr0i marked this conversation as resolved.
Show resolved Hide resolved

```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()));
}
dr0i marked this conversation as resolved.
Show resolved Hide resolved
}
},
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() {
blackwinter marked this conversation as resolved.
Show resolved Hide resolved
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
Loading