summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/base_maps/entity_name.hpp
blob: e2df632e2c33eb920f894e462ee86c3bea9d1053 (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
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_BASE_MAPS_ENTITY_NAME_HPP
#define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_BASE_MAPS_ENTITY_NAME_HPP

#include <filesystem>
#include <optional>
#include <utility>

#include "gsl/gsl"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/base_maps/entity_name_data.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/buildtool/common/repository_config.hpp"
#include "src/utils/cpp/hash_combine.hpp"

namespace BuildMaps::Base {

static inline auto IsString(const nlohmann::json& x) noexcept -> bool {
    return x.is_string();
}

static inline auto IsString(const ExpressionPtr& x) noexcept -> bool {
    return x->IsString();
}

// for compatibility with ExpressionPtr
static inline auto GetList(const nlohmann::json& x) noexcept
    -> nlohmann::json const& {
    return x;
}

static inline auto GetList(const ExpressionPtr& x) noexcept
    -> Expression::list_t const& {
    return x->Value<Expression::list_t>()->get();
}

static inline auto IsList(const nlohmann::json& x) noexcept -> bool {
    return x.is_array();
}

static inline auto IsList(const ExpressionPtr& x) noexcept -> bool {
    return x->IsList();
}

template <typename T>
auto Size(const T& x) noexcept -> std::size_t {
    return x.size();
}

static inline auto GetString(const nlohmann::json& x) -> std::string {
    return x.template get<std::string>();
}

static inline auto GetString(const ExpressionPtr& x) -> const std::string& {
    return x->String();
}

static inline auto ToString(const nlohmann::json& x) -> std::string {
    return x.dump();
}

static inline auto ToString(const ExpressionPtr& x) noexcept -> std::string {
    return x->ToString();
}

static inline auto IsNone(const nlohmann::json& x) noexcept -> bool {
    return x.is_null();
}

static inline auto IsNone(const ExpressionPtr& x) noexcept -> bool {
    return x->IsNone();
}

template <typename T>
// IsList(list) == true and Size(list) == 2
[[nodiscard]] inline auto ParseEntityName_2(T const& list,
                                            EntityName const& current) noexcept
    -> std::optional<EntityName> {
    try {
        if (IsString(list[0]) and IsString(list[1])) {
            return EntityName{current.GetNamedTarget().repository,
                              GetString(list[0]),
                              GetString(list[1])};
        }

    } catch (...) {
    }
    return std::nullopt;
}

template <typename T>
// IsList(list) == true
[[nodiscard]] inline auto ParseEntityNameFSReference(
    std::string const& s0,  // list[0]
    T const& list,
    std::size_t const list_size,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        bool const is_file = s0 == EntityName::kFileLocationMarker;
        auto const ref_type =
            s0 == EntityName::kFileLocationMarker
                ? ReferenceType::kFile
                : (s0 == EntityName::kGlobMarker ? ReferenceType::kGlob
                                                 : ReferenceType::kTree);
        if (list_size == 3) {
            if (IsString(list[2])) {
                auto const& name = GetString(list[2]);
                auto const& x = current.GetNamedTarget();
                if (IsNone(list[1])) {
                    return EntityName{x.repository, x.module, name, ref_type};
                }
                if (IsString(list[1])) {
                    auto const& middle = GetString(list[1]);
                    if (middle == "." or middle == x.module) {
                        return EntityName{
                            x.repository, x.module, name, ref_type};
                    }
                }
                if (logger) {
                    (*logger)(
                        fmt::format("Invalid module name {} for file reference",
                                    ToString(list[1])));
                }
            }
        }
    } catch (...) {
    }
    return std::nullopt;
}
template <typename T>
// IsList(list) == true
// list[0] == kRelativeLocationMarker
[[nodiscard]] inline auto ParseEntityNameRelative(
    T const& list,
    std::size_t const list_size,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        if (list_size == 3 and IsString(list[1]) and IsString(list[2])) {
            auto const& relmodule = GetString(list[1]);
            auto const& name = GetString(list[2]);

            std::filesystem::path m{current.GetNamedTarget().module};
            auto const& module = (m / relmodule).lexically_normal().string();
            if (module.compare(0, 3, "../") != 0) {
                return EntityName{
                    current.GetNamedTarget().repository, module, name};
            }
            if (logger) {
                (*logger)(fmt::format(
                    "Relative module name {} is outside of workspace",
                    relmodule));
            }
        }
    } catch (...) {
    }
    return std::nullopt;
}

