winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

DscComposableObject.cpp (3616B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "DscComposableObject.h"
      5 
      6 namespace AppInstaller::CLI
      7 {
      8     namespace
      9     {
     10         std::string GetTypeString(Json::ValueType type)
     11         {
     12             switch (type)
     13             {
     14             case Json::nullValue:
     15                 return "null";
     16             case Json::intValue:
     17             case Json::uintValue:
     18             case Json::realValue:
     19                 return "number";
     20             case Json::stringValue:
     21                 return "string";
     22             case Json::booleanValue:
     23                 return "boolean";
     24             case Json::arrayValue:
     25                 return "array";
     26             case Json::objectValue:
     27                 return "object";
     28             default:
     29                 THROW_HR(E_UNEXPECTED);
     30             }
     31         }
     32     }
     33 
     34     namespace details
     35     {
     36         const Json::Value* GetProperty(const Json::Value& object, std::string_view name)
     37         {
     38             return object.find(name.data(), name.data() + name.length());
     39         }
     40 
     41         void AddProperty(Json::Value& object, std::string_view name, std::optional<Json::Value>&& value)
     42         {
     43             if (value)
     44             {
     45                 object[std::string{ name }] = std::move(value).value();
     46             }
     47         }
     48 
     49         Json::Value GetBaseSchema(const std::string& title)
     50         {
     51             Json::Value result{ Json::ValueType::objectValue };
     52 
     53             result["$schema"] = "http://json-schema.org/draft-07/schema#";
     54             result["title"] = title;
     55             result["type"] = "object";
     56             result["additionalProperties"] = false;
     57 
     58             return result;
     59         }
     60 
     61         void AddPropertySchema(
     62             Json::Value& object,
     63             std::string_view name,
     64             DscComposablePropertyFlag flags,
     65             Json::ValueType type,
     66             std::string_view description,
     67             const std::vector<std::string>& enumValues,
     68             const std::optional<std::string>& defaultValue)
     69         {
     70             Json::Value& propertiesObject = object["properties"];
     71 
     72             if (propertiesObject.isNull())
     73             {
     74                 propertiesObject = Json::Value{ Json::ValueType::objectValue };
     75             }
     76 
     77             std::string nameString{ name };
     78 
     79             Json::Value property{ Json::ValueType::objectValue };
     80 
     81             if (type != Json::ValueType::objectValue)
     82             {
     83                 property["type"] = GetTypeString(type);
     84             }
     85 
     86             property["description"] = std::string{ description };
     87 
     88             if (!enumValues.empty())
     89             {
     90                 Json::Value enumArray{ Json::ValueType::arrayValue };
     91 
     92                 for (const std::string& enumValue : enumValues)
     93                 {
     94                     enumArray.append(enumValue);
     95                 }
     96 
     97                 property["enum"] = std::move(enumArray);
     98             }
     99 
    100             if (defaultValue)
    101             {
    102                 property["default"] = defaultValue.value();
    103             }
    104 
    105             propertiesObject[nameString] = std::move(property);
    106 
    107             if (WI_IsFlagSet(flags, DscComposablePropertyFlag::Required))
    108             {
    109                 Json::Value& requiredArray = object["required"];
    110 
    111                 if (requiredArray.isNull())
    112                 {
    113                     requiredArray = Json::Value{ Json::ValueType::arrayValue };
    114                 }
    115 
    116                 requiredArray.append(nameString);
    117             }
    118         }
    119     }
    120 }