Skip to content

Commit

Permalink
Add: Generate response with {fmt} (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut authored May 13, 2024
1 parent d3f20d5 commit 4d9c250
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,16 @@ endif()

if(UCALL_BUILD_EXAMPLES)
add_executable(ucall_example_server_posix examples/ucall_server.cpp)
target_link_libraries(ucall_example_server_posix ucall_server_posix cxxopts)
target_link_libraries(ucall_example_server_posix ucall_server_posix cxxopts
fmt::fmt)
target_compile_options(ucall_example_server_posix
PUBLIC -DCXXOPTS_NO_EXCEPTIONS=ON)
endif()

if(UCALL_BUILD_EXAMPLES AND UCALL_BUILD_LIB_URING)
add_executable(ucall_example_server_uring examples/ucall_server.cpp)
target_link_libraries(ucall_example_server_uring ucall_server_uring cxxopts)
target_link_libraries(ucall_example_server_uring ucall_server_uring cxxopts
fmt::fmt)
target_compile_options(ucall_example_server_uring
PUBLIC -DCXXOPTS_NO_EXCEPTIONS=ON)
endif()
26 changes: 25 additions & 1 deletion examples/ucall_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cxxopts.hpp> // Parsing CLI arguments

#include <fmt/ranges.h>
#include <ucall/ucall.h>

/**
Expand Down Expand Up @@ -103,7 +104,30 @@ static void validate_user_identity(ucall_call_t call, ucall_callback_tag_t) {
}

char result[1024];
ucall_call_reply_content(call, result, strlen(result));
std::vector<double> suggested_session_ids;
// TODO: populate suggested_session_ids
auto format_result = fmt::format_to_n(
result,
sizeof(result),
"{{\n"
" \"session_ids\": [{0}],\n"
" \"user\": {{\n"
" \"name\": \"{1}\",\n"
" \"age\": {2},\n"
" \"user_id\": {3},\n"
" \"access_token\": \"{4}\",\n"
" \"repeated_sesson_ids\": [{0}]\n"
" }}\n"
"}}",
fmt::join(suggested_session_ids, ", "),
fmt::string_view(name_ptr, name_len),
age,
user_id,
fmt::string_view(token_ptr, token_len));
if (format_result.size > sizeof(result)) {
return ucall_call_reply_error_out_of_memory(call);
}
ucall_call_reply_content(call, result, format_result.size);
}

struct key_value_pair {
Expand Down

0 comments on commit 4d9c250

Please sign in to comment.