From 8d10a65088fa7121894e06c1c9d142e552969185 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 21 Jan 2016 10:45:59 -0800 Subject: [PATCH] Make Schema constructable from initializer lists This enables the following syntax for defining object schemas, which is useful for writing tests: Schema schema = { {"origin", "", { {"array", PropertyTypeArray, "target"} }}, {"target", "", { {"prop1", PropertyTypeInt}, {"prop2", PropertyTypeFloat}, }}, }; --- src/object_schema.cpp | 27 ++++++++++++++++++++------- src/object_schema.hpp | 4 ++++ src/property.hpp | 2 +- src/schema.hpp | 1 + 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/object_schema.cpp b/src/object_schema.cpp index 33cd2497..3be3953f 100644 --- a/src/object_schema.cpp +++ b/src/object_schema.cpp @@ -28,6 +28,14 @@ using namespace realm; ObjectSchema::~ObjectSchema() = default; +ObjectSchema::ObjectSchema(std::string name, std::string primary_key, std::initializer_list properties) +: name(std::move(name)) +, properties(properties) +, primary_key(std::move(primary_key)) +{ + set_primary_key_property(); +} + ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(name) { ConstTableRef table = ObjectStore::table_for_object_type(group, name); @@ -50,13 +58,7 @@ ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(n } primary_key = realm::ObjectStore::get_primary_key_for_object(group, name); - if (primary_key.length()) { - auto primary_key_prop = primary_key_property(); - if (!primary_key_prop) { - throw InvalidPrimaryKeyException(name, primary_key); - } - primary_key_prop->is_primary = true; - } + set_primary_key_property(); } Property *ObjectSchema::property_for_name(StringData name) { @@ -71,3 +73,14 @@ Property *ObjectSchema::property_for_name(StringData name) { const Property *ObjectSchema::property_for_name(StringData name) const { return const_cast(this)->property_for_name(name); } + +void ObjectSchema::set_primary_key_property() +{ + if (primary_key.length()) { + auto primary_key_prop = primary_key_property(); + if (!primary_key_prop) { + throw InvalidPrimaryKeyException(name, primary_key); + } + primary_key_prop->is_primary = true; + } +} diff --git a/src/object_schema.hpp b/src/object_schema.hpp index 4ede6ce5..10a2e555 100644 --- a/src/object_schema.hpp +++ b/src/object_schema.hpp @@ -31,6 +31,7 @@ namespace realm { class ObjectSchema { public: ObjectSchema() = default; + ObjectSchema(std::string name, std::string primary_key, std::initializer_list properties); ~ObjectSchema(); // create object schema from existing table @@ -49,6 +50,9 @@ namespace realm { const Property *primary_key_property() const { return property_for_name(primary_key); } + + private: + void set_primary_key_property(); }; } diff --git a/src/property.hpp b/src/property.hpp index 3883e805..05993ae1 100644 --- a/src/property.hpp +++ b/src/property.hpp @@ -53,7 +53,7 @@ namespace realm { bool is_indexed = false; bool is_nullable = false; - size_t table_column; + size_t table_column = -1; bool requires_index() const { return is_primary || is_indexed; } }; diff --git a/src/schema.hpp b/src/schema.hpp index b8d50777..2df1ec9e 100644 --- a/src/schema.hpp +++ b/src/schema.hpp @@ -31,6 +31,7 @@ private: public: // Create a schema from a vector of ObjectSchema Schema(base types); + Schema(std::initializer_list types) : Schema(base(types)) { } // find an ObjectSchema by name iterator find(std::string const& name);