private static string ResolveParamsAndVariables()

in BenchPress/Generators/AzureDeploymentImporter.cs [282:352]


    private static string ResolveParamsAndVariables(
        string stringToResolve,
        JsonObject armTemplateObject
    )
    {
        // Find and remove square brackets from the parameter/variable string. Square brackets are specific to
        // ARM template syntax and are not needed in generated tests.
        stringToResolve = Regex.Replace(
            stringToResolve,
            s_squareBracketPattern,
            s_squareBracketSubstituition
        );

        // Find all matches in the parameter/variable string that follows the pattern of "parameters('...')" or
        // "variables('...')". The regular expression pattern defines two named subexpressions: paramOrVarType, which
        // represents the type of parameter/variable (e.g., "parameters" or "variables"), and paramOrVarName, which
        // represents the name of the parameter/variable.
        var matches = s_parametersOrVariablesRegex.Matches(stringToResolve);
        foreach (Match match in matches)
        {
            var name = match.Groups[s_paramOrVarNameGroupKey].Value;
            var type = match.Groups[s_paramOrVarTypeGroupKey].Value;
            var resolvedValue = System.String.Empty;

            if (!string.IsNullOrWhiteSpace(name))
            {
                // ARM templates will contain a JSON Object representing parameters and variables for the ARM template.
                // Get the correct JSON Object using the type from the regex match (e.g., "parameters" or "variables").
                var parametersOrVariablesObj = armTemplateObject[type];
                if (parametersOrVariablesObj != null)
                {
                    // Get the value of the parameter/variable from the JSON Object. If the value is still a JSON
                    // Object, that means it is a parameter that has a default value, so get the default value (if it
                    // exists). Otherwise, the value can be converted to a string and assigned to resolveValue.
                    var resolvedValueNode = parametersOrVariablesObj[name];
                    if (resolvedValueNode != null)
                    {
                        if (resolvedValueNode is JsonObject)
                        {
                            resolvedValue = resolvedValueNode[s_defaultValueKey]?.ToString();
                        }
                        else
                        {
                            resolvedValue = resolvedValueNode.ToString();
                        }
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(resolvedValue))
            {
                // If square brackets are present in the resolved value, remove them from the value. Square brackets
                // are specific to ARM template syntax to represent expressions and are not needed in generated tests.
                // If the resolved value does not have square brackets, then the resolved value does not contain ARM
                // functions and should be wrapped in single quotes.
                if (Regex.Match(resolvedValue, s_squareBracketPattern).Success)
                {
                    resolvedValue = Regex.Replace(
                        resolvedValue,
                        s_squareBracketPattern,
                        s_squareBracketSubstituition
                    );
                }
                else
                {
                    resolvedValue = "\'" + resolvedValue + "\'";
                }
                stringToResolve = stringToResolve.Replace(match.Value, resolvedValue);
            }
        }
        return stringToResolve;
    }