Skip to content

Commit

Permalink
new c api for collections in mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-cab committed Aug 11, 2023
1 parent 471a158 commit e64fe16
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef bool (*realm_on_object_store_error_callback_t)(realm_userdata_t userdata

/* Accessor types */
typedef struct realm_object realm_object_t;

typedef struct realm_list realm_list_t;
typedef struct realm_set realm_set_t;
typedef struct realm_dictionary realm_dictionary_t;
Expand Down Expand Up @@ -280,6 +281,11 @@ typedef enum realm_collection_type {
RLM_COLLECTION_TYPE_DICTIONARY = 4,
} realm_collection_type_e;

typedef struct realm_collection {
void* collection;
realm_collection_type_e collection_type;
} realm_collection_t;

typedef struct realm_property_info {
const char* name;
const char* public_name;
Expand Down Expand Up @@ -1784,6 +1790,11 @@ RLM_API bool realm_list_set(realm_list_t*, size_t index, realm_value_t value);
*/
RLM_API bool realm_list_insert(realm_list_t*, size_t index, realm_value_t value);


//unique API for nested collections
RLM_API realm_collection_t* realm_collection_insert_collection(
realm_collection_t*, realm_value_t, realm_collection_type_e);

/**
* Insert a collection inside a list (only available for mixed properities)
*
Expand Down Expand Up @@ -2309,9 +2320,10 @@ RLM_API bool realm_dictionary_insert(realm_dictionary_t*, realm_value_t key, rea
RLM_API realm_object_t* realm_dictionary_insert_embedded(realm_dictionary_t*, realm_value_t key);

/**
* Insert a nested collection
* Insert collection inside a dictionary
* @return true/false
*/
RLM_API bool realm_dictionary_insert_collection(realm_dictionary_t*, realm_value_t key, realm_collection_type_e);
RLM_API bool realm_dictionary_insert_collection(realm_dictionary_t*, realm_value_t, realm_collection_type_e);

/**
* Fetch a list from a dictionary.
Expand Down
65 changes: 65 additions & 0 deletions src/realm/object-store/c_api/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,71 @@ RLM_API bool realm_list_insert(realm_list_t* list, size_t index, realm_value_t v
});
}

// Test: unique API for nested collections
RLM_API realm_collection_t* realm_collection_insert_collection(realm_collection_t* collection, realm_value_t path,
realm_collection_type_e type)
{
REALM_ASSERT(collection);
return wrap_err([&]() {
realm_collection_t* collection = nullptr;
auto collection_type = from_capi(collection->collection_type);
if (collection_type) {
if (*collection_type == CollectionType::List) {
realm_list_t* list = (realm_list_t*)(collection->collection);
if (path.type != RLM_TYPE_INT) {
throw InvalidArgument{"Only integer ndx are supported in lists"};
}
auto index = static_cast<int>(path.integer);
const auto collection_type = from_capi(type);
list->insert_collection(index, *collection_type);

collection = new realm_collection_t;
if (collection_type == CollectionType::List) {
collection->collection = new realm_list_t{list->get_list(index)};
collection->collection_type = RLM_COLLECTION_TYPE_LIST;
}
else if (collection_type == CollectionType::Set) {
collection->collection = new realm_set_t{list->get_set(index)};
collection->collection_type = RLM_COLLECTION_TYPE_SET;
}
else if (collection_type == CollectionType::Dictionary) {
collection->collection = new realm_dictionary_t{list->get_dictionary(index)};
collection->collection_type = RLM_COLLECTION_TYPE_DICTIONARY;
}
return collection;
}
else if (*collection_type == CollectionType::Set) {
throw InvalidArgument{"Sets cannot contain any nested collection"};
}
else if (*collection_type == CollectionType::Dictionary) {
realm_dictionary_t* dictionary = (realm_dictionary_t*)(collection->collection);
if (path.type != RLM_TYPE_STRING) {
throw InvalidArgument{"Only string keys are supported in dictionaries"};
}
StringData k{path.string.data, path.string.size};
const auto collection_type = from_capi(type);
dictionary->insert_collection(k, *collection_type);

collection = new realm_collection_t;
if (collection_type == CollectionType::List) {
collection->collection = new realm_list_t{dictionary->get_list(k)};
collection->collection_type = RLM_COLLECTION_TYPE_LIST;
}
else if (collection_type == CollectionType::Set) {
collection->collection = new realm_set_t{dictionary->get_set(k)};
collection->collection_type = RLM_COLLECTION_TYPE_SET;
}
else if (collection_type == CollectionType::Dictionary) {
collection->collection = new realm_dictionary_t{dictionary->get_dictionary(k)};
collection->collection_type = RLM_COLLECTION_TYPE_DICTIONARY;
}
return collection;
}
}
return collection;
});
}

RLM_API bool realm_list_insert_collection(realm_list_t* list, size_t index, realm_collection_type_e type)
{
return wrap_err([&]() {
Expand Down

0 comments on commit e64fe16

Please sign in to comment.