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 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 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
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`: Perform URL-safe encoding (uses Base64URL format). (Default: `false`)

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

[Example in Playground](https://metafacture.org/playground/?example=to_base64)
Expand Down
5 changes: 4 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,10 @@ 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");
final Base64.Encoder encoder = urlSafe ? Base64.getUrlEncoder() : Base64.getEncoder();

record.transform(params.get(0), s -> encoder.encodeToString(s.getBytes()));
}
},
to_json {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
@ExtendWith(MetafixToDo.Extension.class)
public class MetafixMethodTest {

private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=daLgsPSvD9A";
blackwinter marked this conversation as resolved.
Show resolved Hide resolved

@Mock
private StreamReceiver streamReceiver;

Expand Down Expand Up @@ -4084,6 +4086,37 @@ public void shouldTransformStringToBase64() {
);
}

@Test
public void shouldTransformUrlSafeToBase64() {
urlToBase64(",url_safe:'true'", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==");
}

@Test
public void shouldTransformNotUrlSafeToBase64AsDefault() {
urlToBase64("", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kYUxnc1BTdkQ5QQ==");
}

private void urlToBase64(final String option, final String expected) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"to_base64('data.title'" + option + ")"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.literal("title", YOUTUBE_URL);
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().literal("title", expected);
o.get().endEntity();
o.get().endRecord();
}
);
}

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