Skip to content

Commit 8fd4dd8

Browse files
committed
Refactor duplicate code in schemagen
1 parent b0242cf commit 8fd4dd8

File tree

2 files changed

+40
-54
lines changed

2 files changed

+40
-54
lines changed

SchemaGenerator.cpp

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,33 +1719,7 @@ template <>
17191719

17201720
for (const auto& inputField : inputType.fields)
17211721
{
1722-
std::string fieldName(inputField.name);
1723-
1724-
fieldName[0] = std::toupper(fieldName[0]);
1725-
if (inputField.defaultValue.type() == response::Type::Null)
1726-
{
1727-
sourceFile << R"cpp( auto value)cpp" << fieldName
1728-
<< R"cpp( = )cpp" << getArgumentAccessType(inputField)
1729-
<< R"cpp(::require)cpp" << getTypeModifiers(inputField.modifiers)
1730-
<< R"cpp((")cpp" << inputField.name
1731-
<< R"cpp(", value);
1732-
)cpp";
1733-
}
1734-
else
1735-
{
1736-
sourceFile << R"cpp( auto pair)cpp" << fieldName
1737-
<< R"cpp( = )cpp" << getArgumentAccessType(inputField)
1738-
<< R"cpp(::find)cpp" << getTypeModifiers(inputField.modifiers)
1739-
<< R"cpp((")cpp" << inputField.name
1740-
<< R"cpp(", value);
1741-
auto value)cpp" << fieldName << R"cpp( = (pair)cpp" << fieldName << R"cpp(.second
1742-
? std::move(pair)cpp" << fieldName << R"cpp(.first)
1743-
: )cpp" << getArgumentAccessType(inputField)
1744-
<< R"cpp(::require)cpp" << getTypeModifiers(inputField.modifiers)
1745-
<< R"cpp((")cpp" << inputField.name
1746-
<< R"cpp(", defaultValue));
1747-
)cpp";
1748-
}
1722+
sourceFile << getArgumentDeclaration(inputField, "value", "value", "defaultValue");
17491723
}
17501724

17511725
if (!inputType.fields.empty())
@@ -1942,33 +1916,7 @@ std::future<response::Value> )cpp" << objectType.type
19421916

19431917
for (const auto& argument : outputField.arguments)
19441918
{
1945-
std::string argumentName(argument.name);
1946-
1947-
argumentName[0] = std::toupper(argumentName[0]);
1948-
if (argument.defaultValue.type() == response::Type::Null)
1949-
{
1950-
sourceFile << R"cpp( auto arg)cpp" << argumentName
1951-
<< R"cpp( = )cpp" << getArgumentAccessType(argument)
1952-
<< R"cpp(::require)cpp" << getTypeModifiers(argument.modifiers)
1953-
<< R"cpp((")cpp" << argument.name
1954-
<< R"cpp(", params.arguments);
1955-
)cpp";
1956-
}
1957-
else
1958-
{
1959-
sourceFile << R"cpp( auto pair)cpp" << argumentName
1960-
<< R"cpp( = )cpp" << getArgumentAccessType(argument)
1961-
<< R"cpp(::find)cpp" << getTypeModifiers(argument.modifiers)
1962-
<< R"cpp((")cpp" << argument.name
1963-
<< R"cpp(", params.arguments);
1964-
auto arg)cpp" << argumentName << R"cpp( = (pair)cpp" << argumentName << R"cpp(.second
1965-
? std::move(pair)cpp" << argumentName << R"cpp(.first)
1966-
: )cpp" << getArgumentAccessType(argument)
1967-
<< R"cpp(::require)cpp" << getTypeModifiers(argument.modifiers)
1968-
<< R"cpp((")cpp" << argument.name
1969-
<< R"cpp(", defaultArguments));
1970-
)cpp";
1971-
}
1919+
sourceFile << getArgumentDeclaration(argument, "arg", "params.arguments", "defaultArguments");
19721920
}
19731921
}
19741922

@@ -2733,6 +2681,43 @@ std::string Generator::getArgumentDefaultValue(size_t level, const response::Val
27332681
return argumentDefaultValue.str();
27342682
}
27352683

2684+
std::string Generator::getArgumentDeclaration(const InputField& argument, const char* prefixToken, const char* argumentsToken, const char* defaultToken) const noexcept
2685+
{
2686+
std::ostringstream argumentDeclaration;
2687+
std::string argumentName(argument.name);
2688+
2689+
argumentName[0] = std::toupper(argumentName[0]);
2690+
if (argument.defaultValue.type() == response::Type::Null)
2691+
{
2692+
argumentDeclaration << R"cpp( auto )cpp" << prefixToken << argumentName
2693+
<< R"cpp( = )cpp" << getArgumentAccessType(argument)
2694+
<< R"cpp(::require)cpp" << getTypeModifiers(argument.modifiers)
2695+
<< R"cpp((")cpp" << argument.name
2696+
<< R"cpp(", )cpp" << argumentsToken
2697+
<< R"cpp();
2698+
)cpp";
2699+
}
2700+
else
2701+
{
2702+
argumentDeclaration << R"cpp( auto pair)cpp" << argumentName
2703+
<< R"cpp( = )cpp" << getArgumentAccessType(argument)
2704+
<< R"cpp(::find)cpp" << getTypeModifiers(argument.modifiers)
2705+
<< R"cpp((")cpp" << argument.name
2706+
<< R"cpp(", )cpp" << argumentsToken
2707+
<< R"cpp();
2708+
auto )cpp" << prefixToken << argumentName << R"cpp( = (pair)cpp" << argumentName << R"cpp(.second
2709+
? std::move(pair)cpp" << argumentName << R"cpp(.first)
2710+
: )cpp" << getArgumentAccessType(argument)
2711+
<< R"cpp(::require)cpp" << getTypeModifiers(argument.modifiers)
2712+
<< R"cpp((")cpp" << argument.name
2713+
<< R"cpp(", )cpp" << defaultToken
2714+
<< R"cpp());
2715+
)cpp";
2716+
}
2717+
2718+
return argumentDeclaration.str();
2719+
}
2720+
27362721
std::string Generator::getArgumentAccessType(const InputField& argument) const noexcept
27372722
{
27382723
std::ostringstream argumentType;

include/SchemaGenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class Generator
281281

282282
bool outputSource() const noexcept;
283283
std::string getArgumentDefaultValue(size_t level, const response::Value& defaultValue) const noexcept;
284+
std::string getArgumentDeclaration(const InputField& argument, const char* prefixToken, const char* argumentsToken, const char* defaultToken) const noexcept;
284285
std::string getArgumentAccessType(const InputField& argument) const noexcept;
285286
std::string getResultAccessType(const OutputField& result) const noexcept;
286287
std::string getTypeModifiers(const TypeModifierStack& modifiers) const noexcept;

0 commit comments

Comments
 (0)