Refactor Form and FormElement

dependabot/npm_and_yarn/Src/WebController/UI/websocket-extensions-0.1.4
Grzegorz Rychlik 2019-11-13 15:39:29 +01:00
parent e35304a65d
commit 0c732309da
12 changed files with 175 additions and 220 deletions

View File

@ -113,10 +113,10 @@ Global
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x64.Build.0 = Release|x64 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x64.Build.0 = Release|x64
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x86.ActiveCfg = Release|Win32 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x86.ActiveCfg = Release|Win32
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x86.Build.0 = Release|Win32 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.Release|x86.Build.0 = Release|Win32
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x64.ActiveCfg = Release|x64 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x64.ActiveCfg = ReleaseWithDebInfo|x64
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x64.Build.0 = Release|x64 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x64.Build.0 = ReleaseWithDebInfo|x64
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x86.ActiveCfg = Release|Win32 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x86.ActiveCfg = ReleaseWithDebInfo|Win32
{F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x86.Build.0 = Release|Win32 {F2EC73D1-D533-4EE4-955A-A62E306472CC}.ReleaseWithDebInfo|x86.Build.0 = ReleaseWithDebInfo|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -39,8 +39,11 @@ namespace MWR::C3::Linter
ChannelLinter::ChannelLinter(AppConfig config) : ChannelLinter::ChannelLinter(AppConfig config) :
m_Config(std::move(config)), m_Config(std::move(config)),
m_ChannelData(GetChannelInfo(m_Config.m_ChannelName)), m_ChannelData(GetChannelInfo(m_Config.m_ChannelName)),
m_ChannelCapability(GetChannelCapability(m_ChannelData)) m_ChannelCapability(GetChannelCapability(m_ChannelData)),
m_CreateForm(m_ChannelCapability.at("/create/arguments"_json_pointer))
{ {
for (auto&& command : m_ChannelCapability.at("commands"))
m_CommandForms.emplace_back(command.at("arguments"));
} }
void ChannelLinter::Process() void ChannelLinter::Process()
@ -68,7 +71,9 @@ namespace MWR::C3::Linter
{ {
std::cout << "Create channel " << std::endl; std::cout << "Create channel " << std::endl;
Form form(m_ChannelCapability.at("/create/arguments"_json_pointer)); Form form(m_ChannelCapability.at("/create/arguments"_json_pointer));
auto createParams = form.FillForm(channnelArguments); Form form2(m_ChannelCapability.at("/create/arguments"_json_pointer));
form = form2;
auto createParams = form.Fill(channnelArguments);
auto blob = MWR::C3::Core::Profiler::TranslateArguments(createParams); auto blob = MWR::C3::Core::Profiler::TranslateArguments(createParams);
return MakeChannel(blob); return MakeChannel(blob);
} }
@ -110,7 +115,7 @@ namespace MWR::C3::Linter
json command = *commandIt; json command = *commandIt;
Form commandForm(command.at("arguments")); Form commandForm(command.at("arguments"));
command["arguments"] = commandForm.FillForm({begin(commandParams) + 1, end(commandParams)}); // + 1 to omit command id command["arguments"] = commandForm.Fill({begin(commandParams) + 1, end(commandParams)}); // + 1 to omit command id
return C3::Core::Profiler::TranslateCommand(command); return C3::Core::Profiler::TranslateCommand(command);
} }

View File

@ -25,6 +25,8 @@ namespace MWR::C3::Linter
AppConfig m_Config; AppConfig m_Config;
InterfaceFactory::InterfaceData<AbstractChannel> const& m_ChannelData; InterfaceFactory::InterfaceData<AbstractChannel> const& m_ChannelData;
json m_ChannelCapability; json m_ChannelCapability;
const Form m_CreateForm;
std::vector<Form> m_CommandForms;
}; };
} }

View File

