Added regex support to parents filter

This commit is contained in:
Bartek Kryza
2023-06-08 00:03:27 +02:00
parent ad2fc3f8a6
commit b3b95efb65
11 changed files with 306 additions and 139 deletions

View File

@@ -173,7 +173,7 @@ tvl::value_t anyof_filter::match(
}
namespace_filter::namespace_filter(
filter_t type, std::vector<config::namespace_or_regex> namespaces)
filter_t type, std::vector<common::namespace_or_regex> namespaces)
: filter_visitor{type}
, namespaces_{std::move(namespaces)}
{
@@ -192,7 +192,7 @@ tvl::value_t namespace_filter::match(
return ns.starts_with(ns_pattern) || ns == ns_pattern;
}
else {
const auto &regex = std::get<config::regex>(nsit.value());
const auto &regex = std::get<common::regex>(nsit.value());
return regex == ns.to_string();
}
});
@@ -226,7 +226,7 @@ tvl::value_t namespace_filter::match(
return result;
}
else {
return std::get<config::regex>(nsit.value()) ==
return std::get<common::regex>(nsit.value()) ==
e.full_name(false);
}
});
@@ -239,14 +239,14 @@ tvl::value_t namespace_filter::match(
std::get<namespace_>(nsit.value()));
}
else {
return std::get<config::regex>(nsit.value()) ==
return std::get<common::regex>(nsit.value()) ==
e.full_name(false);
}
});
}
element_filter::element_filter(
filter_t type, std::vector<config::string_or_regex> elements)
filter_t type, std::vector<common::string_or_regex> elements)
: filter_visitor{type}
, elements_{std::move(elements)}
{
@@ -312,7 +312,7 @@ tvl::value_t method_type_filter::match(
}
subclass_filter::subclass_filter(
filter_t type, std::vector<config::string_or_regex> roots)
filter_t type, std::vector<common::string_or_regex> roots)
: filter_visitor{type}
, roots_{std::move(roots)}
{
@@ -361,7 +361,8 @@ tvl::value_t subclass_filter::match(const diagram &d, const element &e) const
return false;
}
parents_filter::parents_filter(filter_t type, std::vector<std::string> children)
parents_filter::parents_filter(
filter_t type, std::vector<common::string_or_regex> children)
: filter_visitor{type}
, children_{std::move(children)}
{
@@ -383,11 +384,13 @@ tvl::value_t parents_filter::match(const diagram &d, const element &e) const
// First get all parents of element e
clanguml::common::reference_set<class_diagram::model::class_> parents;
for (const auto &child : children_) {
auto child_ref = cd.find<class_diagram::model::class_>(child);
if (!child_ref.has_value())
continue;
parents.emplace(child_ref.value());
for (const auto &child_pattern : children_) {
auto child_refs = cd.find<class_diagram::model::class_>(child_pattern);
for (auto &child : child_refs) {
if (child.has_value())
parents.emplace(child.value());
}
}
cd.get_parents(parents);

View File

@@ -113,7 +113,7 @@ private:
struct namespace_filter : public filter_visitor {
namespace_filter(
filter_t type, std::vector<config::namespace_or_regex> namespaces);
filter_t type, std::vector<common::namespace_or_regex> namespaces);
~namespace_filter() override = default;
@@ -122,19 +122,19 @@ struct namespace_filter : public filter_visitor {
tvl::value_t match(const diagram &d, const element &e) const override;
private:
std::vector<config::namespace_or_regex> namespaces_;
std::vector<common::namespace_or_regex> namespaces_;
};
struct element_filter : public filter_visitor {
element_filter(
filter_t type, std::vector<config::string_or_regex> elements);
filter_t type, std::vector<common::string_or_regex> elements);
~element_filter() override = default;
tvl::value_t match(const diagram &d, const element &e) const override;
private:
std::vector<config::string_or_regex> elements_;
std::vector<common::string_or_regex> elements_;
};
struct element_type_filter : public filter_visitor {
@@ -162,25 +162,25 @@ private:
};
struct subclass_filter : public filter_visitor {
subclass_filter(filter_t type, std::vector<config::string_or_regex> roots);
subclass_filter(filter_t type, std::vector<common::string_or_regex> roots);
~subclass_filter() override = default;
tvl::value_t match(const diagram &d, const element &e) const override;
private:
std::vector<config::string_or_regex> roots_;
std::vector<common::string_or_regex> roots_;
};
struct parents_filter : public filter_visitor {
parents_filter(filter_t type, std::vector<std::string> roots);
parents_filter(filter_t type, std::vector<common::string_or_regex> roots);
~parents_filter() override = default;
tvl::value_t match(const diagram &d, const element &e) const override;
private:
std::vector<std::string> children_;
std::vector<common::string_or_regex> children_;
};
template <typename DiagramT, typename ElementT,

27
src/common/types.cc Normal file
View File

@@ -0,0 +1,27 @@
/**
* src/common/types.cc
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* 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.
*/
#include "types.h"
namespace clanguml::common {
std::string to_string(const std::string &s) { return s; }
std::string to_string(string_or_regex sr) { return sr.to_string(); }
};

View File

@@ -1,5 +1,5 @@
/**
* src/class_diagram/visitor/translation_unit_visitor.h
* src/common/types.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
@@ -20,15 +20,21 @@
#include <cassert>
#include <cstdint>
#include <optional>
#include <regex>
#include <unordered_set>
#include <variant>
#include <vector>
#include "model/namespace.h"
namespace clanguml::common {
using id_t = int64_t;
enum class generator_type_t { plantuml, json };
std::string to_string(const std::string &s);
template <typename T> class optional_ref {
public:
using optional_type = T;
@@ -137,4 +143,76 @@ using reference_vector = std::vector<std::reference_wrapper<T>>;
template <typename T>
using reference_set = std::unordered_set<std::reference_wrapper<T>>;
/**
* @brief Wrapper around std::regex, which contains original pattern
*/
struct regex {
regex(std::regex r, std::string p)
: regexp{std::move(r)}
, pattern{std::move(p)}
{
}
[[nodiscard]] bool operator==(const std::string &v) const
{
return std::regex_match(v, regexp);
}
std::regex regexp;
std::string pattern;
};
template <typename T> struct or_regex {
or_regex() = default;
or_regex(T v)
: value_{std::move(v)}
{
}
or_regex(std::regex r, std::string p)
: value_{regex{std::move(r), std::move(p)}}
{
}
or_regex &operator=(const T &v)
{
value_ = v;
return *this;
}
or_regex &operator=(const regex &v)
{
value_ = v;
return *this;
}
[[nodiscard]] bool operator==(const T &v) const
{
if (std::holds_alternative<regex>(value_))
return std::regex_match(v, std::get<regex>(value_).regexp);
return std::get<T>(value_) == v;
}
std::string to_string() const
{
if (std::holds_alternative<regex>(value_))
return std::get<regex>(value_).pattern;
return clanguml::common::to_string(std::get<T>(value_));
}
const std::variant<T, regex> &value() const { return value_; }
private:
std::variant<T, regex> value_;
};
using string_or_regex = or_regex<std::string>;
std::string to_string(string_or_regex sr);
using namespace_or_regex = common::or_regex<common::model::namespace_>;
} // namespace clanguml::common