Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support and test for function default properties #5001

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ realm.write(() => {
```
### Enhancements
* Small improvement to performance by caching JSI property String object [#4863](https://github.com/realm/realm-js/pull/4863)
* Add support for using functions as default property values, in order to allow dynamic defaults [#5001](https://github.com/realm/realm-js/pull/5001), [#2393](https://github.com/realm/realm-js/issues/2393)

### Compatibility
* React Native >= v0.70.0
Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ import "./sync/flexible";
import "./sync/asymmetric";
import "./sync/sync-as-local";
import "./transaction";
import "./schema";
50 changes: 50 additions & 0 deletions integration-tests/tests/src/tests/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { expect } from "chai";
import Realm from "realm";

describe("Realm schema", () => {
describe("Default property values", () => {
it("can take a function as a default property value", () => {
interface Test {
dynamic?: number;
}

const realm = new Realm({
schema: [
{
name: "Test",
properties: {
dynamic: {
type: "int",
default: () => 42,
},
},
},
],
});

const test = realm.write(() => {
return realm.create<Test>("Test", {});
});

expect(test.dynamic).to.equal(42);
});
});
});
17 changes: 15 additions & 2 deletions src/js_object_accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "js_set.hpp"
#include "js_realm_object.hpp"
#include "js_schema.hpp"
#include "js_types.hpp"
#include "realm/util/optional.hpp"

#if REALM_ENABLE_SYNC
#include <realm/util/base64.hpp>
Expand Down Expand Up @@ -119,9 +121,20 @@ class NativeAccessor {

OptionalValue default_value_for_property(const ObjectSchema& object_schema, const Property& prop)
{
auto defaults = get_delegate<JSEngine>(m_realm.get())->m_defaults[object_schema.name];
const auto& defaults = get_delegate<JSEngine>(m_realm.get())->m_defaults[object_schema.name];
auto it = defaults.find(prop.name);
return it != defaults.end() ? util::make_optional(ValueType(it->second)) : std::nullopt;

if (it == defaults.end()) {
return std::nullopt;
}
else {
auto value = ValueType(it->second);
// Call functions passed as the default value to allow dynamic defaults
if (Value::is_function(m_ctx, value)) {
value = Function<JSEngine>::call(m_ctx, Value::validated_to_function(m_ctx, value), 0, {});
}
return util::make_optional(value);
}
}

template <typename T>
Expand Down