@ -299,9 +299,8 @@
<ClInclude Include="AppConfig.hpp" /> <ClInclude Include="AppConfig.hpp" />
<ClInclude Include="ChannelLinter.h" /> <ClInclude Include="ChannelLinter.h" />
<ClInclude Include="Form.h" /> <ClInclude Include="Form.h" />
<ClInclude Include="FormElement.hpp" /> <ClInclude Include="FormElement.h" />
<ClInclude Include="ArgumentParser.h" /> <ClInclude Include="ArgumentParser.h" />
<ClInclude Include="InputVector.h" />
<ClInclude Include="MockDeviceBridge.h" /> <ClInclude Include="MockDeviceBridge.h" />
<ClInclude Include="StdAfx.h" /> <ClInclude Include="StdAfx.h" />
</ItemGroup> </ItemGroup>
@ -310,7 +309,7 @@
<ClCompile Include="ChannelLinterMain.cpp" /> <ClCompile Include="ChannelLinterMain.cpp" />
<ClCompile Include="Form.cpp" /> <ClCompile Include="Form.cpp" />
<ClCompile Include="ArgumentParser.cpp" /> <ClCompile Include="ArgumentParser.cpp" />
<ClCompile Include="InputVector.cpp" /> <ClCompile Include="FormElement.cpp" />
<ClCompile Include="MockDeviceBridge.cpp" /> <ClCompile Include="MockDeviceBridge.cpp" />
<ClCompile Include="StdAfx.cpp"> <ClCompile Include="StdAfx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

View File

@ -5,21 +5,20 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Form.cpp" /> <ClCompile Include="Form.cpp" />
<ClCompile Include="InputVector.cpp" />
<ClCompile Include="StdAfx.cpp" /> <ClCompile Include="StdAfx.cpp" />
<ClCompile Include="MockDeviceBridge.cpp" /> <ClCompile Include="MockDeviceBridge.cpp" />
<ClCompile Include="ChannelLinterMain.cpp" /> <ClCompile Include="ChannelLinterMain.cpp" />
<ClCompile Include="ChannelLinter.cpp" /> <ClCompile Include="ChannelLinter.cpp" />
<ClCompile Include="ArgumentParser.cpp" /> <ClCompile Include="ArgumentParser.cpp" />
<ClCompile Include="FormElement.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Form.h" /> <ClInclude Include="Form.h" />
<ClInclude Include="FormElement.hpp" />
<ClInclude Include="InputVector.h" />
<ClInclude Include="StdAfx.h" /> <ClInclude Include="StdAfx.h" />
<ClInclude Include="MockDeviceBridge.h" /> <ClInclude Include="MockDeviceBridge.h" />
<ClInclude Include="ChannelLinter.h" /> <ClInclude Include="ChannelLinter.h" />
<ClInclude Include="ArgumentParser.h" /> <ClInclude Include="ArgumentParser.h" />
<ClInclude Include="AppConfig.hpp" /> <ClInclude Include="AppConfig.hpp" />
<ClInclude Include="FormElement.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -3,52 +3,49 @@
namespace MWR::C3::Linter namespace MWR::C3::Linter
{ {
Form::Form(json argumentForm) :
m_ArgumentsForm(std::move(argumentForm))
{
for (auto& arg : m_ArgumentsForm)
{
if (arg.is_array())
{
for (auto& a : arg)
{
m_Elements.emplace_back(MakeFormElement(a));
}
}
else
{
m_Elements.emplace_back(MakeFormElement(arg));
}
}
}
Form::Form(json argumentForm) : m_ArgumentsForm(std::move(argumentForm)) Form::Form(Form const& other) noexcept :
Form(other.m_ArgumentsForm)
{ {
} }
json Form::FillForm(InputVector input) Form& Form::operator=(Form const& other) noexcept
{ {
try Form tmp(other);
{ std::swap(*this, tmp);
auto createParams = m_ArgumentsForm; return *this;
auto fillArg = [&input](json& arg)
{
ValidateAndSet(arg, input.GetNext());
};
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;
} json Form::Fill(StringVector input)
catch (std::out_of_range&)
{ {
input.Reset(); if (input.size() < m_Elements.size())
std::string message; throw std::runtime_error("Not enough arguments given to fill out form [required = " + std::to_string(m_Elements.size()) + ", given = " + std::to_string(input.size()) + "]");
auto AddParameterMessage = [&message, &input](json& arg)
{ // TODO log "Too many arguments, ignoring some of them"
auto& name = arg.at("name").get_ref<std::string const&>(); // if (input.size() > m_Elemets.size())
auto& type = arg.at("type").get_ref<std::string const&>();
message += name + " (" + type + ") > " + input.GetOptionalNext().value_or("") + '\n'; auto inputIt = begin(input);
}; std::for_each(begin(m_Elements), end(m_Elements), [&inputIt](auto& element) {element->ValidateAndSet(*inputIt++); });
for (size_t i = 0; i < m_ArgumentsForm.size(); ++i)
{ return m_ArgumentsForm;
auto& arg = m_ArgumentsForm[i];
if (arg.is_array())
for (size_t j = 0; j < arg.size(); ++j)
AddParameterMessage(arg[j]);
else
AddParameterMessage(arg);
}
throw std::runtime_error("Failed to fill from arguments: not enough arguments given.\nRequired parameter > received argument\n" + message);
}
} }
StringVector Form::GetComplementaryArgs(StringVector input) StringVector Form::GetComplementaryArgs(StringVector input)

View File

@ -7,13 +7,18 @@ namespace MWR::C3::Linter
public: public:
Form(json argumentForm); Form(json argumentForm);
json FillForm(InputVector input); Form(Form const& other) noexcept;
Form& operator=(Form const& other) noexcept;
Form(Form&& other) noexcept = default;
Form& operator=(Form&& other) noexcept = default;
json Fill(StringVector input);
StringVector GetComplementaryArgs(StringVector input); StringVector GetComplementaryArgs(StringVector input);
private: private:
// TODO Form should contain a list of form elements (validated), not just a json
json m_ArgumentsForm; json m_ArgumentsForm;
std::vector<std::unique_ptr<FormElement>> m_Elements;
}; };
} }

