• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1057363609

01 Nov 2023 08:48AM UTC coverage: 80.195% (-0.01%) from 80.209%
1057363609

push

gitlab-ci

lluiscampos
fix: Serialize identity keys as single strings when possible

Use similar logic than for inventory keys: if the vector has one
element serialize as string, else submit as a list of strings.

Changelog: None
Ticket: MEN-681

Signed-off-by: Lluis Campos <lluis.campos@northern.tech>

21 of 21 new or added lines in 2 files covered. (100.0%)

6904 of 8609 relevant lines covered (80.2%)

9346.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/src/common/identity_parser/identity_parser.cpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#include <common/identity_parser.hpp>
16

17
#include <common/common.hpp>
18
#include <common/json.hpp>
19
#include <common/key_value_parser.hpp>
20
#include <common/processes.hpp>
21

22
namespace mender {
23
namespace common {
24
namespace identity_parser {
25

26
using namespace std;
27
namespace kvp = mender::common::key_value_parser;
28
namespace procs = mender::common::processes;
29

30
kvp::ExpectedKeyValuesMap GetIdentityData(const string &identity_data_generator) {
8✔
31
        procs::Process proc({identity_data_generator});
40✔
32
        auto ex_line_data = proc.GenerateLineData();
8✔
33
        if (!ex_line_data) {
8✔
34
                return expected::unexpected(
×
35
                        ex_line_data.error().WithContext("While getting identity data"));
×
36
        }
37

38
        auto ex_key_values = kvp::ParseKeyValues(ex_line_data.value());
8✔
39
        return ex_key_values;
40
}
41

42
string DumpIdentityData(kvp::KeyValuesMap &identity_data) {
9✔
43
        stringstream top_ss;
18✔
44
        top_ss << "{";
9✔
45
        auto key_vector = common::GetMapKeyVector(identity_data);
18✔
46
        std::sort(key_vector.begin(), key_vector.end());
9✔
47
        for (const auto &key : key_vector) {
31✔
48
                top_ss << R"(")" << json::EscapeString(key) << R"(":)";
44✔
49
                if (identity_data[key].size() == 1) {
22✔
50
                        top_ss << R"(")" + json::EscapeString(identity_data[key][0]) + R"(")";
30✔
51
                } else {
52
                        stringstream items_ss;
14✔
53
                        items_ss << "[";
7✔
54
                        for (const auto &str : identity_data[key]) {
21✔
55
                                items_ss << R"(")" + json::EscapeString(str) + R"(",)";
28✔
56
                        }
57
                        auto items_str = items_ss.str();
58
                        // replace the trailing comma with the closing square bracket
59
                        items_str[items_str.size() - 1] = ']';
7✔
60
                        top_ss << items_str;
7✔
61
                }
62
                top_ss << ",";
22✔
63
        }
64
        auto str = top_ss.str();
65
        if (str[str.size() - 1] == ',') {
9✔
66
                // replace the trailing comma with the closing curly bracket
67
                str.pop_back();
8✔
68
        }
69
        str.push_back('}');
9✔
70

71
        return str;
9✔
72
}
73

74
} // namespace identity_parser
75
} // namespace common
76
} // namespace mender
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc