From ac1cdfdc8be6871cdfef933bc44e007b044dec22 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Tue, 27 Oct 2015 10:04:56 -0700 Subject: [PATCH] support null for basic propety types --- src/RJSSchema.mm | 2 +- src/object-store/object_accessor.hpp | 9 +++++++++ tests/ObjectTests.js | 19 +++++++++++++++++-- tests/schemas.js | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/RJSSchema.mm b/src/RJSSchema.mm index c9a4e247..50c504cd 100644 --- a/src/RJSSchema.mm +++ b/src/RJSSchema.mm @@ -58,7 +58,7 @@ static inline Property RJSParseProperty(JSContextRef ctx, JSObjectRef propertyOb JSValueRef optionalValue = JSObjectGetProperty(ctx, propertyObject, optionalString, NULL); if (!JSValueIsUndefined(ctx, optionalValue)) { if (!JSValueIsBoolean(ctx, optionalValue)) { - throw std::runtime_error("Property expected to be of type boolean"); + throw std::runtime_error("'optional' designation expected to be of type boolean"); } prop.is_nullable = JSValueToBoolean(ctx, optionalValue); } diff --git a/src/object-store/object_accessor.hpp b/src/object-store/object_accessor.hpp index 25d79dfd..cd45e8f7 100644 --- a/src/object-store/object_accessor.hpp +++ b/src/object-store/object_accessor.hpp @@ -156,6 +156,11 @@ namespace realm { } size_t column = property.table_column; + if (property.is_nullable && Accessor::is_null(ctx, value)) { + m_row.set_null(column); + return; + } + switch (property.type) { case PropertyTypeBool: m_row.set_bool(column, Accessor::to_bool(ctx, value)); @@ -209,6 +214,10 @@ namespace realm { using Accessor = NativeAccessor; size_t column = property.table_column; + if (property.is_nullable && m_row.is_null(column)) { + return Accessor::null_value(ctx); + } + switch (property.type) { case PropertyTypeBool: return Accessor::from_bool(ctx, m_row.get_bool(column)); diff --git a/tests/ObjectTests.js b/tests/ObjectTests.js index abf413ff..21887319 100644 --- a/tests/ObjectTests.js +++ b/tests/ObjectTests.js @@ -48,17 +48,32 @@ module.exports = BaseTest.extend({ TestCase.assertEqual(object.nonexistent, undefined); }, testNullableBasicTypesPropertyGetters: function() { - var basicTypesValues = [null, null, null, null, null, null, null]; + var nullValues = [null, null, null, null, null, null, null]; + var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; + var realm = new Realm({schema: [schemas.NullableBasicTypes]}); + var nullObject = null; var object = null; realm.write(function() { + nullObject = realm.create('NullableBasicTypesObject', nullValues); object = realm.create('NullableBasicTypesObject', basicTypesValues); }); for (var i = 0; i < schemas.BasicTypes.properties.length; i++) { var prop = schemas.BasicTypes.properties[i]; - TestCase.assertEqual(object[prop.name], null); + TestCase.assertEqual(nullObject[prop.name], null); + + if (prop.type == Realm.Types.FLOAT) { + TestCase.assertEqualWithTolerance(object[prop.name], basicTypesValues[i], 0.000001); + } + else if (prop.type == Realm.Types.DATE) { + TestCase.assertEqual(object[prop.name].getTime(), basicTypesValues[i].getTime()); + } + else { + TestCase.assertEqual(object[prop.name], basicTypesValues[i]); + } } + }, testBasicTypesPropertySetters: function() { var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; diff --git a/tests/schemas.js b/tests/schemas.js index f75f4341..718a1ff4 100644 --- a/tests/schemas.js +++ b/tests/schemas.js @@ -54,7 +54,7 @@ exports.BasicTypes = { }; exports.NullableBasicTypes = { - name: 'NullableBasicTypes', + name: 'NullableBasicTypesObject', properties: [ {name: 'boolCol', type: Realm.Types.BOOL, optional: true}, {name: 'intCol', type: Realm.Types.INT, optional: true},