Skip to content

Commit

Permalink
Add template
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelBawa-msft committed Nov 14, 2024
1 parent 3e66324 commit e9d77e9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AppInstallerCommonCore/Public/winget/UserSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ namespace AppInstaller::Settings

// Experiments
using Experiments_t = std::map<std::string, bool>;
SETTINGMAPPING_SPECIALIZATION(Setting::Experiments, std::string, Experiments_t, {}, ".experiments"sv);
SETTINGMAPPING_SPECIALIZATION(Setting::Experiments, Experiments_t, Experiments_t, {}, ".experiments"sv);

// Used to deduce the SettingVariant type; making a variant that includes std::monostate and all SettingMapping types.
template <size_t... I>
Expand Down
27 changes: 27 additions & 0 deletions src/AppInstallerCommonCore/UserSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ namespace AppInstaller::Settings
return convertedValue;
}

template<>
std::string GetValueString(std::map<std::string, bool> value)
{
std::stringstream convertedValue;
convertedValue << "{";

bool first = true;
for (auto const& entry : value)
{
if (first)
{
first = false;
}
else
{
convertedValue << ", ";
}

convertedValue << "'" << entry.first << "' = " << entry.second;
}

convertedValue << "}";

return convertedValue.str();
}

std::optional<Json::Value> ParseSettingsContent(const std::string& content, std::string_view settingName, std::vector<UserSettings::Warning>& warnings)
{
Json::Value root;
Expand Down Expand Up @@ -278,6 +304,7 @@ namespace AppInstaller::Settings
WINGET_VALIDATE_PASS_THROUGH(UninstallPurgePortablePackage)
WINGET_VALIDATE_PASS_THROUGH(NetworkWingetAlternateSourceURL)
WINGET_VALIDATE_PASS_THROUGH(MaxResumes)
WINGET_VALIDATE_PASS_THROUGH(Experiments)

#ifndef AICLI_DISABLE_TEST_HOOKS
WINGET_VALIDATE_PASS_THROUGH(EnableSelfInitiatedMinidump)
Expand Down
21 changes: 21 additions & 0 deletions src/AppInstallerSharedLib/JsonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ namespace AppInstaller::JSON

return std::nullopt;
}

template<>
std::optional<std::map<std::string, bool>> GetValue(const Json::Value& node)
{
std::map<std::string, bool> result;

if (node.isObject())
{
for (const auto& entry : node.getMemberNames())
{
if (node[entry].isBool())
{
result[entry] = node[entry].asBool();
}
}

return result;
}

return std::nullopt;
}

#ifndef WINGET_DISABLE_FOR_FUZZING
utility::string_t GetUtilityString(std::string_view nodeName)
Expand Down
3 changes: 3 additions & 0 deletions src/AppInstallerSharedLib/Public/winget/JsonUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace AppInstaller::JSON

template<>
std::optional<std::vector<std::string>> GetValue<std::vector<std::string>>(const Json::Value& node);

template<>
std::optional<std::map<std::string, bool>> GetValue(const Json::Value& node);

#ifndef WINGET_DISABLE_FOR_FUZZING
// For cpprestsdk JSON
Expand Down

0 comments on commit e9d77e9

Please sign in to comment.