Skip to content

Commit

Permalink
UUID support in Mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
steffenagger committed Mar 26, 2021
1 parent 90ea5cf commit 0c1f31f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/common/type_deduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GenericTypeDeductionImpl {
{types::Float, "Float"}, {types::Double, "Double"},
{types::Decimal, "Decimal128"}, {types::Boolean, "Boolean"},
{types::ObjectId, "ObjectId"}, {types::Object, "Object"},
{types::UUID, "UUID"}, {types::Object, "Object"},
{types::Undefined, "Undefined"}, {types::Null, "Null"}};
js_to_realm_map = reverse_deduction_types_map();
}
Expand Down Expand Up @@ -105,6 +106,9 @@ class GenericTypeDeductionImpl {
if (Value::is_object_id(context, value)) {
return types::ObjectId;
}
if (Value::is_uuid(context, value)) {
return types::UUID;
}
if (Value::is_object(context, value)) {
return types::Object;
}
Expand Down
12 changes: 12 additions & 0 deletions src/js_mixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ class MixedObjectID : public MixedWrapper<Context, Value> {
}
};

template <typename Context, typename Value, typename Utils>
class MixedUUID : public MixedWrapper<Context, Value> {
Mixed wrap(Context context, Value const &value) {
return Mixed(Utils::to_uuid(context, value));
}

Value unwrap(Context context, Mixed mixed) {
return Utils::from_uuid(context, mixed.get<UUID>());
}
};

template <typename Context, typename Value, typename Utils>
class MixedBinary : public MixedWrapper<Context, Value> {
private:
Expand Down Expand Up @@ -158,6 +169,7 @@ class TypeMixed {
{types::Boolean, new MixedBoolean<Context, Value, Utils>},
{types::Decimal, new MixedDecimal128<Context, Value, Utils>},
{types::ObjectId, new MixedObjectID<Context, Value, Utils>},
{types::UUID, new MixedUUID<Context, Value, Utils>},
{types::Binary, new MixedBinary<Context, Value, Utils>},
{types::Timestamp, new MixedTimeStamp<Context, Value, Utils>},
};
Expand Down
47 changes: 28 additions & 19 deletions tests/js/mixed-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const Realm = require('realm');
const TestCase = require('./asserts');
const AppConfig = require('./support/testConfig');

const {Decimal128, ObjectId} = Realm.BSON;
const {Decimal128, ObjectId, UUID} = Realm.BSON;

const SingleSchema = {
name: 'Mixed',
properties: {
a: 'mixed',
b: 'mixed',
c: 'mixed'
c: 'mixed',
d: 'mixed'
}
}

Expand All @@ -47,30 +48,36 @@ module.exports = {
testMixedComplexTypes() {
let realm = new Realm({schema: [SingleSchema]});
let d128 = Decimal128.fromString('6.022e23');
let id = new ObjectId();
let oid = new ObjectId();
let uuid = new UUID();
let date = new Date();

realm.write(()=> realm.create(SingleSchema.name, { a: id, b:d128, c: date } ))
realm.write(()=> realm.create(SingleSchema.name, { a: oid, b: uuid, c: d128, d: date } ))

let data = realm.objects(SingleSchema.name)[0]
TestCase.assertTrue(typeof data.a === typeof id , 'should be the same type ObjectId');
TestCase.assertEqual(data.a.toString(), id.toString(), 'should have the same content');

TestCase.assertEqual(data.b.toString(), d128.toString(), 'Should be the same Decimal128');
TestCase.assertEqual(data.c.toString(), date.toString(), 'Should be the same Date');
TestCase.assertTrue(typeof data.a === typeof oid , 'should be the same type ObjectId');
TestCase.assertEqual(data.a.toString(), oid.toString(), 'should be the same ObjectId');
TestCase.assertEqual(data.b.toString(), uuid.toString(), 'Should be the same UUID');
TestCase.assertEqual(data.c.toString(), d128.toString(), 'Should be the same Decimal128');
TestCase.assertEqual(data.d.toString(), date.toString(), 'Should be the same Date');
},

testMixedMutability() {
let realm = new Realm({schema: [SingleSchema]});
let d128 = Decimal128.fromString('6.022e23');
let id = new ObjectId();
let oid = new ObjectId();
let uuid = new UUID();
let date = new Date();

realm.write(()=> realm.create(SingleSchema.name, { a: id } ))
realm.write(()=> realm.create(SingleSchema.name, { a: oid }))
let data = realm.objects(SingleSchema.name)[0]

TestCase.assertTrue(typeof data.a === typeof id , 'should be the same type ObjectId');
TestCase.assertEqual(data.a.toString(), id.toString(), 'should have the same content');
TestCase.assertTrue(typeof data.a === typeof oid , 'should be the same type ObjectId');
TestCase.assertEqual(data.a.toString(), oid.toString(), 'should have the same content');

realm.write(()=> data.a = uuid);
TestCase.assertTrue(typeof data.a === typeof uuid , 'should be the same type UUID');
TestCase.assertEqual(data.a.toString(), uuid.toString(), 'should have the same content UUID');

realm.write(()=> data.a = d128 )
TestCase.assertEqual(data.a.toString(), d128.toString(), 'Should be the same Decimal128');
Expand Down Expand Up @@ -124,25 +131,27 @@ module.exports = {
});

realm.write(() => {
realm.create("MixedObject", { _id: new ObjectId(), key: "one", value: 1 });
realm.create("MixedObject", { _id: new ObjectId(), key: "two", value: "2" });
realm.create("MixedObject", { _id: new ObjectId(), key: "three", value: 3.0 });
realm.create("MixedObject", { _id: new ObjectId(), key: "1", value: 1 });
realm.create("MixedObject", { _id: new ObjectId(), key: "2", value: "2" });
realm.create("MixedObject", { _id: new ObjectId(), key: "3", value: 3.0 });
realm.create("MixedObject", { _id: new ObjectId(), key: "4", value: new UUID() });
});

await realm.syncSession.uploadAllLocalChanges();
TestCase.assertEqual(realm.objects("MixedObject").length, 3);
TestCase.assertEqual(realm.objects("MixedObject").length, 4);
realm.close();

Realm.deleteFile(config);

let realm2 = await Realm.open(config);
await realm2.syncSession.downloadAllServerChanges();

let objects = realm2.objects("MixedObject");
TestCase.assertEqual(objects.length, 3);
let objects = realm2.objects("MixedObject").sorted("key");
TestCase.assertEqual(objects.length, 4);
TestCase.assertTrue(typeof objects[0].value, "number");
TestCase.assertTrue(typeof objects[1].value, "string");
TestCase.assertTrue(typeof objects[2].value, "number");
TestCase.assertTrue(objects[3].value instanceof UUID, "UUID");

realm2.close();
}
Expand Down

0 comments on commit 0c1f31f

Please sign in to comment.