diff --git a/components/core/src/clp/ffi/KeyValuePairLogEvent.cpp b/components/core/src/clp/ffi/KeyValuePairLogEvent.cpp index ad175b3f1..4b528c039 100644 --- a/components/core/src/clp/ffi/KeyValuePairLogEvent.cpp +++ b/components/core/src/clp/ffi/KeyValuePairLogEvent.cpp @@ -55,14 +55,14 @@ class JsonSerializationIterator { JsonExceptionHandler json_exception_callback ) : m_schema_tree_node{schema_tree_node}, - m_parent{parent_json_obj}, + m_parent_json_obj{parent_json_obj}, m_json_exception_callback{json_exception_callback} { for (auto const child_id : schema_tree_node->get_children_ids()) { if (schema_subtree_bitmap[child_id]) { - m_children.push_back(child_id); + m_child_schema_tree_nodes.push_back(child_id); } } - m_curr_child_it = m_children.cbegin(); + m_child_schema_tree_node_it = m_child_schema_tree_nodes.cbegin(); } // Delete copy/move constructor and assignment @@ -77,9 +77,12 @@ class JsonSerializationIterator { // If the current node is the root, then replace the `parent` with this node's JSON // object. Otherwise, add this node's JSON object as a child of the parent JSON object. if (m_schema_tree_node->get_id() == SchemaTree::cRootId) { - *m_parent = std::move(m_map); + *m_parent_json_obj = std::move(m_json_obj); } else { - m_parent->emplace(string{m_schema_tree_node->get_key_name()}, std::move(m_map)); + m_parent_json_obj->emplace( + string{m_schema_tree_node->get_key_name()}, + std::move(m_json_obj) + ); } } catch (nlohmann::json::exception const& ex) { m_json_exception_callback(ex); @@ -89,24 +92,26 @@ class JsonSerializationIterator { /** * @return Whether there are more child schema tree nodes to traverse. */ - [[nodiscard]] auto has_next_child() const -> bool { - return m_curr_child_it != m_children.end(); + [[nodiscard]] auto has_next_child_schema_tree_node() const -> bool { + return m_child_schema_tree_node_it != m_child_schema_tree_nodes.end(); } /** * Gets the next child schema tree node and advances the iterator. * @return The next child schema tree node. */ - [[nodiscard]] auto get_next_child() -> SchemaTreeNode::id_t { return *(m_curr_child_it++); } + [[nodiscard]] auto get_next_child_schema_tree_node() -> SchemaTreeNode::id_t { + return *(m_child_schema_tree_node_it++); + } - [[nodiscard]] auto get_map() -> nlohmann::json::object_t& { return m_map; } + [[nodiscard]] auto get_json_obj() -> nlohmann::json::object_t& { return m_json_obj; } private: SchemaTreeNode const* m_schema_tree_node; - vector m_children; - vector::const_iterator m_curr_child_it; - nlohmann::json::object_t* m_parent; - nlohmann::json::object_t m_map; + vector m_child_schema_tree_nodes; + vector::const_iterator m_child_schema_tree_node_it; + nlohmann::json::object_t* m_parent_json_obj; + nlohmann::json::object_t m_json_obj; JsonExceptionHandler m_json_exception_callback; }; @@ -310,35 +315,34 @@ auto insert_kv_pair_into_json_obj( std::optional const& optional_val, nlohmann::json::object_t& json_obj ) -> bool { - auto const key_name{node.get_key_name()}; + string const key_name{node.get_key_name()}; auto const type{node.get_type()}; if (false == optional_val.has_value()) { - json_obj.emplace(string{key_name}, nlohmann::json::object()); + json_obj.emplace(key_name, nlohmann::json::object()); return true; } - string const key_name_str{key_name}; try { auto const& val{optional_val.value()}; switch (type) { case SchemaTreeNode::Type::Int: - json_obj.emplace(key_name_str, val.get_immutable_view()); + json_obj.emplace(key_name, val.get_immutable_view()); break; case SchemaTreeNode::Type::Float: - json_obj.emplace(key_name_str, val.get_immutable_view()); + json_obj.emplace(key_name, val.get_immutable_view()); break; case SchemaTreeNode::Type::Bool: - json_obj.emplace(key_name_str, val.get_immutable_view()); + json_obj.emplace(key_name, val.get_immutable_view()); break; case SchemaTreeNode::Type::Str: if (val.is()) { - json_obj.emplace(key_name_str, string{val.get_immutable_view()}); + json_obj.emplace(key_name, string{val.get_immutable_view()}); } else { auto const decoded_result{decode_as_encoded_text_ast(val)}; if (false == decoded_result.has_value()) { return false; } - json_obj.emplace(key_name_str, decoded_result.value()); + json_obj.emplace(key_name, decoded_result.value()); } break; case SchemaTreeNode::Type::UnstructuredArray: { @@ -346,11 +350,11 @@ auto insert_kv_pair_into_json_obj( if (false == decoded_result.has_value()) { return false; } - json_obj.emplace(key_name_str, nlohmann::json::parse(decoded_result.value())); + json_obj.emplace(key_name, nlohmann::json::parse(decoded_result.value())); break; } case SchemaTreeNode::Type::Obj: - json_obj.emplace(key_name_str, nullptr); + json_obj.emplace(key_name, nullptr); break; default: return false; @@ -417,34 +421,39 @@ auto KeyValuePairLogEvent::serialize_to_json( // // On the way up, add the current node's `nlohmann::json::object_t` to the parent's // `nlohmann::json::object_t`. - auto const& root_node{m_schema_tree->get_node(SchemaTree::cRootId)}; - auto json_root = nlohmann::json::object_t(); - - dfs_stack.emplace(&root_node, schema_subtree_bitmap, &json_root, json_exception_handler); + auto const& root_schema_tree_node{m_schema_tree->get_node(SchemaTree::cRootId)}; + auto root_json_obj = nlohmann::json::object_t(); + + dfs_stack.emplace( + &root_schema_tree_node, + schema_subtree_bitmap, + &root_json_obj, + json_exception_handler + ); while (false == dfs_stack.empty() && false == json_exception_captured) { auto& top{dfs_stack.top()}; - if (false == top.has_next_child()) { + if (false == top.has_next_child_schema_tree_node()) { dfs_stack.pop(); continue; } - auto const child_node_id{top.get_next_child()}; - auto const& child_node{m_schema_tree->get_node(child_node_id)}; - if (m_node_id_value_pairs.contains(child_node_id)) { + auto const child_schema_tree_node_id{top.get_next_child_schema_tree_node()}; + auto const& child_schema_tree_node{m_schema_tree->get_node(child_schema_tree_node_id)}; + if (m_node_id_value_pairs.contains(child_schema_tree_node_id)) { // Handle leaf node if (false == insert_kv_pair_into_json_obj( - child_node, - m_node_id_value_pairs.at(child_node_id), - top.get_map() + child_schema_tree_node, + m_node_id_value_pairs.at(child_schema_tree_node_id), + top.get_json_obj() )) { return std::errc::protocol_error; } } else { dfs_stack.emplace( - &child_node, + &child_schema_tree_node, schema_subtree_bitmap, - &top.get_map(), + &top.get_json_obj(), json_exception_handler ); } @@ -454,6 +463,6 @@ auto KeyValuePairLogEvent::serialize_to_json( return std::errc::protocol_error; } - return json_root; + return root_json_obj; } } // namespace clp::ffi diff --git a/components/core/src/clp/ffi/KeyValuePairLogEvent.hpp b/components/core/src/clp/ffi/KeyValuePairLogEvent.hpp index add067c4b..e08128f49 100644 --- a/components/core/src/clp/ffi/KeyValuePairLogEvent.hpp +++ b/components/core/src/clp/ffi/KeyValuePairLogEvent.hpp @@ -65,8 +65,10 @@ class KeyValuePairLogEvent { * Serializes the log event into a `nlohmann::json` object. * @return A result containing the serialized JSON object or an error code indicating the * failure: - * - std::errc::protocol_error if a `nlohmann::json::exception` occurs, or typecasting a `Value` - * instance fails. + * - std::errc::protocol_error if a value in the log event couldn't be decoded or it couldn't be + * inserted into a JSON object. + * - std::errc::result_out_of_range if a node ID in the log event doesn't exist in the schema + * tree. */ [[nodiscard]] auto serialize_to_json( ) const -> OUTCOME_V2_NAMESPACE::std_result;