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

@@ -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;
}