Skip to content

Commit

Permalink
RCPP-61 Add BSON support (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire authored Mar 25, 2024
1 parent 15d8612 commit 7f91ab8
Show file tree
Hide file tree
Showing 17 changed files with 1,171 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ X.Y.Z Release notes (YYYY-MM-DD)
* Add `managed<std::vector<>>::as_results()` to allow the ability to derive a `realm::results<>` collection from a managed vector.
* Allow a `realm::uuid` to be constructed with `std::array<uint8_t, 16>`.
* Add support for integrating `cpprealm` with the Conan package manager.
* Add BSON support for `user::call_function` and `user::get_custom_data` API's.
* Add `user::get_custom_data()` and deprecate `user::custom_data()`.

### Breaking Changes
* None
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ let package = Package(
"db/results_tests.cpp",
"db/run_loop_tests.cpp",
"db/set_tests.cpp",
"db/string_tests.cpp"
"db/string_tests.cpp",
"db/bson_tests.cpp"
],
resources: [
.copy("setup_baas.rb"),
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def source(self):
git = Git(self)
git.clone(url="https://github.com/realm/realm-cpp", target=".")
git.folder = "."
git.checkout(commit="3f125272f21ec24d8f7f2be767b1100a8ccaa88b")
git.checkout(commit="c1096b169258581927fa1591686bdc1687f368ed")
git.run("submodule update --init --recursive")

def layout(self):
Expand Down
1 change: 1 addition & 0 deletions include/cpprealm/bson.hpp
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cpprealm/internal/bridge/bridge_types.
set(SOURCES
cpprealm/analytics.cpp
cpprealm/app.cpp
cpprealm/bson.cpp
cpprealm/db.cpp
cpprealm/client_reset.cpp
cpprealm/managed_binary.cpp
Expand Down Expand Up @@ -73,6 +74,7 @@ set(HEADERS
cpprealm/analytics.hpp
cpprealm/app.hpp
cpprealm/accessors.hpp
cpprealm/bson.hpp
cpprealm/db.hpp
cpprealm/client_reset.hpp
cpprealm/link.hpp
Expand Down
53 changes: 42 additions & 11 deletions src/cpprealm/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ namespace realm {
}
}

std::optional<bsoncxx::document> user::get_custom_data() const
{
if (auto v = m_user->custom_data()) {
return bsoncxx::document(*v);
} else {
return std::nullopt;
}
}

void user::call_function(const std::string& name, const std::string& arguments,
std::function<void(std::optional<std::string>, std::optional<app_error>)> callback) const
{
Expand All @@ -241,14 +250,6 @@ namespace realm {
});
}

/**
Calls the Atlas App Services function with the provided name and arguments.
@param name The name of the Atlas App Services function to be called.
@param arguments The `BsonArray` of arguments to be provided to the function.
@param callback The completion handler to call when the function call is complete.
This handler is executed on the thread the method was called from.
*/
std::future<std::optional<std::string>> user::call_function(const std::string& name, const std::string& arguments) const
{
std::promise<std::optional<std::string>> p;
Expand All @@ -265,9 +266,39 @@ namespace realm {
return f;
}

/**
Refresh a user's custom data. This will, in effect, refresh the user's auth session.
*/
void user::call_function(const std::string& name, const std::vector<bsoncxx>& args_bson,
std::function<void(std::optional<bsoncxx>, std::optional<app_error>)> callback) const
{
bson::BsonArray core_bson;
for(auto& b : args_bson) {
core_bson.push_back(b);
}
m_user->sync_manager()->app().lock()->call_function(m_user, name, core_bson, std::nullopt, [cb = std::move(callback)](util::Optional<bson::Bson>&& b,
std::optional<app_error> err) {
cb(b ? std::optional<bsoncxx>(*b) : std::nullopt, err);
});
}

std::future<std::optional<bsoncxx>> user::call_function(const std::string& name, const std::vector<bsoncxx>& args_bson) const
{
std::promise<std::optional<bsoncxx>> p;
std::future<std::optional<bsoncxx>> f = p.get_future();
bson::BsonArray core_bson;
for(auto& b : args_bson) {
core_bson.push_back(b);
}

m_user->sync_manager()->app().lock()->call_function(m_user, name, core_bson, std::nullopt, [p = std::move(p)](util::Optional<bson::Bson>&& b,
std::optional<app_error> err) mutable {
if (err) {
p.set_exception(std::make_exception_ptr(app_error(std::move(*err))));
} else {
p.set_value(b ? std::optional<bsoncxx>(*b) : std::nullopt);
}
});
return f;
}

void user::refresh_custom_user_data(std::function<void(std::optional<app_error>)> callback)
{
m_user->refresh_custom_data(std::move(callback));
Expand Down
31 changes: 29 additions & 2 deletions src/cpprealm/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef CPPREALM_APP_HPP
#define CPPREALM_APP_HPP

#include <cpprealm/bson.hpp>
#include <cpprealm/db.hpp>

#include <cpprealm/internal/bridge/realm.hpp>
Expand Down Expand Up @@ -156,11 +157,15 @@ struct user {

[[nodiscard]] std::future<void> log_out() const;


[[deprecated("Replaced by `get_custom_data()`. This method will be removed in a future release.")]]
[[nodiscard]] std::optional<std::string> custom_data() const;

/**
The custom data of the user.
This is configured in your Atlas App Services app.
*/
[[nodiscard]] std::optional<std::string> custom_data() const;
[[nodiscard]] std::optional<bsoncxx::document> get_custom_data() const;

/**
Calls the Atlas App Services function with the provided name and arguments.
Expand All @@ -170,6 +175,7 @@ struct user {
@param callback The completion handler to call when the function call is complete.
This handler is executed on the thread the method was called from.
*/
[[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]]
void call_function(const std::string& name, const std::string& args_ejson,
std::function<void(std::optional<std::string>, std::optional<app_error>)> callback) const;

Expand All @@ -178,12 +184,33 @@ struct user {
@param name The name of the Atlas App Services function to be called.
@param arguments The string represented extended json to be provided to the function.
@param callback The completion handler to call when the function call is complete.
@return A future containing an optional std::string once the operation has completed.
This handler is executed on the thread the method was called from.
*/
[[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]]
[[nodiscard]] std::future<std::optional<std::string>> call_function(const std::string& name,
const std::string& args_ejson) const;

/**
Calls the Atlas App Services function with the provided name and arguments.
@param name The name of the Atlas App Services function to be called.
@param arguments The BSON array to be provided to the function.
@param callback The completion handler to call when the function call is complete.
This handler is executed on the thread the method was called from.
*/
void call_function(const std::string& name, const std::vector<bsoncxx>& args_bson,
std::function<void(std::optional<bsoncxx>, std::optional<app_error>)> callback) const;

/**
Calls the Atlas App Services function with the provided name and arguments.
@param name The name of the Atlas App Services function to be called.
@param arguments The BSON array to be provided to the function.
@return A future containing optional BSON once the operation has completed.
*/
[[nodiscard]] std::future<std::optional<bsoncxx>> call_function(const std::string& name, const std::vector<bsoncxx>& args_bson) const;

/**
Refresh a user's custom data. This will, in effect, refresh the user's auth session.
*/
Expand Down
Loading

0 comments on commit 7f91ab8

Please sign in to comment.