| #include "state_merge.h" |
| |
| #include "safepower_agent.pb.h" |
| #include "google/protobuf/map.h" |
| |
| namespace safepower_agent { |
| |
| using ::safepower_agent_proto::SystemState; |
| using ::safepower_agent_proto::NodeState; |
| |
| static void Update(google::protobuf::Message& dest, const google::protobuf::Message& update) { |
| dest.MergeFrom(update); |
| } |
| |
| // NodeState contains a map field, so we need to handle it separately. |
| static void Update(NodeState& dest, const NodeState& update); |
| |
| // Recursively update a map field. |
| template <typename Key, typename Value> |
| static void Update(google::protobuf::Map<Key, Value>& dest, |
| const google::protobuf::Map<Key, Value>& update) { |
| for (const auto& [key, value] : update) { |
| auto found = dest.find(key); |
| if (found == dest.end()) { |
| dest.insert({key, value}); |
| } else { |
| Update(found->second, value); |
| } |
| } |
| } |
| |
| static void Update(NodeState& dest, const NodeState& update) { |
| Update(*dest.mutable_component_state(), update.component_state()); |
| } |
| |
| template <> |
| void ApplyStateUpdate(SystemState& current_state, const SystemState& update) { |
| if (update.has_epoch_ms()) { |
| current_state.set_epoch_ms(update.epoch_ms()); |
| } |
| Update(*current_state.mutable_node_state(), update.node_state()); |
| } |
| |
| |
| } // namespace safepower_agent |