lib/mongodb: allow new MongoObjectId instances
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 8 Aug 2016 16:44:57 +0000 (12:44 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 8 Aug 2016 18:07:04 +0000 (14:07 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/mongodb/mongodb.nit
lib/mongodb/native_mongodb.nit

index cd203e4..80c4d4c 100644 (file)
@@ -162,9 +162,13 @@ end
 # Since the MongoDB notation is not JSON complient, the mongoc wrapper uses
 # a JSON based notation like `{"$oid": "hash"}`.
 # This is the notation returned by the `to_json` service.
-private class MongoObjectId
+class MongoObjectId
 
-       var native: BSONObjectId
+       private var native: BSONObjectId = new BSONObjectId
+
+       private init with_native(native: BSONObjectId) do
+               self.native = native
+       end
 
        # The unique ID as an MongoDB Object ID string.
        fun id: String do return native.id
@@ -270,7 +274,7 @@ class MongoClient
        private fun last_id: nullable MongoObjectId do
                var last_id = sys.last_mongoc_id
                if last_id == null then return null
-               return new MongoObjectId(last_id)
+               return new MongoObjectId.with_native(last_id)
        end
 
        # Set the last generated id or `null` to unset once used.
index e1ec0b4..ba9b5e3 100644 (file)
@@ -114,12 +114,23 @@ end
 # * a 2-byte process id (Big Endian), and
 # * a 3-byte counter (Big Endian), starting with a random value.
 extern class BSONObjectId `{ bson_oid_t * `}
+
+       # Generates a new `bson_oid_t`.
+       new `{
+               bson_oid_t *self = malloc(sizeof(bson_oid_t));
+               bson_oid_init(self, NULL);
+               return self;
+       `}
+
        # Object id.
        fun id: String import NativeString.to_s_with_copy `{
                char str[25];
                bson_oid_to_string(self, str);
                return NativeString_to_s_with_copy(str);
        `}
+
+       # Destroy `self`.
+       fun destroy `{ free(self); `}
 end
 
 redef class Sys