Added comment() inja function - Fixes #30

This commit is contained in:
Bartek Kryza
2022-04-02 14:52:00 +02:00
parent 7a8cd5925b
commit 2d9f7c88f5
12 changed files with 94 additions and 6 deletions

View File

@@ -41,6 +41,21 @@ common::model::diagram_t diagram::type() const
return common::model::diagram_t::kClass;
}
type_safe::optional_ref<const clanguml::common::model::element> diagram::get(
const std::string &full_name) const
{
type_safe::optional_ref<const clanguml::common::model::element> res;
res = get_class(full_name);
if (res.has_value())
return res;
res = get_enum(full_name);
return res;
}
bool diagram::has_class(const class_ &c) const
{
return std::any_of(classes_.cbegin(), classes_.cend(),
@@ -65,6 +80,18 @@ type_safe::optional_ref<const class_> diagram::get_class(
return type_safe::nullopt;
}
type_safe::optional_ref<const enum_> diagram::get_enum(
const std::string &name) const
{
for (const auto &e : enums_) {
if (e.get().full_name(false) == name) {
return {e};
}
}
return type_safe::nullopt;
}
void diagram::add_type_alias(std::unique_ptr<type_alias> &&ta)
{
LOG_DBG(

View File

@@ -43,6 +43,9 @@ public:
common::model::diagram_t type() const override;
type_safe::optional_ref<const clanguml::common::model::element> get(
const std::string &full_name) const override;
const std::vector<type_safe::object_ref<const class_>> classes() const;
const std::vector<type_safe::object_ref<const enum_>> enums() const;
@@ -54,6 +57,9 @@ public:
type_safe::optional_ref<const class_> get_class(
const std::string &name) const;
type_safe::optional_ref<const enum_> get_enum(
const std::string &name) const;
void add_type_alias(std::unique_ptr<type_alias> &&ta);
void add_class(std::unique_ptr<class_> &&c);

View File

@@ -325,6 +325,27 @@ template <typename C, typename D> void generator<C, D>::init_env()
return m_model.to_alias(
m_config.using_namespace().relative(alias_match));
});
m_env.add_callback("comment", 1, [this](inja::Arguments &args) {
std::string res{};
auto full_name = args[0]->get<std::string>();
auto element = m_model.get(full_name);
if (!element.has_value()) {
// Try with current using namespace prepended
element = m_model.get(fmt::format(
"{}::{}", m_config.using_namespace().to_string(), full_name));
}
if (element.has_value()) {
auto comment = element.value().comment();
if (comment.has_value())
res = comment.value();
}
return res;
});
}
}

View File

@@ -19,6 +19,8 @@
#include "enums.h"
#include <type_safe/optional_ref.hpp>
#include <memory>
#include <string>
@@ -36,6 +38,9 @@ public:
virtual diagram_t type() const = 0;
virtual type_safe::optional_ref<const element> get(
const std::string &full_name) const = 0;
diagram(const diagram &) = delete;
diagram(diagram &&);
diagram &operator=(const diagram &) = delete;

View File

@@ -80,6 +80,9 @@ inja::json element::context() const
ctx["alias"] = alias();
ctx["full_name"] = full_name(false);
ctx["namespace"] = get_namespace().to_string();
if (comment().has_value())
ctx["comment"] = comment().value();
return ctx;
}

View File

@@ -28,6 +28,12 @@ common::model::diagram_t diagram::type() const
return common::model::diagram_t::kPackage;
}
type_safe::optional_ref<const clanguml::common::model::element> diagram::get(
const std::string &full_name) const
{
return {};
}
std::string diagram::to_alias(const std::string &full_name) const
{
LOG_DBG("Looking for alias for {}", full_name);

View File

@@ -40,6 +40,9 @@ public:
common::model::diagram_t type() const override;
type_safe::optional_ref<const clanguml::common::model::element> get(
const std::string &full_name) const;
std::string to_alias(const std::string &full_name) const;
};
}

View File

@@ -28,6 +28,12 @@ common::model::diagram_t diagram::type() const
return common::model::diagram_t::kSequence;
}
type_safe::optional_ref<const clanguml::common::model::element> diagram::get(
const std::string &full_name) const
{
return {};
}
std::string diagram::to_alias(const std::string &full_name) const
{
return full_name;

View File

@@ -36,6 +36,9 @@ public:
common::model::diagram_t type() const override;
type_safe::optional_ref<const clanguml::common::model::element> get(
const std::string &full_name) const;
std::string to_alias(const std::string &full_name) const;
bool started{false};