summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/base_maps/rule_map.cpp
blob: 7bdc14e45d1f12f752c7358e65eba62aa7ab300f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

#include "src/buildtool/build_engine/base_maps/rule_map.hpp"

#include <optional>
#include <string>
#include <unordered_set>

#include "fmt/core.h"
#include "src/buildtool/build_engine/base_maps/field_reader.hpp"

namespace BuildMaps::Base {

namespace {

auto rule_fields = std::unordered_set<std::string>{"anonymous",
                                                   "config_doc",
                                                   "config_fields",
                                                   "config_transitions",
                                                   "config_vars",
                                                   "doc",
                                                   "expression",
                                                   "field_doc",
                                                   "implicit",
                                                   "imports",
                                                   "import",
                                                   "string_fields",
                                                   "tainted",
                                                   "target_fields"};

[[nodiscard]] auto ReadAnonymousObject(EntityName const& id,
                                       nlohmann::json const& json,
                                       AsyncMapConsumerLoggerPtr const& logger)
    -> std::optional<UserRule::anonymous_defs_t> {
    auto obj = GetOrDefault(json, "anonymous", nlohmann::json::object());
    if (not obj.is_object()) {
        (*logger)(
            fmt::format("Field anonymous in rule {} is not an object", id.name),
            true);
        return std::nullopt;
    }

    UserRule::anonymous_defs_t anon_defs{};
    anon_defs.reserve(obj.size());
    for (auto const& [name, def] : obj.items()) {
        if (not def.is_object()) {
            (*logger)(fmt::format("Entry {} in field anonymous in rule {} is "
                                  "not an object",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }

        auto target = def.find("target");
        if (target == def.end()) {
            (*logger)(fmt::format("Entry target for {} in field anonymous in "
                                  "rule {} is missing",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }
        if (not target->is_string()) {
            (*logger)(fmt::format("Entry target for {} in field anonymous in "
                                  "rule {} is not a string",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }

        auto provider = def.find("provider");
        if (provider == def.end()) {
            (*logger)(fmt::format("Entry provider for {} in field anonymous in "
                                  "rule {} is missing",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }
        if (not provider->is_string()) {
            (*logger)(fmt::format("Entry provider for {} in field anonymous in "
                                  "rule {} is not a string",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }

        auto rule_map = def.find("rule_map");
        if (rule_map == def.end()) {
            (*logger)(fmt::format("Entry rule_map for {} in field anonymous in "
                                  "rule {} is missing",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }
        if (not rule_map->is_object()) {
            (*logger)(fmt::format("Entry rule_map for {} in field anonymous in "
                                  "rule {} is not an object",
                                  name,
                                  id.name),
                      true);
            return std::nullopt;
        }

        Expression::map_t::underlying_map_t rule_mapping{};
        for (auto const& [key, val] : rule_map->items()) {
            auto rule_name = ParseEntityNameFromJson(
                val, id, [&logger, &id, &name = name](auto msg) {
                    (*logger)(
                        fmt::format("Parsing rule name for entry {} in field "
                                    "anonymous in rule {} failed with:\n{}",
                                    name,
                                    id.name,
                                    msg),
                        true);
                });
            if (not rule_name) {
                return std::nullopt;
            }
            rule_mapping.emplace(key, ExpressionPtr{std::move(*rule_name)});
        }

        anon_defs.emplace(
            name,
            UserRule::AnonymousDefinition{
                target->get<std::string>(),
                provider->get<std::string>(),
                ExpressionPtr{Expression::map_t{std::move(rule_mapping)}}});
    }
    return anon_defs;
}

[[nodiscard]] auto ReadImplicitObject(EntityName const& id,
                                      nlohmann::json const& json,
                                      AsyncMapConsumerLoggerPtr const& logger)
    -> std::optional<UserRule::implicit_t> {
    auto map = GetOrDefault(json, "implicit", nlohmann::json::object());
    if (not map.is_object()) {
        (*logger)(
            fmt::format("Field implicit in rule {} is not an object", id.name),
            true);
        return std::nullopt;
    }

    auto implicit_targets = UserRule::implicit_t{};
    implicit_targets.reserve(map.size());

    for (auto const& [key, val] : map.items()) {
        if (not val.is_array()) {
            (*logger)(fmt::format("Entry in implicit field of rule {} is not a "
                                  "list.",
                                  id.name),
                      true);
            return std::nullopt;
        }
        auto targets = typename UserRule::implicit_t::mapped_type{};
        targets.reserve(val.size());
        for (auto const& item : val) {
            auto expr_id = ParseEntityNameFromJson(
                item, id, [&logger, &item, &id](std::string const& parse_err) {
                    (*logger)(fmt::format("Parsing entry {} in implicit field "
                                          "of rule {} failed with:\n{}",
                                          item.dump(),
                                          id.name,
                                          parse_err),
                              true);
                });
            if (not expr_id) {
                return std::nullopt;
            }
            targets.emplace_back(*expr_id);
        }
        implicit_targets.emplace(key, targets);
    }
    return implicit_targets;
}

[[nodiscard]] auto ReadConfigTransitionsObject(
    EntityName const& id,
    nlohmann::json const& json,
    std::vector<std::string> const& config_vars,
    ExpressionFunction::imports_t const& imports,
    AsyncMapConsumerLoggerPtr const& logger)
    -> std::optional<UserRule::config_trans_t> {
    auto map =
        GetOrDefault(json, "config_transitions", nlohmann::json::object());
    if (not map.is_object()) {
        (*logger)(
            fmt::format("Field config_transitions in rule {} is not an object",
                        id.name),
            true);
        return std::nullopt;
    }

    auto config_transitions = UserRule::config_trans_t{};
    config_transitions.reserve(map.size());

    for (auto const& [key, val] : map.items()) {
        auto expr = Expression::FromJson(val);
        if (not expr) {
            (*logger)(fmt::format("Failed to create expression for entry {} in "
                                  "config_transitions list of rule {}.",
                                  key,
                                  id.name),
                      true);
            return std::nullopt;
        }
        config_transitions.emplace(
            key,
            std::make_shared<ExpressionFunction>(config_vars, imports, expr));
    }
    return config_transitions;
}

}  // namespace

auto CreateRuleMap(gsl::not_null<RuleFileMap*> const& rule_file_map,
                   gsl::not_null<ExpressionFunctionMap*> const& expr_map,
                   std::size_t jobs) -> UserRuleMap {
    auto user_rule_creator = [rule_file_map, expr_map](auto ts,
                                                       auto setter,
                                                       auto logger,
                                                       auto /*subcaller*/,
                                                       auto const& id) {
        if (not id.IsDefinitionName()) {
            (*logger)(fmt::format("{} cannot name a rule", id.ToString()),
                      true);
            return;
        }
        rule_file_map->ConsumeAfterKeysReady(
            ts,
            {id.ToModule()},
            [ts, expr_map, setter = std::move(setter), logger, id](
                auto json_values) {
                auto rule_it = json_values[0]->find(id.name);
                if (rule_it == json_values[0]->end()) {
                    (*logger)(
                        fmt::format(
                            "Cannot find rule {} in {}", id.name, id.module),
                        true);
                    return;
                }

                auto reader =
                    FieldReader::Create(rule_it.value(), id, "rule", logger);
                if (not reader) {
                    return;
                }
                reader->ExpectFields(rule_fields);

                auto expr = reader->ReadExpression("expression");
                if (not expr) {
                    return;
                }

                auto target_fields = reader->ReadStringList("target_fields");
                if (not target_fields) {
                    return;
                }

                auto string_fields = reader->ReadStringList("string_fields");
                if (not string_fields) {
                    return;
                }

                auto config_fields = reader->ReadStringList("config_fields");
                if (not config_fields) {
                    return;
                }

                auto implicit_targets =
                    ReadImplicitObject(id, rule_it.value(), logger);
                if (not implicit_targets) {
                    return;
                }

                auto anonymous_defs =
                    ReadAnonymousObject(id, rule_it.value(), logger);
                if (not anonymous_defs) {
                    return;
                }

                auto config_vars = reader->ReadStringList("config_vars");
                if (not config_vars) {
                    return;
                }

                auto tainted = reader->ReadStringList("tainted");
                if (not tainted) {
                    return;
                }

                auto import_aliases =
                    reader->ReadEntityAliasesObject("imports");
                if (not import_aliases) {
                    return;
                }
                auto [names, ids] = std::move(*import_aliases).Obtain();

                expr_map->ConsumeAfterKeysReady(
                    ts,
                    std::move(ids),
                    [ts,
                     id,
                     json = rule_it.value(),
                     expr = std::move(expr),
                     target_fields = std::move(*target_fields),
                     string_fields = std::move(*string_fields),
                     config_fields = std::move(*config_fields),
                     implicit_targets = std::move(*implicit_targets),
                     anonymous_defs = std::move(*anonymous_defs),
                     config_vars = std::move(*config_vars),
                     tainted = std::move(*tainted),
                     names = std::move(names),
                     setter = std::move(setter),
                     logger](auto expr_funcs) {
                        auto imports = ExpressionFunction::imports_t{};
                        imports.reserve(expr_funcs.size());
                        for (std::size_t i{}; i < expr_funcs.size(); ++i) {
                            imports.emplace(names[i], *expr_funcs[i]);
                        }

                        auto config_transitions = ReadConfigTransitionsObject(
                            id, json, config_vars, imports, logger);
                        if (not config_transitions) {
                            return;
                        }

                        auto rule = UserRule::Create(
                            target_fields,
                            string_fields,
                            config_fields,
                            implicit_targets,
                            anonymous_defs,
                            config_vars,
                            tainted,
                            std::move(*config_transitions),
                            std::make_shared<ExpressionFunction>(
                                std::move(config_vars),
                                std::move(imports),
                                std::move(expr)),
                            [&logger](auto const& msg) {
                                (*logger)(msg, true);
                            });
                        if (rule) {
                            (*setter)(std::move(rule));
                        }
                    },
                    [logger, id](auto msg, auto fatal) {
                        (*logger)(fmt::format("While reading expression map "
                                              "for rule {} in {}: {}",
                                              id.name,
                                              id.module,
                                              msg),
                                  fatal);
                    });
            },
            [logger, id](auto msg, auto fatal) {
                (*logger)(
                    fmt::format(
                        "While reading rule file in {}: {}", id.module, msg),
                    fatal);
            });
    };
    return UserRuleMap{user_rule_creator, jobs};
}

}  // namespace BuildMaps::Base