Skip to content

Commit

Permalink
Fixing Issue #3731 (#3735)
Browse files Browse the repository at this point in the history
* Fixing Issue #3731

* Adding entry to the changelog
  • Loading branch information
cesarvr authored May 12, 2021
1 parent 47b8b52 commit 269361e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NOTE: Bump file format version to 21. NO DOWNGRADE PATH IS AVAILABLE.
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* Set didn't export `objectType` to Realm.schema when it contained scalar types.
* Fixed the naming of `url` (now `baseUrl`) property on an app config to match the TypeScript declaration and other SDKs. ([#3612](https://github.com/realm/realm-js/issues/3612))
* Add explicity support for Nullable/Undefined values for the Mixed type. ([#3731](https://github.com/realm/realm-js/issues/3731))

### Compatibility
* MongoDB Realm Cloud.
Expand Down
8 changes: 8 additions & 0 deletions src/common/type_deduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ class GenericTypeDeductionImpl {
return realm_to_js_map[value];
}

template <typename MixedValue>
types::Type from(MixedValue mixed) {
if(mixed.is_null()) return types::Type::Null;

int realm_type = static_cast<int>(mixed.get_type());
return static_cast<types::Type>(realm_type);
}

types::Type from(DataType data_type) {
int realm_type = static_cast<int>(data_type);
return static_cast<types::Type>(realm_type);
Expand Down
15 changes: 14 additions & 1 deletion src/js_mixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ class MixedUUID : public MixedWrapper<Context, Value> {
}
};

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

Value unwrap(Context context, Mixed mixed) {
return Utils::from_null(context);
}
};

template <typename Context, typename Value, typename Utils>
class MixedBinary : public MixedWrapper<Context, Value> {
private:
Expand Down Expand Up @@ -166,6 +177,8 @@ class TypeMixed {
{types::UUID, new MixedUUID<Context, Value, Utils>},
{types::Binary, new MixedBinary<Context, Value, Utils>},
{types::Timestamp, new MixedTimeStamp<Context, Value, Utils>},
{types::Null, new MixedNullable<Context, Value, Utils>},
{types::Undefined, new MixedNullable<Context, Value, Utils>},
};

TypeMixed() {}
Expand All @@ -186,7 +199,7 @@ class TypeMixed {

Value wrap(Context context, Mixed mixed) {
auto type_deduction = TypeDeduction::get_instance();
auto rjs_type = type_deduction.from(mixed.get_type());
auto rjs_type = type_deduction.from(mixed);
auto strategy = strategies[rjs_type];

if (strategy == nullptr) {
Expand Down
29 changes: 28 additions & 1 deletion tests/js/mixed-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const TestCase = require('./asserts');

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


const SingleSchema = {
name: 'mixed',
properties: {
Expand Down Expand Up @@ -173,6 +172,34 @@ module.exports = {
() => realm.write(()=> realm.create(SingleSchema.name, { a: Object.create({}) } ) ),
new Error('Only Realm instances are supported.') )
},

testMixedEmptyValues() {
const MixedNullableSchema = {
name: 'mixed',
properties: {
nullable: 'mixed',
nullable_list: 'mixed[]'
}
}

let realm = new Realm({schema: [MixedNullableSchema]});
realm.write(()=> realm.create(MixedNullableSchema.name, { nullable: undefined } ) )

let value = realm.objects(MixedNullableSchema.name)[0]
realm.write(()=> value.nullable = null )
realm.write(()=> value.nullable = undefined )

realm.write(() => {
value.nullable_list = [6, null, undefined, null, 5]
});

TestCase.assertEqual(value.nullable_list[0], 6, 'Should be equal 6');
TestCase.assertEqual(value.nullable_list[1], null, 'Should be equal null');
TestCase.assertEqual(value.nullable_list[2], null, 'Should be equal null');
TestCase.assertEqual(value.nullable_list[3], null, 'Should be equal null');
TestCase.assertEqual(value.nullable_list[4], 5, 'Should be equal 5');
},

}


0 comments on commit 269361e

Please sign in to comment.