-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/1.0.0-preview.20240207' into master/1.x
- Loading branch information
Showing
16 changed files
with
301 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
jackson-jq-extra/src/main/java/net/thisptr/jackson/jq/extra/functions/Uuid35Function.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.thisptr.jackson.jq.extra.functions; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import net.thisptr.jackson.jq.Expression; | ||
import net.thisptr.jackson.jq.Function; | ||
import net.thisptr.jackson.jq.PathOutput; | ||
import net.thisptr.jackson.jq.Scope; | ||
import net.thisptr.jackson.jq.Version; | ||
import net.thisptr.jackson.jq.exception.JsonQueryException; | ||
import net.thisptr.jackson.jq.exception.JsonQueryTypeException; | ||
import net.thisptr.jackson.jq.extra.internal.misc.Preconditions; | ||
import net.thisptr.jackson.jq.extra.internal.misc.UuidUtils; | ||
import net.thisptr.jackson.jq.path.Path; | ||
|
||
public class Uuid35Function implements Function { | ||
private final int uuidVersion; | ||
|
||
public Uuid35Function(int uuidVersion) { | ||
this.uuidVersion = uuidVersion; | ||
} | ||
|
||
@Override | ||
public void apply(Scope scope, List<Expression> args, JsonNode in, Path path, PathOutput output, Version version) throws JsonQueryException { | ||
Preconditions.checkInputType("uuid5", in, JsonNodeType.STRING, JsonNodeType.BINARY); | ||
|
||
args.get(0).apply(scope, in, (namespaceArg) -> { | ||
if (!namespaceArg.isTextual()) | ||
throw new JsonQueryTypeException("namespace must be string, but got: %s", namespaceArg.getNodeType()); | ||
UUID namespace; | ||
try { | ||
namespace = UUID.fromString(namespaceArg.asText()); | ||
} catch (IllegalArgumentException e) { | ||
throw new JsonQueryException("namespace must be a valid UUID", e); | ||
} | ||
|
||
UUID uuid; | ||
if (in.isBinary()) { | ||
try { | ||
uuid = UuidUtils.uuid3or5(namespace, in.binaryValue(), this.uuidVersion); | ||
} catch (IOException e) { | ||
throw new JsonQueryException(e); | ||
} | ||
} else { | ||
uuid = UuidUtils.uuid3or5(namespace, in.asText().getBytes(StandardCharsets.UTF_8), this.uuidVersion); | ||
} | ||
|
||
output.emit(new TextNode(uuid.toString()), null); | ||
}); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
jackson-jq-extra/src/main/java/net/thisptr/jackson/jq/extra/internal/misc/UuidUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package net.thisptr.jackson.jq.extra.internal.misc; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.UUID; | ||
|
||
public class UuidUtils { | ||
|
||
public static UUID uuid3(final UUID namespace, final byte[] name) { | ||
return uuid3or5(namespace, name, 3); | ||
} | ||
|
||
public static UUID uuid5(final UUID namespace, final byte[] name) { | ||
return uuid3or5(namespace, name, 5); | ||
} | ||
|
||
public static UUID uuid3or5(final UUID namespace, final byte[] name, final int version) { | ||
// https://datatracker.ietf.org/doc/html/rfc4122#section-4.3 | ||
MessageDigest md; | ||
try { | ||
switch (version) { | ||
case 3: | ||
md = MessageDigest.getInstance("MD5"); | ||
break; | ||
case 5: | ||
md = MessageDigest.getInstance("SHA-1"); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("invalid version"); | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
md.update(toBytes(namespace)); | ||
md.update(name); | ||
byte[] hash = md.digest(); | ||
|
||
// put in the variant and version bits | ||
hash[6] &= (byte) 0b0000_1111; | ||
hash[6] |= (byte) (version << 4); | ||
hash[8] &= (byte) 0b0011_1111; | ||
hash[8] |= (byte) 0b1000_0000; | ||
return fromBytes(hash); | ||
} | ||
|
||
public static byte[] toBytes(UUID uuid) { | ||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]); | ||
bb.putLong(uuid.getMostSignificantBits()); | ||
bb.putLong(uuid.getLeastSignificantBits()); | ||
return bb.array(); | ||
} | ||
|
||
public static UUID fromBytes(byte[] bytes) { | ||
ByteBuffer bb = ByteBuffer.wrap(bytes); | ||
long mostSigBits = bb.getLong(); | ||
long leastSigBits = bb.getLong(); | ||
return new UUID(mostSigBits, leastSigBits); | ||
} | ||
} |
Oops, something went wrong.