View File

@ -1,40 +1,10 @@
#pragma once #include "StdAfx.h"
#include "FormElement.h"
namespace MWR::C3::Linter namespace MWR::C3::Linter
{ {
namespace namespace
{ {
class FormElement
{
public:
enum class Type
{
Unknown = 0,
Uint8,
Uint16,
Uint32,
Uint64,
Int8,
Int16,
Int32,
Int64,
Float,
Boolean,
String,
Ip,
Binary,
};
virtual void ValidateAndSet(std::string_view input) = 0;
protected:
FormElement(json& definition) : m_Definition(definition)
{
}
json& m_Definition;
};
inline void CheckLengthConstraint(json const& definition, std::string_view input) inline void CheckLengthConstraint(json const& definition, std::string_view input)
{ {
if (definition.contains("min") && input.length() < definition["min"].get<size_t>()) if (definition.contains("min") && input.length() < definition["min"].get<size_t>())
@ -146,10 +116,44 @@ namespace MWR::C3::Linter
m_Definition["value"] = value; m_Definition["value"] = value;
} }
}; };
}
inline std::unique_ptr<FormElement> MakeFormElement(json& element) FormElement::FormElement(json& definition) :
m_Definition(definition)
{ {
switch (element.at("type").get<FormElement::Type>()) if (!m_Definition.contains("name"))
throw std::invalid_argument{ "Form element must contain 'name' property." };
}
NLOHMANN_JSON_SERIALIZE_ENUM
(
FormElement::Type,
{
{FormElement::Type::Unknown, nullptr},
{FormElement::Type::Uint8, "uint8"},
{FormElement::Type::Uint16, "uint16"},
{FormElement::Type::Uint32, "uint32"},
{FormElement::Type::Uint64, "uint64"},
{FormElement::Type::Int8, "int8"},
{FormElement::Type::Int16, "int16"},
{FormElement::Type::Int32, "int32"},
{FormElement::Type::Int64, "int64"},
{FormElement::Type::Float, "float"},
{FormElement::Type::Boolean, "boolean"},
{FormElement::Type::String, "string"},
{FormElement::Type::Ip, "ip"},
{FormElement::Type::Binary, "binary"},
}
);
std::unique_ptr<FormElement> MakeFormElement(json& element)
{
if (!element.is_object())
throw std::invalid_argument { "Form element must be a json object." };
if (!element.contains("type"))
throw std::invalid_argument{ "Form element must contain 'type' property." };
switch (element["type"].get<FormElement::Type>())
{ {
case FormElement::Type::Uint8: case FormElement::Type::Uint8:
return std::make_unique<NumericFormElement<uint8_t>>(element); return std::make_unique<NumericFormElement<uint8_t>>(element);
@ -182,32 +186,10 @@ namespace MWR::C3::Linter
} }
} }
NLOHMANN_JSON_SERIALIZE_ENUM void ValidateAndSet(json& element, std::string_view input)
(
FormElement::Type,
{
{FormElement::Type::Unknown, nullptr},
{FormElement::Type::Uint8, "uint8"},
{FormElement::Type::Uint16, "uint16"},
{FormElement::Type::Uint32, "uint32"},
{FormElement::Type::Uint64, "uint64"},
{FormElement::Type::Int8, "int8"},
{FormElement::Type::Int16, "int16"},
{FormElement::Type::Int32, "int32"},
{FormElement::Type::Int64, "int64"},
{FormElement::Type::Float, "float"},
{FormElement::Type::Boolean, "boolean"},
{FormElement::Type::String, "string"},
{FormElement::Type::Ip, "ip"},
{FormElement::Type::Binary, "binary"},
}
);
}
inline void ValidateAndSet(json& element, std::string_view input)
{ {
auto formElement = MakeFormElement(element); auto formElement = MakeFormElement(element);
formElement->ValidateAndSet(input); formElement->ValidateAndSet(input);
} }
}
}

