Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Json to match Response refactor #1

Merged
merged 8 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ jobs:
run: cmake --build "$BUILD_DIR" --config $CMAKE_BUILD_TYPE

- name: Test
working-directory: ${{github.workspace}}/build
working-directory: "$BUILD_DIR"
thorulf4 marked this conversation as resolved.
Show resolved Hide resolved
run: ctest --config $CMAKE_BUILD_TYPE --output-on-failure
8 changes: 7 additions & 1 deletion cmake/nlohmann.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ FetchContent_Declare(json
URL https://github.com/nlohmann/json/releases/download/v3.11.2/json.tar.xz
DOWNLOAD_EXTRACT_TIMESTAMP ON
)
FetchContent_MakeAvailable(json)

# Manually make available so we can mark library as SYSTEM headers thus disabling warnings
FetchContent_GetProperties(json)
if(NOT json_POPULATED)
FetchContent_Populate(json)
add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} SYSTEM)
endif()
3 changes: 2 additions & 1 deletion include/uls/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Server
bool is_running{false};

void send(const std::string& message_type, const nlohmann::json& message);
void send_error(const nlohmann::json& message);

public:
Server(IOStream io): io(std::move(io)) {}
Expand Down Expand Up @@ -58,7 +59,7 @@ class Server
void send_notification(const std::string& type, Data&& element)
{
auto message = Serializer<std::remove_reference_t<Data>>::serialize(std::forward<Data>(element));
send(type, message);
send("notif/" + type, message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces additional string allocations and construction..

}

void start();
Expand Down
4 changes: 2 additions & 2 deletions src/highlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ void Highlight::configure(Server& server)

repository.add_on_document_update([this, &server](UTAP::Document& doc) {
auto keywords = keywords_for_path(doc, repository.get_current_xpath());
server.send_notification("notif/keywords", keywords);
server.send_notification("keywords", keywords);
});

repository.add_on_current_node_changed([this, &server](const std::string& xpath) {
auto keywords = keywords_for_path(repository.get_document(), xpath);
server.send_notification("notif/keywords", keywords);
server.send_notification("keywords", keywords);
});
}
15 changes: 10 additions & 5 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace views = std::ranges::views;
using namespace std::chrono_literals;
using json = nlohmann::json;

nlohmann::json OK_RESPONSE = {{"res", "OK"}};
nlohmann::json FAIL_RESPONSE = {{"res", "FAIL"}};
nlohmann::json OK_RESPONSE = {"OK"};
nlohmann::json FAIL_RESPONSE = {"FAIL"};

std::vector<Command>::const_iterator find_command(const std::vector<Command>& commands, const std::string& name)
{
Expand All @@ -34,10 +34,10 @@ void Server::start()
auto response = cmd->callback(message["args"]);
send("response/" + cmd->name, response);
} catch (nlohmann::json::parse_error& e) {
send("error", e.what());
send_error(e.what());
stop();
} catch (std::exception& e) {
send("error", e.what());
send_error(e.what());
}
}
}
Expand All @@ -53,7 +53,12 @@ Server& Server::add_close_command(std::string name)

void Server::send(const std::string& message_type, const nlohmann::json& message)
{
io.out << json{{"type", message_type}, {"msg", message}} << std::endl;
io.out << json{{"res", message_type}, {"info", message}} << std::endl;
}

void Server::send_error(const nlohmann::json& message)
{
send("err", message);
}

void Server::stop() { is_running = false; }
Expand Down
4 changes: 2 additions & 2 deletions test/server_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ struct MockIO : IOStream
{
nlohmann::json message;
out_buf >> message;
return message["msg"];
return message["info"];
}

bool expect_error()
{
nlohmann::json message;
out_buf >> message;
return message["type"] == "error";
return message["type"] == "err";
}

bool out_eof() { return out_buf.eof(); }
Expand Down
Loading