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

mendersoftware / mender / 1507843000

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

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.06 hits per line

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

77.88
/src/common/yaml/platform/yaml-cpp/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 <fstream>
18
#include <map>
19
#include <string>
20

21
#include <yaml-cpp/yaml.h>
22

23
#include <common/io.hpp>
24

25
using namespace std;
26

27
namespace expected = mender::common::expected;
28
namespace error = mender::common::error;
29
namespace io = mender::common::io;
30

31
namespace mender {
32
namespace common {
33
namespace yaml {
34

35
ExpectedYaml LoadFromFile(string file_path) {
3✔
36
        ifstream f;
6✔
37
        errno = 0;
3✔
38
        f.open(file_path);
3✔
39
        if (not f) {
3✔
40
                int io_errno = errno;
1✔
41
                auto err = error::Error(
42
                        std::generic_category().default_error_condition(io_errno),
2✔
43
                        "Failed to open '" + file_path + "': " + strerror(io_errno));
2✔
44
                return expected::unexpected(err);
2✔
45
        }
46

47
        try {
48
                YAML::Node loaded_yaml = YAML::LoadFile(file_path);
3✔
49
                return loaded_yaml;
1✔
50
        } catch (YAML::Exception &e) {
1✔
51
                return expected::unexpected(
1✔
52
                        MakeError(ParseError, "Failed to parse '" + file_path + "'" + e.what()));
3✔
53
        }
54
}
55

56
ExpectedYaml Load(string yaml_str) {
18✔
57
        try {
58
                YAML::Node loaded_yaml = YAML::Load(yaml_str);
34✔
59
                return loaded_yaml;
16✔
60
        } catch (YAML::Exception &e) {
2✔
61
                return expected::unexpected(
2✔
62
                        MakeError(ParseError, "Failed to parse '" + yaml_str + "'" + e.what()));
6✔
63
        }
64
}
65

66
ExpectedYaml Load(istream &str) {
4✔
67
        try {
68
                YAML::Node loaded_yaml = YAML::Load(str);
6✔
69
                return loaded_yaml;
2✔
70
        } catch (YAML::Exception &e) {
4✔
71
                return expected::unexpected(
2✔
72
                        MakeError(ParseError, "Failed to parse YAML from stream: " + string(e.what())));
6✔
73
        }
74
}
75

76
ExpectedYaml Load(io::Reader &reader) {
2✔
77
        auto str_ptr = reader.GetStream();
4✔
78
        return Load(*str_ptr);
4✔
79
}
80

81
bool Yaml::IsObject() const {
6✔
82
        return n_yaml.IsMap();
6✔
83
}
84
bool Yaml::IsArray() const {
3✔
85
        return n_yaml.IsSequence();
3✔
86
}
87

88
template <typename T>
89
bool Is(const YAML::Node &n) {
5✔
90
        try {
91
                n.as<T>();
5✔
92
                return true;
5✔
93
        } catch (YAML::Exception &e) {
×
94
                return false;
95
        }
96
}
97

98
bool Yaml::IsString() const {
5✔
99
        return Is<string>(this->n_yaml);
5✔
100
}
101
bool Yaml::IsInt64() const {
2✔
102
        return Is<int64_t>(this->n_yaml);
2✔
103
}
104
bool Yaml::IsDouble() const {
×
105
        return Is<double>(this->n_yaml);
×
106
}
107
bool Yaml::IsNumber() const {
×
108
        if (not n_yaml.IsScalar()) {
×
109
                return false;
110
        }
NEW
111
        return IsInt64() or IsDouble();
×
112
}
113
bool Yaml::IsBool() const {
2✔
114
        return Is<bool>(this->n_yaml);
2✔
115
}
116
bool Yaml::IsNull() const {
5✔
117
        return this->n_yaml.IsNull();
5✔
118
}
119

120
string Yaml::Dump(const int indent) const {
1✔
121
        stringstream ss {};
2✔
122
        YAML::Emitter out {ss};
2✔
123
        out.SetIndent(indent);
1✔
124
        out << n_yaml;
1✔
125
        return ss.str();
1✔
126
}
127

128
string GetYamlNodeType(YAML::Node n) {
2✔
129
        switch (n.Type()) {
2✔
130
        case YAML::NodeType::Map:
131
                return "Map";
1✔
132
        case YAML::NodeType::Undefined:
133
                return "Undefined";
×
134
        case YAML::NodeType::Null:
135
                return "Null";
×
136
        case YAML::NodeType::Sequence:
137
                return "Sequence";
×
138
        case YAML::NodeType::Scalar:
139
                return "Scalar";
1✔
140
        }
141
        assert(false); // Should never happen
142
        return "Unknown";
×
143
}
144

145
string Yaml::GetType() const {
×
146
        return GetYamlNodeType(this->n_yaml);
×
147
}
148

149
ExpectedYaml Yaml::Get(const char *child_key) const {
26✔
150
        if (not n_yaml[child_key]) {
26✔
151
                return expected::unexpected(
2✔
152
                        MakeError(KeyError, "Key '" + string(child_key) + "' doesn't exist"));
6✔
153
        }
154
        return YAML::Clone(n_yaml[child_key]);
24✔
155
}
156

157
ExpectedYaml Yaml::Get(const size_t idx) const {
8✔
158
        if (not n_yaml.IsSequence()) {
8✔
159
                return expected::unexpected(MakeError(
×
160
                        TypeError,
161
                        "The YAML node is not a Sequence. Unable to index it. The node is a: "
162
                                + GetYamlNodeType(n_yaml)));
×
163
        }
164
        if (not n_yaml[idx]) {
8✔
165
                return expected::unexpected(
1✔
166
                        MakeError(IndexError, "Index " + to_string(idx) + " out of range"));
3✔
167
        }
168
        return n_yaml[idx];
7✔
169
}
170

171
ExpectedSize Yaml::GetArraySize() const {
5✔
172
        if (not n_yaml.IsSequence()) {
5✔
173
                return expected::unexpected(MakeError(
2✔
174
                        TypeError, "The YAML node is a '" + GetYamlNodeType(n_yaml) + "', not a Sequence"));
6✔
175
        }
176
        return n_yaml.size();
3✔
177
}
178

179
ExpectedChildrenMap Yaml::GetChildren() const {
3✔
180
        if (not this->IsObject()) {
3✔
181
                return expected::unexpected(MakeError(
×
182
                        TypeError,
183
                        "The YAML node is a '" + GetYamlNodeType(n_yaml) + "', not an Map (Object)"));
×
184
        }
185
        ChildrenMap map {};
186
        for (const auto &item : this->n_yaml) {
12✔
187
                map[item.first.Scalar()] = item.second;
27✔
188
        }
189
        return map;
3✔
190
}
191

192
template <typename T>
193
string ToString();
194

195
template <>
196
string ToString<string>() {
×
197
        return "string";
×
198
}
199
template <>
200
string ToString<int64_t>() {
1✔
201
        return "integer";
1✔
202
}
203
template <>
204
string ToString<bool>() {
2✔
205
        return "bool";
2✔
206
}
207
template <>
208
string ToString<double>() {
×
209
        return "double";
×
210
}
211

212
template <typename T>
213
typename enable_if<
214
        not is_integral<T>::value or is_same<T, int64_t>::value or is_same<T, bool>::value,
215
        expected::expected<T, error::Error>>::type
216
Yaml::Get() const {
7✔
217
        try {
218
                return n_yaml.as<T>();
14✔
219
        } catch (YAML::Exception &e) {
×
220
                return expected::unexpected(
×
221
                        MakeError(TypeError, "The YAML node is not a " + ToString<T>()));
×
222
        }
223
}
224

225
template expected::expected<string, error::Error> Yaml::Get<string>() const;
226

227
template expected::expected<int64_t, error::Error> Yaml::Get<int64_t>() const;
228

229
template expected::expected<double, error::Error> Yaml::Get<double>() const;
230

231
template expected::expected<bool, error::Error> Yaml::Get<bool>() const;
232

233
} // namespace yaml
234
} // namespace common
235
} // 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