Skip to content

Commit

Permalink
tom make:model enhanced empty input values
Browse files Browse the repository at this point in the history
Enhanced processing of empty input values.
  • Loading branch information
silverqx committed Jul 19, 2023
1 parent 9302944 commit f6a79c4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
4 changes: 1 addition & 3 deletions tom/include/tom/commands/make/stubs/modelstubs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ R"(
inline const auto *const ModelMutatorsStub =
R"(
/*! Map of mutator names to methods. */
inline static const QHash<QString, MutatorFunction> u_mutators {
{{ mutatorItems }}
};)";
inline static const QHash<QString, MutatorFunction> u_mutators {{{ mutatorItems }}};)";

/*! Mutator mapping item for model's u_mutators hash stub. */
inline const auto *const ModelMutatorItemStub =
Expand Down
2 changes: 2 additions & 0 deletions tom/include/tom/commands/make/support/modelcreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ namespace Tom::Commands::Make::Support
static QString
createMutatorItem(const QString &className, const QString &mutator,
QString::size_type mutatorsMaxSize);
/*! Wrap final mutator items list for parent stub. */
static QString wrapMutatorItemsList(const QStringList &mutatorItems);

/* Global */
/*! Create model's TinyORM includes section. */
Expand Down
65 changes: 62 additions & 3 deletions tom/src/tom/commands/make/support/modelcreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <range/v3/action/push_back.hpp>
#include <range/v3/action/remove_if.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/move.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/zip.hpp>

#include <orm/constants.hpp>
#include <orm/macros/likely.hpp>
#include <orm/utils/container.hpp>
#include <orm/utils/string.hpp>

Expand All @@ -26,6 +28,7 @@ using Orm::Constants::COMMA;
using Orm::Constants::DOT;
using Orm::Constants::NEWLINE;
using Orm::Constants::NOSPACE;
using Orm::Constants::NOSPACE3;
using Orm::Constants::QUOTE;
using Orm::Constants::SPACE;