View File

@ -0,0 +1,37 @@
#pragma once
namespace MWR::C3::Linter
{
class FormElement
{
public:
enum class Type
{
Unknown = 0,
Uint8,
Uint16,
Uint32,
Uint64,
Int8,
Int16,
Int32,
Int64,
Float,
Boolean,
String,
Ip,
Binary,
};
virtual void ValidateAndSet(std::string_view input) = 0;
protected:
FormElement(json& definition);
json& m_Definition;
};
std::unique_ptr<FormElement> MakeFormElement(json& element);
void ValidateAndSet(json& element, std::string_view input);
}

View File

@ -1,41 +0,0 @@
#include "stdafx.h"
namespace MWR::C3::Linter
{
InputVector::InputVector(StringVector input) : m_Input{ std::move(input) }, m_Current{ m_Input.cbegin() }
{
m_Current = m_Input.cbegin();
}
InputVector::InputVector(InputVector const& other) :
m_Input{ other.m_Input },
m_Current{ m_Input.cbegin() + std::distance(other.m_Input.begin(), other.m_Current) }
{
}
InputVector& InputVector::operator=(InputVector const& other)
{
InputVector tmp(other);
std::swap(tmp, *this);
return *this;
}
std::string InputVector::GetNext()
{
if (m_Current == m_Input.cend())
throw std::out_of_range("Input vector has no more elements");
return *m_Current++;
}
void InputVector::Reset()
{
m_Current = m_Input.cbegin();
}
std::optional<std::string> InputVector::GetOptionalNext()
{
if (m_Current == m_Input.cend())
return {};
return *m_Current++;
}
}

View File

@ -1,30 +0,0 @@
#pragma once
namespace MWR::C3::Linter
{
class InputVector
{
public:
InputVector(StringVector input);
template<typename It>
InputVector(It first, It last) :m_Input{ first, last }, m_Current{ begin(m_Input) }
{
}
InputVector(InputVector const& other);
InputVector& operator = (InputVector const& other);
std::string GetNext();
void Reset();
size_t Size() { return m_Input.size(); }
std::string& operator[](size_t index) { return m_Input.operator [](index); }
std::string const& operator[](size_t index) const { return m_Input.operator [](index); }
std::string at(size_t index) { return m_Input.at(index); }
std::string const& at(size_t index) const { return m_Input.at(index); }
std::optional<std::string> GetOptionalNext();
private:
StringVector m_Input;
StringVector::const_iterator m_Current;
};
}

View File

@ -8,6 +8,7 @@
#include <optional> #include <optional>
#include <string_view> #include <string_view>
#include <charconv> #include <charconv>
#include <utility>
// C3 inclusion. // C3 inclusion.
#include "Common/MWR/C3/Sdk.hpp" #include "Common/MWR/C3/Sdk.hpp"
@ -20,8 +21,7 @@ using json = nlohmann::json;
#include "argparse.hpp" #include "argparse.hpp"
#include "AppConfig.hpp" #include "AppConfig.hpp"
#include "ArgumentParser.h" #include "ArgumentParser.h"
#include "InputVector.h" #include "FormElement.h"
#include "FormElement.hpp"
#include "Form.h" #include "Form.h"
#include "MockDeviceBridge.h" #include "MockDeviceBridge.h"
#include "ChannelLinter.h" #include "ChannelLinter.h"