Skip to content

add parseHex and parseUuid spel functions to return byte arrays #171

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions docs/guide/src/docs/asciidoc/import.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ The `replicate` command exposes 2 command objects named `source` and `target`.

`geo`:: Convenience function that takes a longitude and a latitude to produce a RediSearch geo-location string in the form `longitude,latitude` (e.g. `location=#geo(lon,lat)`)

`parseHex`:: Parse a hexadecimal string to a byte array.

`parseUuid`:: Parse a UUID string to a byte array.

.Processor example
[source,console]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class EvaluationContextArgs {
public static final String VAR_DATE = "date";
public static final String VAR_NUMBER = "number";
public static final String VAR_FAKER = "faker";
public static final String PARSE_HEX = "parseHex";
public static final String PARSE_UUID = "parseUuid";

@Option(arity = "1..*", names = "--var", description = "SpEL expressions for context variables, in the form var=\"exp\". For details see https://docs.spring.io/spring-framework/reference/core/expressions.html", paramLabel = "<v=exp>")
private Map<String, Expression> varExpressions = new LinkedHashMap<>();
Expand All @@ -39,6 +41,9 @@ public class EvaluationContextArgs {
public StandardEvaluationContext evaluationContext() {
StandardEvaluationContext context = new StandardEvaluationContext();
RiotUtils.registerFunction(context, "geo", GeoLocation.class, "toString", String.class, String.class);
RiotUtils.registerFunction(context, PARSE_HEX, SpelUtils.class, "parseHex", String.class);
RiotUtils.registerFunction(context, PARSE_UUID, SpelUtils.class, "parseUuid", String.class);

context.setVariable(VAR_DATE, new SimpleDateFormat(dateFormat));
context.setVariable(VAR_NUMBER, new DecimalFormat(numberFormat));
context.setVariable(VAR_FAKER, new Faker());
Expand Down
37 changes: 37 additions & 0 deletions plugins/riot/src/main/java/com/redis/riot/SpelUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.redis.riot;

public class SpelUtils {

public static String parseHex(String hexVal) {
if (hexVal == null) {
return null;
}
if (hexVal.startsWith("0x")) {
hexVal = hexVal.substring(2);
}
byte[] bytes = new byte[hexVal.length() / 2];
for (int i = 0; i < bytes.length; i++) {
byte b = (byte) Integer.parseInt(hexVal.substring(2 * i, 2 * i + 2), 16);
bytes[i] = b;
}

return new String(bytes);
}

public static String parseUuid(String uuid) {
if (uuid == null) {
return null;
}

java.util.UUID uuidObj = java.util.UUID.fromString(uuid);
byte[] bytes = new byte[16];
long msb = uuidObj.getMostSignificantBits();
long lsb = uuidObj.getLeastSignificantBits();
for (int i = 0; i < 8; i++) {
bytes[i] = (byte) (msb >>> (8 * (7 - i)));
bytes[8 + i] = (byte) (lsb >>> (8 * (7 - i)));
}

return new String(bytes);
}
}
Loading