template <typename T>
// IsList(list) == true
// list[0] == EntityName::kLocationMarker
[[nodiscard]] inline auto ParseEntityNameLocation(
    T const& list,
    std::size_t const list_size,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        if (list_size == 4 and IsString(list[1]) and IsString(list[2]) and
            IsString(list[3])) {
            auto const& local_repo_name = GetString(list[1]);
            auto const& module = GetString(list[2]);
            auto const& target = GetString(list[3]);
            auto const* repo_name = RepositoryConfig::Instance().GlobalName(
                current.GetNamedTarget().repository, local_repo_name);
            if (repo_name != nullptr) {
                return EntityName{*repo_name, module, target};
            }
            if (logger) {
                (*logger)(fmt::format("Cannot resolve repository name {}",
                                      local_repo_name));
            }
        }

    } catch (...) {
    }
    return std::nullopt;
}

template <typename T>
// IsList(list) == true and Size(list) >= 3
[[nodiscard]] inline auto ParseEntityName_3(
    T const& list,
    std::size_t const list_size,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        // the first entry of the list must be a string
        if (IsString(list[0])) {
            auto const& s0 = GetString(list[0]);
            if (s0 == EntityName::kRelativeLocationMarker) {
                return ParseEntityNameRelative(
                    list, list_size, current, logger);
            }
            if (s0 == EntityName::kLocationMarker) {
                return ParseEntityNameLocation(
                    list, list_size, current, logger);
            }
            if (s0 == EntityName::kAnonymousMarker and logger) {
                (*logger)(fmt::format(
                    "Parsing anonymous target is not "
                    "supported. Identifiers of anonymous targets should be "
                    "obtained as FIELD value of anonymous fields"));
            }
            else if (s0 == EntityName::kFileLocationMarker or
                     s0 == EntityName::kTreeLocationMarker or
                     s0 == EntityName::kGlobMarker) {
                return ParseEntityNameFSReference(
                    s0, list, list_size, current, logger);
            }
        }
    } catch (...) {
    }
    return std::nullopt;
}

template <typename T>
[[nodiscard]] inline auto ParseEntityName(
    T const& source,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    try {
        std::optional<EntityName> res = std::nullopt;
        if (IsString(source)) {
            const auto& x = current.GetNamedTarget();
            return EntityName{x.repository, x.module, GetString(source)};
        }
        if (IsList(source)) {
            auto const& list = GetList(source);
            const auto list_size = Size(list);
            if (list_size == 2) {
                res = ParseEntityName_2(list, current);
            }
            else if (list_size >= 3) {
                res = ParseEntityName_3(list, list_size, current, logger);
            }
        }
        if (logger and (res == std::nullopt)) {
            (*logger)(fmt::format("Syntactically invalid entity name: {}.",
                                  ToString(source)));
        }
        return res;
    } catch (...) {
    }
    return std::nullopt;
}

[[nodiscard]] inline auto ParseEntityNameFromJson(
    nlohmann::json const& json,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    return ParseEntityName(json, current, std::move(logger));
}

[[nodiscard]] inline auto ParseEntityNameFromExpression(
    ExpressionPtr const& expr,
    EntityName const& current,
    std::optional<std::function<void(std::string const&)>> logger =
        std::nullopt) noexcept -> std::optional<EntityName> {
    return ParseEntityName(expr, current, std::move(logger));
}

}  // namespace BuildMaps::Base

#endif