private static object EvaluateIndex()

in src/PSRule.Rules.Azure/Data/Template/ExpressionBuilder.cs [162:193]


    private static object EvaluateIndex(ITemplateContext context, ExpressionFnOuter leftSideExpression, ExpressionFnOuter index)
    {
        // Get the value of the expression to the left side of the index.
        var leftValue = leftSideExpression(context);

        // Get the number or string that was used as the index value. i.e. [0], ["name"]
        var indexValue = index(context);

        if (leftValue is IMock mock)
            return mock.GetValue(indexValue);

        // If the left side is an array, get that element number.
        if (ExpressionHelpers.TryArray(leftValue, out var array) && ExpressionHelpers.TryConvertInt(indexValue, out var arrayIndex))
            return array.GetValue(arrayIndex);

        // If the left side is an object, then get the property.
        if (leftValue is JObject jObject && ExpressionHelpers.TryString(indexValue, out var propertyName))
        {
            if (!jObject.TryGetValue(propertyName, StringComparison.OrdinalIgnoreCase, out var property))
                throw new ExpressionReferenceException(propertyName, string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.PropertyNotFound, propertyName));

            return property;
        }

        if (leftValue is ILazyObject lazy && ExpressionHelpers.TryConvertString(indexValue, out var memberName) && lazy.TryProperty(memberName, out var value))
            return value;

        if (ExpressionHelpers.TryString(indexValue, out propertyName) && ExpressionHelpers.TryPropertyOrField(leftValue, propertyName, out value))
            return value;

        throw new InvalidOperationException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.IndexInvalid, indexValue));
    }