Skip to content

Commit

Permalink
Add yaml templates
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed Aug 1, 2023
1 parent 247e636 commit e358126
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions src/App/Tweaks/Declarative/Yaml/YamlReader.Template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace
{
constexpr auto TemplateAttrKey = "$data";
constexpr auto InstanceAttrKey = "$instances";

constexpr auto AttrMark = '$';
constexpr auto AttrOpen = '{';
Expand Down Expand Up @@ -49,7 +49,10 @@ std::string FormatString(const std::string& aInput, const Core::Map<uint64_t, st

if (*(attrOpen + 1) != AttrOpen)
{
return aInput;
*out = *str;
++out;
++str;
continue;
}

auto* attrClose = strchr(str, AttrClose);
Expand Down Expand Up @@ -143,7 +146,7 @@ YAML::Node FormatNode(const YAML::Node& aNode, const Core::Map<uint64_t, std::st
}
}

void App::YamlReader::ExpandTemplates(YAML::Node& aRootNode)
void App::YamlReader::ProcessTemplates(YAML::Node& aRootNode)
{
Core::Vector<std::pair<const std::string&, YAML::Node>> templates;

Expand All @@ -152,7 +155,7 @@ void App::YamlReader::ExpandTemplates(YAML::Node& aRootNode)
const auto& subKey = nodeIt.first.Scalar();
const auto& subNode = nodeIt.second;

if (!subNode.IsMap() || !subNode[TemplateAttrKey].IsDefined())
if (!subNode.IsMap() || !subNode[InstanceAttrKey].IsDefined())
continue;

templates.emplace_back(subKey, subNode);
Expand All @@ -162,15 +165,15 @@ void App::YamlReader::ExpandTemplates(YAML::Node& aRootNode)
{
aRootNode.remove(templateName);

const auto& dataNode = templateNode[TemplateAttrKey];
const auto& dataNode = templateNode[InstanceAttrKey];

if (!dataNode.IsSequence())
{
LogError("{}: Template data must be an array of structs.", templateName);
LogError("{}: Template instances must be an array of structs.", templateName);
continue;
}

templateNode.remove(TemplateAttrKey);
templateNode.remove(InstanceAttrKey);

for (std::size_t i = 0; i < dataNode.size(); ++i)
{
Expand All @@ -191,7 +194,7 @@ void App::YamlReader::ExpandTemplates(YAML::Node& aRootNode)

if (aRootNode[instanceName].IsDefined())
{
LogError("{}: Cannot instantiate {}, because it already exists.",
LogError("{}: Cannot create instance {}, because it already exists.",
templateName, instanceName);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/Tweaks/Declarative/Yaml/YamlReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void App::YamlReader::Read(App::TweakChangeset& aChangeset)
}

ConvertLegacyNodes();
ExpandTemplates(m_data);
ProcessTemplates(m_data);

auto propMode = ResolvePropertyMode(m_data);

Expand Down
2 changes: 1 addition & 1 deletion src/App/Tweaks/Declarative/Yaml/YamlReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class YamlReader
Red::InstancePtr<> MakeValue(const Red::CBaseRTTIType* aType, const YAML::Node& aNode);
std::pair<Red::CName, Red::InstancePtr<>> TryMakeValue(const YAML::Node& aNode);

void ExpandTemplates(YAML::Node& aRootNode);
void ProcessTemplates(YAML::Node& aRootNode);
void ConvertLegacyNodes();

std::filesystem::path m_path;
Expand Down

0 comments on commit e358126

Please sign in to comment.