Skip to content

Commit

Permalink
Update Function example to use string args
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc committed Jan 5, 2024
1 parent 8a240d2 commit 336c985
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
28 changes: 8 additions & 20 deletions examples/cpp/sync/call-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@

static const std::string APP_ID = "cpp-tester-uliix";

// This test is currently commented out because the SDK has removed the
// exposed Core headers that gave it access to a BSON library.
// See PR https://github.com/realm/realm-cpp/pull/123/
// Per Lee, a separate project will create a C++ SDK BSON library, but in
// the meantime, I'll need to use some other library to make this test work.
// I need to figure out how to create BSON strings in C++ and pass them
// instead of using realm::bson::Bson for the params.
// TODO: Figure out what library to use and how to make this test/example work.
#if 0
TEST_CASE("call a function", "[realm][sync]") {
// :snippet-start: call-a-function
// Connect to an App Services App and authenticate a user
Expand All @@ -25,24 +16,21 @@ TEST_CASE("call a function", "[realm][sync]") {
auto user = app.login(realm::App::credentials::anonymous()).get();
auto sync_config = user.flexible_sync_configuration();

// If a function takes arguments, pass them as BSON
auto arg1 = realm::bson::Bson("john.smith");
auto arg2 = realm::bson::Bson("@companyemail.com");
// If the function takes arguments, pass them as a string array.
// Any quotes within the array must be escaped.
auto argArray = "[\"john.smith\", \"@companyemail.com\"]";

// Call an App Services function as the logged-in user
auto result = user.call_function("concatenate", {arg1, arg2}).get();
auto result = user.call_function("concatenate", argArray).get();

// Verify that the result has a value
CHECK(result);
auto bsonResult = result.value();
auto functionResult = result.value();

// Translate the BSON result back to a string
auto resultString = std::string(bsonResult);
// Prints "Calling the concatenate function returned
// [email protected]."
std::cout << "Calling the concatenate function returned " << resultString
// "[email protected]"."
std::cout << "Calling the concatenate function returned " << functionResult
<< ".\n";
// :snippet-end:
REQUIRE(resultString == "[email protected]");
REQUIRE(functionResult == "\"[email protected]\"");
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ auto app = realm::App(appConfig);
auto user = app.login(realm::App::credentials::anonymous()).get();
auto sync_config = user.flexible_sync_configuration();

// If a function takes arguments, pass them as BSON
auto arg1 = realm::bson::Bson("john.smith");
auto arg2 = realm::bson::Bson("@companyemail.com");
// If the function takes arguments, pass them as a string array.
// Any quotes within the array must be escaped.
auto argArray = "[\"john.smith\", \"@companyemail.com\"]";

// Call an App Services function as the logged-in user
auto result = user.call_function("concatenate", {arg1, arg2}).get();
auto result = user.call_function("concatenate", argArray).get();

// Verify that the result has a value
CHECK(result);
auto bsonResult = result.value();
auto functionResult = result.value();

// Translate the BSON result back to a string
auto resultString = std::string(bsonResult);
// Prints "Calling the concatenate function returned
// [email protected]."
std::cout << "Calling the concatenate function returned " << resultString
// "[email protected]"."
std::cout << "Calling the concatenate function returned " << functionResult
<< ".\n";
7 changes: 3 additions & 4 deletions source/sdk/cpp/app-services/call-a-function.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ To execute a function from the C++ SDK, use the
:cpp-sdk:`call_function() <structrealm_1_1user.html#a82ab02822dd96e8d44201b996dd6ed0c>`
member function on the ``user`` object. Pass in the name of the
function as a string for the first parameter. This function takes two arguments,
which we provide as a ``BsonArray`` of arguments:
which we provide as a string array of arguments:

.. literalinclude:: /examples/generated/cpp/call-function.snippet.call-a-function.cpp
:language: cpp

The callback can provide an optional BSON result, or an optional error.
In the example above, we check that the result has a value, and then cast
it back to a string.
The callback can provide an optional string result, or an optional error.
In the example above, we check that the result has a value.

0 comments on commit 336c985

Please sign in to comment.