2019-10-29 13:15:13 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Form.h"
|
|
|
|
|
|
|
|
namespace MWR::C3::Linter
|
|
|
|
{
|
|
|
|
|
|
|
|
Form::Form(json argumentForm) : m_ArgumentsForm(std::move(argumentForm))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
json Form::FillForm(InputVector input)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto createParams = m_ArgumentsForm;
|
|
|
|
auto fillArg = [&input](json& arg)
|
|
|
|
{
|
2019-10-31 12:11:49 +00:00
|
|
|
ValidateAndSet(arg, input.GetNext());
|
2019-10-29 13:15:13 +00:00
|
|
|
};
|
|
|
|
for (size_t i = 0; i < createParams.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& arg = createParams[i];
|
|
|
|
if (arg.is_array())
|
|
|
|
for (size_t j = 0; j < arg.size(); ++j)
|
|
|
|
fillArg(arg[j]);
|
|
|
|
else
|
|
|
|
fillArg(arg);
|
|
|
|
}
|
|
|
|
return createParams;
|
|
|
|
}
|
|
|
|
catch (std::out_of_range&)
|
|
|
|
{
|
2019-10-31 12:11:49 +00:00
|
|
|
input.Reset();
|
|
|
|
std::string message;
|
|
|
|
auto AddParameterMessage = [&message, &input](json& arg)
|
|
|
|
{
|
|
|
|
auto& name = arg.at("name").get_ref<std::string const&>();
|
|
|
|
auto& type = arg.at("type").get_ref<std::string const&>();
|
|
|
|
message += name + " (" + type + ") > " + input.GetOptionalNext().value_or("") + '\n';
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < m_ArgumentsForm.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& arg = m_ArgumentsForm[i];
|
|
|
|
if (arg.is_array())
|
|
|
|
for (size_t j = 0; j < arg.size(); ++j)
|
|
|
|
AddParameterMessage(arg[j]);
|
|
|
|
else
|
|
|
|
AddParameterMessage(arg);
|
|
|
|
}
|
2019-11-05 16:18:25 +00:00
|
|
|
throw std::runtime_error("Failed to fill from arguments: not enough arguments given.\nRequired parameter > received argument\n" + message);
|
2019-10-29 13:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 13:18:56 +00:00
|
|
|
|
2019-11-07 16:21:44 +00:00
|
|
|
StringVector Form::GetComplementaryArgs(StringVector input)
|
2019-10-29 13:18:56 +00:00
|
|
|
{
|
2019-11-07 16:21:44 +00:00
|
|
|
size_t currentOffset = 0;
|
|
|
|
for (auto const& arg : m_ArgumentsForm)
|
2019-10-29 13:18:56 +00:00
|
|
|
{
|
|
|
|
if (arg.is_array())
|
|
|
|
{
|
2019-11-07 16:21:44 +00:00
|
|
|
auto size = arg.size();
|
|
|
|
auto first = begin(input) + currentOffset;
|
|
|
|
std::rotate(first, first + size - 1, first + size);
|
|
|
|
currentOffset += size;
|
2019-10-29 13:18:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-07 16:21:44 +00:00
|
|
|
++currentOffset;
|
2019-10-29 13:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-07 16:21:44 +00:00
|
|
|
return input;
|
2019-10-29 13:18:56 +00:00
|
|
|
}
|
2019-10-29 13:15:13 +00:00
|
|
|
}
|