From 449f377c954a71374efba94340968669fa17840d Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Mon, 9 Aug 2021 11:20:25 +0700 Subject: [PATCH] add object id funcs --- object_id.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 object_id.go diff --git a/object_id.go b/object_id.go new file mode 100644 index 0000000..3026c85 --- /dev/null +++ b/object_id.go @@ -0,0 +1,25 @@ +package mongodb + +import "go.mongodb.org/mongo-driver/bson/primitive" + +// NewObjectID generates a new ObjectID based on the timestamp. +func NewObjectID() primitive.ObjectID { + return primitive.NewObjectID() +} + +// NewStringID ... +func NewStringID() string { + return NewObjectID().Hex() +} + +// ConvertFromString ... +func ConvertFromString(s string) (value primitive.ObjectID, isValid bool) { + id, err := primitive.ObjectIDFromHex(s) + return id, err == nil +} + +// IsValid ... +func IsValid(s string) bool { + return primitive.IsValidObjectID(s) +} +