Expand Down Expand Up @@ -236,6 +239,10 @@ ModelCreator::createOneToOneRelation(
for (const auto &[relatedClass, foreignKey, relationOrder] :
ranges::views::zip(relatedClasses, foreignKeys, orderList)
) {
// Nothing to do, don't create relationship methods with empty names
if (relatedClass.isEmpty())
continue;

// Insert to model includes, usings, and relations lists
m_includesList.emplace(QString(ModelIncludeItemStub).arg(relatedClass.toLower()));
m_forwardsList.emplace(QString(ModelForwardItemStub).arg(relatedClass));
Expand Down Expand Up @@ -290,6 +297,10 @@ ModelCreator::createOneToManyRelation(
for (const auto &[relatedClass, foreignKey, relationOrder] :
ranges::views::zip(relatedClasses, foreignKeys, orderList)
) {
// Nothing to do, don't create relationship methods with empty names
if (relatedClass.isEmpty())
continue;

// Insert to model includes, usings, and relations lists
m_includesList.emplace(QString(ModelIncludeItemStub).arg(relatedClass.toLower()));
m_forwardsList.emplace(QString(ModelForwardItemStub).arg(relatedClass));
Expand Down Expand Up @@ -344,6 +355,10 @@ ModelCreator::createBelongsToRelation(
for (const auto &[relatedClass, foreignKey, relationOrder] :
ranges::views::zip(relatedClasses, foreignKeys, orderList)
) {
// Nothing to do, don't create relationship methods with empty names
if (relatedClass.isEmpty())
continue;

// Insert to model includes, usings, and relations lists
m_includesList.emplace(QString(ModelIncludeItemStub).arg(relatedClass.toLower()));
m_forwardsList.emplace(QString(ModelForwardItemStub).arg(relatedClass));
Expand Down Expand Up @@ -435,6 +450,10 @@ ModelCreator::createBelongsToManyRelation(
pivotClasses, pivotInverseClasses, asList, withPivotList,
withTimestampsList)
) {
// Nothing to do, don't create relationship methods with empty names
if (relatedClass.isEmpty())
continue;

// Insert to model includes, usings, and relations lists
m_includesList.emplace(QString(ModelIncludeItemStub).arg(relatedClass.toLower()));
m_forwardsList.emplace(QString(ModelForwardItemStub).arg(relatedClass));
Expand Down Expand Up @@ -802,6 +821,11 @@ QString ModelCreator::prepareInitializerListValues(const QStringList &list)
const auto wrapValues = [](const QStringList &values, const QString &prefix)
{
return values
// Skip empty values (allows to create initializers like xyz {}).
| ranges::views::filter([](const QString &value)
{
return !value.isEmpty();
})
| ranges::views::transform([&prefix](const QString &value)
{
return QStringLiteral("%2\"%1\"").arg(value, prefix);
Expand Down Expand Up @@ -935,6 +959,10 @@ ModelCreator::createOneToOneRelationItem(
for (const auto &[relatedClass, relationOrder] :
ranges::views::zip(relatedClasses, orderList)
) {
// Nothing to do, don't create relation mappings with empty names
if (relatedClass.isEmpty())
continue;

const auto relationName = guessOneTypeRelationName(relatedClass);
const auto spaceAlign = QString(relationsMaxSize - relationName.size(), SPACE);

Expand Down Expand Up @@ -973,6 +1001,10 @@ ModelCreator::createOneToManyRelationItem(
for (const auto &[relatedClass, relationOrder] :
ranges::views::zip(relatedClasses, orderList)
) {
// Nothing to do, don't create relation mappings with empty names
if (relatedClass.isEmpty())
continue;

const auto relationName = guessManyTypeRelationName(relatedClass);
const auto spaceAlign = QString(relationsMaxSize - relationName.size(), SPACE);

Expand Down Expand Up @@ -1011,6 +1043,10 @@ ModelCreator::createBelongsToRelationItem(
for (const auto &[relatedClass, relationOrder] :
ranges::views::zip(relatedClasses, orderList)
) {
// Nothing to do, don't create relation mappings with empty names
if (relatedClass.isEmpty())
continue;

const auto relationName = guessOneTypeRelationName(relatedClass);
const auto spaceAlign = QString(relationsMaxSize - relationName.size(), SPACE);

Expand Down Expand Up @@ -1049,6 +1085,10 @@ ModelCreator::createBelongsToManyRelationItem(
for (const auto &[relatedClass, relationOrder] :
ranges::views::zip(relatedClasses, orderList)
) {
// Nothing to do, don't create relation mappings with empty names
if (relatedClass.isEmpty())
continue;

const auto relationName = guessManyTypeRelationName(relatedClass);
const auto spaceAlign = QString(relationsMaxSize - relationName.size(), SPACE);

Expand Down Expand Up @@ -1095,7 +1135,8 @@ ModelCreator::createMutatorsHash(const QString &className, const QStringList &ac
mutatorItemsList << createMutatorItem(className, mutator, mutatorsMaxSize);

// Join and replace the main stub
const auto mutatorItems = mutatorItemsList.join(NEWLINE);
// The wrapMutatorItemsList() allows to create initializer like u_mutators {}
const auto mutatorItems = wrapMutatorItemsList(mutatorItemsList);

return QString(ModelMutatorsStub)
.replace(QStringLiteral("{{ mutatorItems }}"), mutatorItems)
Expand All @@ -1110,10 +1151,12 @@ ModelCreator::prepareMutatorNames(const QStringList &accessors,
std::set<QString> appendsSet;

for (const auto &accessor : accessors)
accessorsSet.emplace(StringUtils::snake(accessor));
if (!accessor.isEmpty())
accessorsSet.emplace(StringUtils::snake(accessor));

for (const auto &append : appends)
appendsSet.emplace(StringUtils::snake(append));
if (!append.isEmpty())
appendsSet.emplace(StringUtils::snake(append));

accessorsSet.merge(std::move(appendsSet));

Expand Down Expand Up @@ -1157,6 +1200,22 @@ ModelCreator::createMutatorItem(const QString &className, const QString &mutator
.replace(QStringLiteral("{{mutatorNameCamel}}"), mutatorCamel);
}

QString ModelCreator::wrapMutatorItemsList(const QStringList &mutatorItems)
{
// Nothing to do
if (mutatorItems.isEmpty()) T_UNLIKELY
return "";

else T_LIKELY {
auto mutatorItemsJoined = mutatorItems.join(NEWLINE);

mutatorItemsJoined.prepend(NEWLINE)
.append(NOSPACE.arg(NEWLINE, QString(4, SPACE)));

return mutatorItemsJoined;
}
}

/* Global */

QString ModelCreator::createIncludesOrmSection(const CmdOptions &cmdOptions)
Expand Down

0 comments on commit f6a79c4

Please sign in to comment.