From c09581a587b1c83fc73f3c7b97fd8342e6e7eb81 Mon Sep 17 00:00:00 2001 From: jimpil Date: Wed, 17 Jan 2024 22:03:06 +0000 Subject: [PATCH] improve mongo-id --- src/mongo_driver_3/data_literals.clj | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/mongo_driver_3/data_literals.clj b/src/mongo_driver_3/data_literals.clj index e5aa377..70a31fc 100644 --- a/src/mongo_driver_3/data_literals.clj +++ b/src/mongo_driver_3/data_literals.clj @@ -8,18 +8,27 @@ (defmethod print-method ObjectId [^ObjectId c ^Writer w] (.write w ^String (str "#mongo/id \"" (.toHexString c) "\""))) (defmethod print-dup ObjectId [^ObjectId c ^Writer w] (.write w ^String (str "#mongo/id \"" (.toHexString c) "\""))) +(defprotocol AsObjectId + (oid-from [this])) + +(extend-protocol AsObjectId + (Class/forName "[B") + (oid-from [this] (ObjectId. ^bytes this)) + nil + (oid-from [_] (ObjectId.)) + String + (oid-from [this] (ObjectId. this)) + Date + (oid-from [this] (ObjectId. this)) + ByteBuffer + (oid-from [this] (ObjectId. this)) + ) + + + (defn mongo-id ;; https://mongodb.github.io/mongo-java-driver/4.8/apidocs/bson/org/bson/types/ObjectId.html - (^ObjectId [] (ObjectId.)) - (^ObjectId [o] - (cond - (string? o) (ObjectId. ^String o) - (bytes? o) (ObjectId. ^bytes o) - (instance? Date o) (ObjectId. ^Date o) - (instance? ByteBuffer o) (ObjectId. ^ByteBuffer o) - :else - (throw - (IllegalArgumentException. - (str "Can not construct an ObjectId from class: " (type o)))))) + (^ObjectId [] (ObjectId.)) + (^ObjectId [o] (oid-from o)) ([o1 o2] (if (and (int? o1) (int? o2))