Skip to content

Commit 93afd59

Browse files
committed
Merge #386 from remote-tracking branch 'ssh/384-encodeUrlToBase64'
2 parents 3b3bf14 + 8fe3804 commit 93afd59

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-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`: Perform URL-safe encoding (uses Base64URL format). (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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ 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+
final Base64.Encoder encoder = urlSafe ? Base64.getUrlEncoder() : Base64.getEncoder();
685+
686+
record.transform(params.get(0), s -> encoder.encodeToString(s.getBytes()));
684687
}
685688
},
686689
to_json {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
@ExtendWith(MetafixToDo.Extension.class)
3737
public class MetafixMethodTest {
3838

39+
private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=daLgsPSvD9A";
40+
3941
@Mock
4042
private StreamReceiver streamReceiver;
4143

@@ -4084,6 +4086,37 @@ public void shouldTransformStringToBase64() {
40844086
);
40854087
}
40864088

4089+
@Test
4090+
public void shouldTransformUrlSafeToBase64() {
4091+
urlToBase64(",url_safe:'true'", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g_dj1kYUxnc1BTdkQ5QQ==");
4092+
}
4093+
4094+
@Test
4095+
public void shouldTransformNotUrlSafeToBase64AsDefault() {
4096+
urlToBase64("", "aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kYUxnc1BTdkQ5QQ==");
4097+
}
4098+
4099+
private void urlToBase64(final String option, final String expected) {
4100+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4101+
"to_base64('data.title'" + option + ")"
4102+
),
4103+
i -> {
4104+
i.startRecord("1");
4105+
i.startEntity("data");
4106+
i.literal("title", YOUTUBE_URL);
4107+
i.endEntity();
4108+
i.endRecord();
4109+
},
4110+
o -> {
4111+
o.get().startRecord("1");
4112+
o.get().startEntity("data");
4113+
o.get().literal("title", expected);
4114+
o.get().endEntity();
4115+
o.get().endRecord();
4116+
}
4117+
);
4118+
}
4119+
40874120
@Test // checkstyle-disable-line JavaNCSS
40884121
public void shouldCreateVariableFromLiteralValue() {
40894122
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

0 commit comments

Comments
 (0)