Skip to content

Commit

Permalink
Refactor server parameters, add boolean parameter (Checkbox)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Oct 16, 2024
1 parent 2f72c27 commit 1285f08
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 40 deletions.
4 changes: 4 additions & 0 deletions Source/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ set(SRC_LIST Network/NetworkClient.cpp
Upload/Parameters/AbstractParameter.cpp
Upload/Parameters/TextParameter.cpp
Upload/Parameters/ChoiceParameter.cpp
Upload/Parameters/BooleanParameter.cpp
Upload/Parameters/ParameterFactory.cpp
ServiceLocator.cpp
Settings/BasicSettings.cpp
Logging/MyLogSink.cpp
Expand Down Expand Up @@ -117,6 +119,8 @@ set(HEADER_LIST Network/NetworkClient.h
Upload/Parameters/AbstractParameter.h
Upload/Parameters/TextParameter.h
Upload/Parameters/ChoiceParameter.h
Upload/Parameters/BooleanParameter.h
Upload/Parameters/ParameterFactory.h
Utils/CoreUtils.h
Utils/CryptoUtils.h
Utils/SimpleXml.h
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Upload/Parameters/AbstractParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AbstractParameter {
virtual std::string getType() const = 0;

virtual void setValue(const std::string& val) = 0;
virtual std::string getValue() const = 0;
virtual std::string getValueAsString() const = 0;

private:
std::string name_, title_;
Expand Down
33 changes: 33 additions & 0 deletions Source/Core/Upload/Parameters/BooleanParameter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "BooleanParameter.h"

#include "Core/Utils/StringUtils.h"

BooleanParameter::BooleanParameter(std::string name)
: AbstractParameter(std::move(name))
{
}

std::string BooleanParameter::getType() const {
return TYPE;
}

std::string BooleanParameter::getValueAsString() const {
return value_ ? "1" : "0";
}

void BooleanParameter::setValue(const std::string& val) {
std::string lower = IuStringUtils::toLower(val);
if (val == "1" || lower == "true") {
value_ = true;
} else {
value_ = false;
}
}

void BooleanParameter::setValue(bool val) {
value_ = val;
}

bool BooleanParameter::getValue() const {
return value_;
}
19 changes: 19 additions & 0 deletions Source/Core/Upload/Parameters/BooleanParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "AbstractParameter.h"

class BooleanParameter: public AbstractParameter {
public:
explicit BooleanParameter(std::string name);

std::string getType() const override;
std::string getValueAsString() const override;
void setValue(const std::string& val) override;

inline static const std::string TYPE = "boolean";

void setValue(bool val);
bool getValue() const;
private:
bool value_ = false;
};
29 changes: 26 additions & 3 deletions Source/Core/Upload/Parameters/ChoiceParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ std::string ChoiceParameter::getType() const {
return TYPE;
}

std::string ChoiceParameter::getValue() const {
return value_;
std::string ChoiceParameter::getValueAsString() const {
if (selectedIndex_ == -1) {
return {};
}
return items_.at(selectedIndex_).first;
}

void ChoiceParameter::setValue(const std::string& val) {
value_ = val;
size_t i = 0;

for (const auto& it : items_) {
if (it.first == val) {
selectedIndex_ = i;
return;
}
++i;
}
selectedIndex_ = -1;
}

void ChoiceParameter::addItem(const std::string& id, const std::string& value) {
Expand All @@ -23,8 +35,19 @@ void ChoiceParameter::addItem(const std::string& id, const std::string& value) {

void ChoiceParameter::clearItems() {
items_.clear();
selectedIndex_ = -1;
}

const std::vector<std::pair<std::string, std::string>>& ChoiceParameter::getItems() const{
return items_;
}

void ChoiceParameter::setSelectedIndex(int val) {
if (val >= -1 && val < items_.size()) {
selectedIndex_ = val;
}
}

int ChoiceParameter::selectedIndex() const {
return selectedIndex_;
}
7 changes: 5 additions & 2 deletions Source/Core/Upload/Parameters/ChoiceParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ class ChoiceParameter: public AbstractParameter {
explicit ChoiceParameter(std::string name);

std::string getType() const override;
std::string getValue() const override;
std::string getValueAsString() const override;
void setValue(const std::string& val) override;
void addItem(const std::string& id, const std::string& value);
void clearItems();
const std::vector<std::pair<std::string, std::string>>& getItems() const;

inline static const std::string TYPE = "choice";
void setSelectedIndex(int val);
int selectedIndex() const;

private:
std::string value_;
std::vector<std::pair<std::string, std::string>> items_;
int selectedIndex_ = -1;
};
78 changes: 78 additions & 0 deletions Source/Core/Upload/Parameters/ParameterFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "ParameterFactory.h"


#include <unordered_map>
#include <functional>

#include "Core/Scripting/API/ScriptAPI.h"
#include "TextParameter.h"
#include "ChoiceParameter.h"
#include "BooleanParameter.h"

class ParameterFactory {
public:
virtual std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const = 0;
};

class ChoiceParameterFactory : public ParameterFactory {
public:
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override {
auto choiceParameter = std::make_unique<ChoiceParameter>(name);
auto choices = table.GetValue<Sqrat::Array>("items");
if (!!choices) {
Sqrat::Array::iterator it;
while (choices->Next(it)) {
Sqrat::Table tbl(it.getValue(), choices->GetVM());
std::string itemLabel = ScriptAPI::GetValue(tbl.GetValue<std::string>("label"));
std::string itemId = ScriptAPI::GetValue(tbl.GetValue<std::string>("id"));
choiceParameter->addItem(itemId, itemLabel);
}
}
return choiceParameter;
}
};

class BooleanParameterFactory : public ParameterFactory {
public:
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override {
return std::make_unique<BooleanParameter>(name);
}
};

class TextParameterFactory : public ParameterFactory {
public:
std::unique_ptr<AbstractParameter> create(const std::string& name, Sqrat::Table& table) const override {
return std::make_unique<TextParameter>(name);
}
};

class ParameterFactoryRegistry {
public:
ParameterFactoryRegistry() {
// Registering factories by parameter type
registry_[ChoiceParameter::TYPE] = std::make_unique<ChoiceParameterFactory>();
registry_[BooleanParameter::TYPE] = std::make_unique<BooleanParameterFactory>();
registry_[TextParameter::TYPE] = std::make_unique<TextParameterFactory>(); // TextParameter by default
}

std::unique_ptr<AbstractParameter> createParameter(const std::string& type, const std::string& name, Sqrat::Table& table) const {
auto it = registry_.find(type);
if (it != registry_.end()) {
return it->second->create(name, table);
}
// Fallback to default factory
it = registry_.find(TextParameter::TYPE);
if (it != registry_.end()) {
return it->second->create(name, table);
}
return {};
}

private:
std::unordered_map<std::string, std::unique_ptr<ParameterFactory>> registry_;
};

std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string& name, const std::string& type, Sqrat::Table& table) {
static ParameterFactoryRegistry factoryRegistry;
return factoryRegistry.createParameter(type, name, table);
}
10 changes: 10 additions & 0 deletions Source/Core/Upload/Parameters/ParameterFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#pragma once

#include <memory>
#include <string>

#include "AbstractParameter.h"
#include "Core/Scripting/Squirrelnc.h"

std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string& name, const std::string& type, Sqrat::Table& table);
4 changes: 2 additions & 2 deletions Source/Core/Upload/Parameters/TextParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ TextParameter::TextParameter(std::string title)
}

std::string TextParameter::getType() const {
return "text";
return TYPE;
}

std::string TextParameter::getValue() const {
std::string TextParameter::getValueAsString() const {
return value_;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Upload/Parameters/TextParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TextParameter: public AbstractParameter {
explicit TextParameter(std::string name);

std::string getType() const override;
std::string getValue() const override;
std::string getValueAsString() const override;
void setValue(const std::string& val) override;

inline static const std::string TYPE = "text";
Expand Down
21 changes: 13 additions & 8 deletions Source/Core/Upload/ScriptUploadEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "AuthTask.h"
#include "FolderTask.h"
#include "Parameters/TextParameter.h"
#include "Parameters/ChoiceParameter.h"
#include "Parameters/ParameterFactory.h"

namespace {

Expand All @@ -56,10 +56,9 @@ std::pair<int, Sqrat::Table> GetOperationResult(Sqrat::SharedPtr<Sqrat::Object>
return {res, t};
}

std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string name, const std::string& type, Sqrat::Table& table) {
if (type == "text") {
return std::make_unique<TextParameter>(name);
} if (type == "choice") {
/* std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string name, const std::string& type, Sqrat::Table& table)
{
if (type == ChoiceParameter::TYPE) {
auto choiceParameter = std::make_unique<ChoiceParameter>(name);
auto choices = table.GetValue<Sqrat::Array>("items");
if (!!choices) {
Expand All @@ -73,20 +72,26 @@ std::unique_ptr<AbstractParameter> SqTableToParameter(const std::string name, co
}
return choiceParameter;
}
return {};
}
if (type == BooleanParameter::TYPE) {
return std::make_unique<BooleanParameter>(name);
} else {
return std::make_unique<TextParameter>(name);
}
}*/

void SqTableToParameterList(Sqrat::SharedPtr<Sqrat::Table> tbl, ParameterList& list) {
list.clear();
Sqrat::Array::iterator it;
Sqrat::Object::iterator it;
while (tbl->Next(it)) {
Sqrat::Object obj(it.getValue(), tbl->GetVM());
if (obj.GetType() == OT_STRING) {
// The old way to declare a parameter
auto newParam = std::make_unique<TextParameter>(it.getName());
std::string value = obj.Cast<std::string>();
newParam->setTitle(value);
list.push_back(std::move(newParam));
} else if (obj.GetType() == OT_TABLE) {
// The new way to declare a parameter (in a nested table)
Sqrat::Table table(it.getValue(), tbl->GetVM());
std::string title = ScriptAPI::GetValue(table.GetValue<std::string>("title"));
std::string typeStr = ScriptAPI::GetValue(table.GetValue<std::string>("type"));
Expand Down
Loading

0 comments on commit 1285f08

Please sign in to comment.