Skip to content

Commit

Permalink
Merge #386 from remote-tracking branch 'ssh/384-encodeUrlToBase64'
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Nov 15, 2024
2 parents 3b3bf14 + 8fe3804 commit 93afd59
Show file tree
Hide file tree
Showing 3 changed files with 42 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`: 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";

@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

0 comments on commit 93afd59

Please sign in to comment.