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

mendersoftware / mender / 1504885822

21 Oct 2024 08:08AM UTC coverage: 76.257% (-0.05%) from 76.305%
1504885822

push

gitlab-ci

web-flow
Merge pull request #1676 from kacf/size_fixes

MEN-7613: Size fixes

48 of 72 new or added lines in 18 files covered. (66.67%)

3 existing lines in 2 files now uncovered.

7313 of 9590 relevant lines covered (76.26%)

11280.18 hits per line

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

54.9
/src/common/yaml/yaml.cpp
1
// Copyright 2024 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/yaml.hpp>
16

17
#include <string>
18
#include <unordered_map>
19
#include <vector>
20

21
namespace mender {
22
namespace common {
23
namespace yaml {
24

25
const YamlErrorCategoryClass YamlErrorCategory;
26

27
const char *YamlErrorCategoryClass::name() const noexcept {
×
28
        return "YamlErrorCategory";
×
29
}
30

31
string YamlErrorCategoryClass::message(int code) const {
×
32
        switch (code) {
×
33
        case NoError:
34
                return "Success";
×
35
        case ParseError:
36
                return "Parse error";
×
37
        case KeyError:
38
                return "Key error";
×
39
        case IndexError:
40
                return "Index error";
×
41
        case TypeError:
42
                return "Type error";
×
43
        default:
44
                return "Unknown";
×
45
        }
46
}
47

48
error::Error MakeError(YamlErrorCode code, const string &msg) {
25✔
49
        return error::Error(error_condition(code, YamlErrorCategory), msg);
25✔
50
}
51

52
std::ostream &operator<<(std::ostream &out, const YamlErrorCode value) {
×
53
        return out << YamlErrorCategoryClass().message(value);
×
54
}
55

56
ExpectedString ToString(const yaml::Yaml &y) {
2✔
57
        return y.Get<string>();
2✔
58
}
59

60
ExpectedStringVector ToStringVector(const yaml::Yaml &y) {
1✔
61
        if (not y.IsArray()) {
1✔
62
                return expected::unexpected(
×
63
                        MakeError(YamlErrorCode::ParseError, "The YAML object is not an array"));
×
64
        }
65
        vector<string> vector_elements {};
1✔
66
        size_t vector_size {y.GetArraySize().value()};
1✔
67
        for (size_t i = 0; i < vector_size; ++i) {
3✔
68
                auto element = y.Get(i).and_then(ToString);
4✔
69
                if (not element) {
2✔
70
                        return expected::unexpected(element.error());
×
71
                }
72
                vector_elements.push_back(element.value());
2✔
73
        }
74
        return vector_elements;
1✔
75
}
76

77
ExpectedKeyValueMap ToKeyValueMap(const yaml::Yaml &y) {
1✔
78
        if (not y.IsObject()) {
1✔
79
                return expected::unexpected(
×
80
                        MakeError(YamlErrorCode::ParseError, "The YAML is not an object"));
×
81
        }
82

83
        auto expected_children = y.GetChildren();
1✔
84
        if (not expected_children) {
1✔
85
                return expected::unexpected(expected_children.error());
×
86
        }
87

88
        unordered_map<string, string> kv_map {};
1✔
89

90
        for (const auto &kv : expected_children.value()) {
2✔
91
                auto expected_value = kv.second.Get<string>();
1✔
92
                if (not expected_value) {
1✔
93
                        return expected::unexpected(expected_value.error());
×
94
                }
95
                const string key = kv.first;
1✔
96
                kv_map[key] = expected_value.value();
1✔
97
        }
98

99
        return kv_map;
1✔
100
}
101

NEW
102
ExpectedInt64 ToInt64(const yaml::Yaml &y) {
×
103
        return y.Get<int64_t>();
×
104
}
105

106
ExpectedBool ToBool(const yaml::Yaml &y) {
×
107
        return y.Get<bool>();
×
108
}
109

110
template <>
111
expected::expected<KeyValueMap, error::Error> Yaml::Get<KeyValueMap>() const {
1✔
112
        return ToKeyValueMap(*this);
1✔
113
}
114

115
template <>
116
expected::expected<vector<string>, error::Error> Yaml::Get<vector<string>>() const {
1✔
117
        return ToStringVector(*this);
1✔
118
}
119

120
} // namespace yaml
121
} // namespace common
122
} // 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