Skip to content

Commit

Permalink
Add support and test for function default properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Duncalf committed Sep 29, 2022
1 parent f4f34aa commit 5c2b7e1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
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 @@ -37,3 +37,4 @@ import "./sync/asymmetric";
import "./sync/sync-as-local";
import "./transaction";
import "./types";
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);
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);
}
// return it != defaults.end() ? util::make_optional(ValueType(it->second)) : std::nullopt;
}

template <typename T>
Expand Down

0 comments on commit 5c2b7e1

Please sign in to comment.