From e9d77e98e69267ced698e4fdb7da3a1d95acb7a8 Mon Sep 17 00:00:00 2001 From: AmirMS <104940545+AmelBawa-msft@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:15:37 -0800 Subject: [PATCH] Add template --- .../Public/winget/UserSettings.h | 2 +- src/AppInstallerCommonCore/UserSettings.cpp | 27 +++++++++++++++++++ src/AppInstallerSharedLib/JsonUtil.cpp | 21 +++++++++++++++ .../Public/winget/JsonUtil.h | 3 +++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/AppInstallerCommonCore/Public/winget/UserSettings.h b/src/AppInstallerCommonCore/Public/winget/UserSettings.h index 13d2304d8e..ff3cb24373 100644 --- a/src/AppInstallerCommonCore/Public/winget/UserSettings.h +++ b/src/AppInstallerCommonCore/Public/winget/UserSettings.h @@ -205,7 +205,7 @@ namespace AppInstaller::Settings // Experiments using Experiments_t = std::map; - 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 diff --git a/src/AppInstallerCommonCore/UserSettings.cpp b/src/AppInstallerCommonCore/UserSettings.cpp index 9e13610537..b30882ddaa 100644 --- a/src/AppInstallerCommonCore/UserSettings.cpp +++ b/src/AppInstallerCommonCore/UserSettings.cpp @@ -75,6 +75,32 @@ namespace AppInstaller::Settings return convertedValue; } + template<> + std::string GetValueString(std::map 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 ParseSettingsContent(const std::string& content, std::string_view settingName, std::vector& warnings) { Json::Value root; @@ -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) diff --git a/src/AppInstallerSharedLib/JsonUtil.cpp b/src/AppInstallerSharedLib/JsonUtil.cpp index 00d598b9db..4373c6de96 100644 --- a/src/AppInstallerSharedLib/JsonUtil.cpp +++ b/src/AppInstallerSharedLib/JsonUtil.cpp @@ -67,6 +67,27 @@ namespace AppInstaller::JSON return std::nullopt; } + + template<> + std::optional> GetValue(const Json::Value& node) + { + std::map 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) diff --git a/src/AppInstallerSharedLib/Public/winget/JsonUtil.h b/src/AppInstallerSharedLib/Public/winget/JsonUtil.h index f433417e18..5d77ef0e2f 100644 --- a/src/AppInstallerSharedLib/Public/winget/JsonUtil.h +++ b/src/AppInstallerSharedLib/Public/winget/JsonUtil.h @@ -33,6 +33,9 @@ namespace AppInstaller::JSON template<> std::optional> GetValue>(const Json::Value& node); + + template<> + std::optional> GetValue(const Json::Value& node); #ifndef WINGET_DISABLE_FOR_FUZZING // For cpprestsdk JSON