Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit 9014b01

Browse files
committed
Add option "url_safe" to "to_base64" function (#384)
- add test - update README
1 parent 3b3bf14 commit 9014b01

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,12 @@ to_json("<sourceField>"[, pretty: "<boolean>"][, error_string: "<errorValue>"])
905905

906906
Replaces the value with its Base64 encoding.
907907

908+
Options:
909+
910+
-`url_safe`: Whether to encode a URL. (Default: `false`)
911+
908912
```perl
909-
to_base64("<sourceField>")
913+
to_base64("<sourceField>"[, url_safe: "<boolean>"])
910914
```
911915

912916
[Example in Playground](https://metafacture.org/playground/?example=to_base64)

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,13 @@ public void apply(final Metafix metafix, final Record record, final List<String>
680680
to_base64 {
681681
@Override
682682
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
683-
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
683+
final boolean urlSafe = getBoolean(options, "url_safe");
684+
if (!urlSafe) {
685+
record.transform(params.get(0), s -> Base64.getEncoder().encodeToString(s.getBytes()));
686+
}
687+
else {
688+
record.transform(params.get(0), s -> Base64.getUrlEncoder().encodeToString(s.getBytes()));
689+
}
684690
}
685691
},
686692
to_json {

metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4084,6 +4084,28 @@ public void shouldTransformStringToBase64() {
40844084
);
40854085
}
40864086

4087+
@Test
4088+
public void shouldTransformUrlToBase64() {
4089+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4090+
"to_base64('data.title', url_safe:'true')"
4091+
),
4092+
i -> {
4093+
i.startRecord("1");
4094+
i.startEntity("data");
4095+
i.literal("title", "https://www.youtube.com/watch?v=daLgsPSvD9A");
4096+
i.endEntity();
4097+
i.endRecord();
4098+
},
4099+
o -> {
4100+
o.get().startRecord("1");
4101+
o.get().startEntity("data");
4102+
o.get().literal("title", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==");
4103+
o.get().endEntity();
4104+
o.get().endRecord();
4105+
}
4106+
);
4107+
}
4108+
40874109
@Test // checkstyle-disable-line JavaNCSS
40884110
public void shouldCreateVariableFromLiteralValue() {
40894111
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

0 commit comments

Comments
 (0)