protected sealed override Object EvaluateCore(
            EvaluationContext context,
            out ResultMemory resultMemory)
        {
            resultMemory = null;
            var left = Parameters[0].Evaluate(context);

            if (left.IsPrimitive)
            {
                var leftString = left.ConvertToString();

                var right = Parameters[1].Evaluate(context);
                if (right.IsPrimitive)
                {
                    var rightString = right.ConvertToString();
                    return(leftString.IndexOf(rightString, StringComparison.OrdinalIgnoreCase) >= 0);
                }
            }
            else if (left.TryGetCollectionInterface(out var collection) &&
                     collection is IReadOnlyArray array &&
                     array.Count > 0)
            {
                var right = Parameters[1].Evaluate(context);
                foreach (var item in array)
                {
                    var itemResult = EvaluationResult.CreateIntermediateResult(context, item);
                    if (right.AbstractEqual(itemResult))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #2
0
        private TemplateToken ConvertToTemplateToken(
            TemplateContext context,
            EvaluationResult result)
        {
            // Literal
            if (TryConvertToLiteralToken(context, result, out LiteralToken literal))
            {
                return(literal);
            }
            // Known raw types
            else if (!Object.ReferenceEquals(result.Raw, null))
            {
                if (result.Raw is SequenceToken sequence)
                {
                    context.Memory.AddBytes(sequence, true);
                    return(sequence);
                }
                else if (result.Raw is MappingToken mapping)
                {
                    context.Memory.AddBytes(mapping, true);
                    return(mapping);
                }
            }

            // Leverage the expression SDK to traverse the object
            if (result.TryGetCollectionInterface(out Object collection))
            {
                if (collection is IReadOnlyObject dictionary)
                {
                    var mapping = CreateMappingToken(context);

                    foreach (KeyValuePair <String, Object> pair in dictionary)
                    {
                        var keyToken    = CreateStringToken(context, pair.Key);
                        var valueResult = EvaluationResult.CreateIntermediateResult(null, pair.Value);
                        var valueToken  = ConvertToTemplateToken(context, valueResult);
                        mapping.Add(keyToken, valueToken);
                    }

                    return(mapping);
                }
                else if (collection is IReadOnlyArray list)
                {
                    var sequence = CreateSequenceToken(context);

                    foreach (var item in list)
                    {
                        var itemResult = EvaluationResult.CreateIntermediateResult(null, item);
                        var itemToken  = ConvertToTemplateToken(context, itemResult);
                        sequence.Add(itemToken);
                    }

                    return(sequence);
                }
            }

            throw new ArgumentException(TemplateStrings.UnableToConvertToTemplateToken(result.Value?.GetType().FullName));
        }
        protected sealed override Object EvaluateCore(
            EvaluationContext context,
            out ResultMemory resultMemory)
        {
            resultMemory = null;
            var items = Parameters[0].Evaluate(context);

            // Array
            if (items.TryGetCollectionInterface(out var collection) &&
                collection is IReadOnlyArray array &&
                array.Count > 0)
            {
                var result = new StringBuilder();
                var memory = new MemoryCounter(this, context.Options.MaxMemory);

                // Append the first item
                var item       = array[0];
                var itemResult = EvaluationResult.CreateIntermediateResult(context, item);
                var itemString = itemResult.ConvertToString();
                memory.Add(itemString);
                result.Append(itemString);

                // More items?
                if (array.Count > 1)
                {
                    var separator = ",";
                    if (Parameters.Count > 1)
                    {
                        var separatorResult = Parameters[1].Evaluate(context);
                        if (separatorResult.IsPrimitive)
                        {
                            separator = separatorResult.ConvertToString();
                        }
                    }

                    for (var i = 1; i < array.Count; i++)
                    {
                        // Append the separator
                        memory.Add(separator);
                        result.Append(separator);

                        // Append the next item
                        var nextItem       = array[i];
                        var nextItemResult = EvaluationResult.CreateIntermediateResult(context, nextItem);
                        var nextItemString = nextItemResult.ConvertToString();
                        memory.Add(nextItemString);
                        result.Append(nextItemString);
                    }
                }

                return(result.ToString());
            }
Example #4
0
 public Boolean MoveNext()
 {
     if (m_enumerator.MoveNext())
     {
         m_current = EvaluationResult.CreateIntermediateResult(m_context, m_enumerator.Current);
         m_index++;
         return(true);
     }
     else
     {
         m_current = null;
         return(false);
     }
 }
Example #5
0
        internal static Boolean ConvertToIfResult(
            TemplateContext context,
            TemplateToken ifResult)
        {
            var expression = ifResult.Traverse().FirstOrDefault(x => x is ExpressionToken);

            if (expression != null)
            {
                throw new ArgumentException($"Unexpected type '{expression.GetType().Name}' encountered while reading 'if'.");
            }

            var evaluationResult = EvaluationResult.CreateIntermediateResult(null, ifResult);

            return(evaluationResult.IsTruthy);
        }
Example #6
0
 public Boolean MoveNext()
 {
     if (m_enumerator.MoveNext())
     {
         var current = (KeyValuePair <String, Object>)m_enumerator.Current;
         var key     = EvaluationResult.CreateIntermediateResult(m_context, current.Key);
         var value   = EvaluationResult.CreateIntermediateResult(m_context, current.Value);
         m_current = new KeyValuePair <EvaluationResult, EvaluationResult>(key, value);
         m_index++;
         return(true);
     }
     else
     {
         m_current = default(KeyValuePair <EvaluationResult, EvaluationResult>);
         return(false);
     }
 }