Skip to content

Commit

Permalink
Merge pull request #355 from metafacture/354-base64
Browse files Browse the repository at this point in the history
Add to_base64 function #354
  • Loading branch information
TobiasNx authored Jun 4, 2024
2 parents f4f0900 + aa8f647 commit c403afa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,16 @@ to_json("<sourceField>"[, pretty: "<boolean>"][, error_string: "<errorValue>"])

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+to_json+{")

##### `to_base64`

Replaces the value with its Base64 encoding.

```perl
to_base64("<sourceField>")
```

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+to_base64+{")

##### `trim`

Deletes whitespace at the beginning and the end of a field value.
Expand Down
7 changes: 7 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -626,6 +627,12 @@ 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()));
}
},
to_json {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4062,4 +4062,26 @@ public void shouldUriEncodePathSegmentWithoutSafeChars() {
);
}

@Test
public void shouldTransformStringToBase64() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"to_base64('data.title')"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.literal("title", "this-is-a-test");
i.endEntity();
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().literal("title", "dGhpcy1pcy1hLXRlc3Q=");
o.get().endEntity();
o.get().endRecord();
}
);
}

}

0 comments on commit c403afa

Please sign in to comment.