support null for basic propety types

This commit is contained in:
Ari Lazier 2015-10-27 10:04:56 -07:00
parent e3fb40f299
commit ac1cdfdc8b
4 changed files with 28 additions and 4 deletions

View File

@ -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);
}

View File

@ -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<ValueType, ContextType>;
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));

View File

@ -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'];

View File

@ -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},