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

mendersoftware / mender / 1056318808

31 Oct 2023 02:09PM UTC coverage: 80.209% (+0.04%) from 80.167%
1056318808

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>
(cherry picked from commit dd09332c7)

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

6906 of 8610 relevant lines covered (80.21%)

9345.51 hits per line

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

92.86
/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) {
7✔
31
        procs::Process proc({identity_data_generator});
35✔
32
        auto ex_line_data = proc.GenerateLineData();
7✔
33
        if (!ex_line_data) {
7✔
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());
7✔
39
        return ex_key_values;
40
}
41

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

72
        return str;
7✔
73
}
74

75
} // namespace identity_parser
76
} // namespace common
77
} // 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