Skip to content

Commit f19666d

Browse files
konturam-blaha
authored andcommitted
Unify duplicated group attributes handling
1 parent 662fa88 commit f19666d

File tree

4 files changed

+62
-100
lines changed

4 files changed

+62
-100
lines changed

dnf5daemon-server/group.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
2626

2727
// map string group attribute name to actual attribute
2828
const std::map<std::string, GroupAttribute> group_attributes{
29-
{"groupid", GroupAttribute::groupid}, {"name", GroupAttribute::name}, {"description", GroupAttribute::description}};
29+
{"groupid", GroupAttribute::groupid},
30+
{"name", GroupAttribute::name},
31+
{"description", GroupAttribute::description},
32+
{"order", GroupAttribute::order},
33+
{"order_int", GroupAttribute::order_int},
34+
{"langonly", GroupAttribute::langonly},
35+
{"uservisible", GroupAttribute::uservisible},
36+
{"default", GroupAttribute::is_default},
37+
{"packages", GroupAttribute::packages},
38+
{"installed", GroupAttribute::installed},
39+
{"repos", GroupAttribute::repos},
40+
};
3041

31-
32-
dnfdaemon::KeyValueMap group_to_map(
33-
const libdnf5::comps::Group & libdnf_group, const std::vector<std::string> & attributes) {
42+
dnfdaemon::KeyValueMap group_to_map(libdnf5::comps::Group & libdnf_group, const std::vector<std::string> & attributes) {
3443
dnfdaemon::KeyValueMap dbus_group;
3544
// add group id by default
3645
dbus_group.emplace(std::make_pair("groupid", libdnf_group.get_groupid()));
@@ -50,6 +59,42 @@ dnfdaemon::KeyValueMap group_to_map(
5059
case GroupAttribute::description:
5160
dbus_group.emplace(attr, libdnf_group.get_description());
5261
break;
62+
case GroupAttribute::order:
63+
dbus_group.emplace(attr, libdnf_group.get_order());
64+
break;
65+
case GroupAttribute::order_int:
66+
dbus_group.emplace(attr, libdnf_group.get_order_int());
67+
break;
68+
case GroupAttribute::langonly:
69+
dbus_group.emplace(attr, libdnf_group.get_langonly());
70+
break;
71+
case GroupAttribute::uservisible:
72+
dbus_group.emplace(attr, libdnf_group.get_uservisible());
73+
break;
74+
case GroupAttribute::is_default:
75+
dbus_group.emplace(attr, libdnf_group.get_default());
76+
break;
77+
case GroupAttribute::installed:
78+
dbus_group.emplace(attr, libdnf_group.get_installed());
79+
break;
80+
case GroupAttribute::repos: {
81+
auto repos_set = libdnf_group.get_repos();
82+
std::vector<std::string> repos(repos_set.begin(), repos_set.end());
83+
dbus_group.emplace(attr, repos);
84+
break;
85+
}
86+
case GroupAttribute::packages: {
87+
dnfdaemon::KeyValueMapList packages;
88+
for (auto pkg : libdnf_group.get_packages()) {
89+
dnfdaemon::KeyValueMap package;
90+
package.emplace("name", pkg.get_name());
91+
package.emplace("type", static_cast<int>(pkg.get_type()));
92+
package.emplace("condition", pkg.get_condition());
93+
packages.push_back(std::move(package));
94+
}
95+
dbus_group.emplace(attr, packages);
96+
break;
97+
}
5398
}
5499
}
55100
return dbus_group;

dnf5daemon-server/group.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
3131
enum class GroupAttribute {
3232
groupid,
3333
name,
34-
description
34+
description,
3535
// TODO(mblaha): translated_name, translated_description, packages, reason
36+
order,
37+
order_int,
38+
langonly,
39+
uservisible,
40+
is_default,
41+
packages,
42+
installed,
43+
repos,
3644
};
3745

38-
dnfdaemon::KeyValueMap group_to_map(
39-
const libdnf5::comps::Group & libdnf_group, const std::vector<std::string> & attributes);
46+
dnfdaemon::KeyValueMap group_to_map(libdnf5::comps::Group & libdnf_group, const std::vector<std::string> & attributes);
4047

4148
#endif

dnf5daemon-server/services/comps/group.cpp

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -19,107 +19,16 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
1919

2020
#include "group.hpp"
2121

22+
#include "../../group.hpp"
2223
#include "dbus.hpp"
2324

2425
#include <libdnf5/common/sack/query_cmp.hpp>
2526
#include <libdnf5/comps/group/group.hpp>
2627
#include <libdnf5/comps/group/query.hpp>
2728
#include <sdbus-c++/sdbus-c++.h>
2829

29-
#include <iostream>
3030
#include <string>
3131

32-
enum class GroupAttribute {
33-
groupid,
34-
name,
35-
description,
36-
// TODO(mblaha): translated name / translated description
37-
order,
38-
order_int,
39-
langonly,
40-
uservisible,
41-
is_default,
42-
packages,
43-
installed,
44-
repos,
45-
};
46-
47-
// map string group attribute name to actual attribute
48-
const std::map<std::string, GroupAttribute> group_attributes{
49-
{"groupid", GroupAttribute::groupid},
50-
{"name", GroupAttribute::name},
51-
{"description", GroupAttribute::description},
52-
{"order", GroupAttribute::order},
53-
{"order_int", GroupAttribute::order_int},
54-
{"langonly", GroupAttribute::langonly},
55-
{"uservisible", GroupAttribute::uservisible},
56-
{"default", GroupAttribute::is_default},
57-
{"packages", GroupAttribute::packages},
58-
{"installed", GroupAttribute::installed},
59-
{"repos", GroupAttribute::repos},
60-
};
61-
62-
dnfdaemon::KeyValueMap group_to_map(libdnf5::comps::Group & libdnf_group, const std::vector<std::string> & attributes) {
63-
dnfdaemon::KeyValueMap dbus_group;
64-
// add group id by default
65-
dbus_group.emplace(std::make_pair("groupid", libdnf_group.get_groupid()));
66-
// attributes required by client
67-
for (auto & attr : attributes) {
68-
auto it = group_attributes.find(attr);
69-
if (it == group_attributes.end()) {
70-
throw std::runtime_error(fmt::format("Group attribute '{}' not supported", attr));
71-
}
72-
switch (it->second) {
73-
case GroupAttribute::groupid:
74-
// groupid is always included
75-
break;
76-
case GroupAttribute::name:
77-
dbus_group.emplace(attr, libdnf_group.get_name());
78-
break;
79-
case GroupAttribute::description:
80-
dbus_group.emplace(attr, libdnf_group.get_description());
81-
break;
82-
case GroupAttribute::order:
83-
dbus_group.emplace(attr, libdnf_group.get_order());
84-
break;
85-
case GroupAttribute::order_int:
86-
dbus_group.emplace(attr, libdnf_group.get_order_int());
87-
break;
88-
case GroupAttribute::langonly:
89-
dbus_group.emplace(attr, libdnf_group.get_langonly());
90-
break;
91-
case GroupAttribute::uservisible:
92-
dbus_group.emplace(attr, libdnf_group.get_uservisible());
93-
break;
94-
case GroupAttribute::is_default:
95-
dbus_group.emplace(attr, libdnf_group.get_default());
96-
break;
97-
case GroupAttribute::installed:
98-
dbus_group.emplace(attr, libdnf_group.get_installed());
99-
break;
100-
case GroupAttribute::repos: {
101-
auto repos_set = libdnf_group.get_repos();
102-
std::vector<std::string> repos(repos_set.begin(), repos_set.end());
103-
dbus_group.emplace(attr, repos);
104-
break;
105-
}
106-
case GroupAttribute::packages: {
107-
dnfdaemon::KeyValueMapList packages;
108-
for (auto pkg : libdnf_group.get_packages()) {
109-
dnfdaemon::KeyValueMap package;
110-
package.emplace("name", pkg.get_name());
111-
package.emplace("type", static_cast<int>(pkg.get_type()));
112-
package.emplace("condition", pkg.get_condition());
113-
packages.push_back(std::move(package));
114-
}
115-
dbus_group.emplace(attr, packages);
116-
break;
117-
}
118-
}
119-
}
120-
return dbus_group;
121-
}
122-
12332

12433
void Group::dbus_register() {
12534
auto dbus_object = session.get_dbus_object();

dnf5daemon-server/services/goal/goal.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,13 @@ sdbus::MethodReply Goal::resolve(sdbus::MethodCall & call) {
237237
std::vector<std::string> grp_attrs{"name"};
238238
dnfdaemon::KeyValueMap trans_item_attrs{};
239239
for (auto & tsgrp : transaction.get_transaction_groups()) {
240+
auto group = tsgrp.get_group();
240241
dbus_transaction.push_back(dnfdaemon::DbusTransactionItem(
241242
dbus_transaction_item_type_to_string(dnfdaemon::DbusTransactionItemType::GROUP),
242243
transaction_item_action_to_string(tsgrp.get_action()),
243244
transaction_item_reason_to_string(tsgrp.get_reason()),
244245
trans_item_attrs,
245-
group_to_map(tsgrp.get_group(), grp_attrs)));
246+
group_to_map(group, grp_attrs)));
246247
}
247248
for (auto & tsenv : transaction.get_transaction_environments()) {
248249
dbus_transaction.push_back(dnfdaemon::DbusTransactionItem(

0 commit comments

Comments
 (0)