Skip to content

Commit

Permalink
[INLONG-11125][SDK] Transform OperatorTools support parseBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyOVO committed Sep 15, 2024
1 parent 6203004 commit bbe2a7a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.sf.jsqlparser.expression.Function;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
Expand Down Expand Up @@ -56,18 +55,18 @@ public Object parse(SourceData sourceData, int rowIndex, Context context) {
if (msgObj == null || lenObj == null) {
return null;
}
String msg = msgObj.toString();
byte[] msgBytes = OperatorTools.parseBytes(msgObj);
int len = Integer.parseInt(lenObj.toString());
switch (len) {
case 0:
case 256:
return DigestUtils.sha256Hex(msg.getBytes(StandardCharsets.UTF_8));
return DigestUtils.sha256Hex(msgBytes);
case 224:
return new DigestUtils(SHA_224).digestAsHex(msg.getBytes(StandardCharsets.UTF_8));
return new DigestUtils(SHA_224).digestAsHex(msgBytes);
case 384:
return DigestUtils.sha384Hex(msg.getBytes(StandardCharsets.UTF_8));
return DigestUtils.sha384Hex(msgBytes);
case 512:
return DigestUtils.sha512Hex(msg.getBytes(StandardCharsets.UTF_8));
return DigestUtils.sha512Hex(msgBytes);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import net.sf.jsqlparser.expression.Function;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;

/**
* ShaFunction
* description: sha(string): Compute the SHA-1 160 bit checksum of a string.
Expand All @@ -48,7 +46,6 @@ public Object parse(SourceData sourceData, int rowIndex, Context context) {
if (msgObj == null) {
return null;
}
String msg = msgObj.toString();
return DigestUtils.sha1Hex(msg.getBytes(StandardCharsets.UTF_8));
return DigestUtils.sha1Hex(OperatorTools.parseBytes(msgObj));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Map;
Expand Down Expand Up @@ -148,6 +149,14 @@ public static Timestamp parseTimestamp(Object value) {
}
}

public static byte[] parseBytes(Object value) {
if (value instanceof byte[]) {
return (byte[]) value;
} else {
return (String.valueOf(value)).getBytes(StandardCharsets.UTF_8);
}
}

/**
* compareValue
* @param left
Expand Down

0 comments on commit bbe2a7a

Please sign in to comment.