Exemple #1
0
        public CoercionThriftCodec(IThriftCodec <T> codec, TypeCoercion typeCoercion)
        {
            Guard.ArgumentNotNull(codec, nameof(codec));
            Guard.ArgumentNotNull(typeCoercion, nameof(typeCoercion));

            this._codec        = codec;
            this._typeCoercion = typeCoercion;
            this.Type          = typeCoercion.ThriftType;
        }
        public Term Visit(TypeCoercion typeCoercion)
        {
            if (_args.Count != 1)
            {
                throw new ExprArgException();
            }

            //type coercions are only relevant for the type inference
            return(_args[0]);
        }
        public IEnumerable <string> Transform(ExpressionNode item)
        {
            _EncounteredOutputStyleBlock = true;
            if (!(ShouldRender && _EncounteredOutputStyleBlock))
            {
                yield break;
            }


            if (item.IfClause != null)
            {
                var ifResult = Environment.Evaluation.EvaluateDynamic(item.IfClause, ExpressionParserTransformer);
                if (TypeCoercion.GetTruthy(ifResult) == false)
                {
                    if (item.ElseClause != null)
                    {
                        foreach (var subItem in item.ElseClause.Transform(this))
                        {
                            yield return(subItem);
                        }
                    }
                    yield break;
                }
            }


            var result = Environment.Evaluation.EvaluateDynamic(item.ExpressionParserNode, ExpressionParserTransformer);

            switch (result)
            {
            case ExpressionParser.Void _:
                yield break;

            case ASTNode astNode:
                foreach (var output in astNode.Transform(this))
                {
                    yield return(output);
                }
                yield break;

            default:
                yield return(JinjaCustomStringProvider.Instance.ToString(result));

                yield break;
            }
        }
        public IEnumerable <string> Transform(IfNode item)
        {
            if (ShouldRender == false)
            {
                yield break;
            }
            foreach (var condition in item.Conditions)
            {
                var result     = Environment.Evaluation.EvaluateDynamic(condition.Expression.ExpressionParserNode, ExpressionParserTransformer);
                var boolResult = TypeCoercion.GetTruthy(result);

                if (boolResult)
                {
                    _EncounteredOutputStyleBlock = true;

                    foreach (var output in condition.Transform(this))
                    {
                        yield return(output);
                    }
                    yield break;
                }
            }
        }
Exemple #5
0
        internal object?Evaluate(IDictionary <string, object?> variables)
        {
            var typedArguments = Parameters.Select(param =>
            {
                if (variables.TryGetValue(param.Name, out var paramValue) == false)
                {
                    throw new NotImplementedException();
                }
                if (TypeCoercion.CanCast(paramValue?.GetType() ?? typeof(object), param.Type) == false)
                {
                    throw new NotImplementedException();
                }
                return(Convert.ChangeType(paramValue, param.Type, CultureInfo.InvariantCulture));
            }).ToArray();
            var invokeMethod = Delegate.GetType().GetMethod("Invoke", ParameterTypes);

            if (ReturnType == typeof(void))
            {
                invokeMethod.Invoke(Delegate, typedArguments);
                return(Void.Instance);
            }
            return(invokeMethod.Invoke(Delegate, typedArguments));
        }
        public IEnumerable <string> Transform(ForNode item)
        {
            _EncounteredOutputStyleBlock = true;
            if (!(ShouldRender && _EncounteredOutputStyleBlock))
            {
                yield break;
            }

            object?evalObj;

            if (item.Expression != null)
            {
                evalObj = Environment.Evaluation.EvaluateDynamic(item.Expression.Expression, Scopes);
            }
            else
            {
                evalObj = item.AlreadyEvaluatedObject;
            }

            Func <object?[], object?> loopFunction = args =>
            {
                if (args?.Length != 1)
                {
                    throw new NotImplementedException();
                }
                return(new ForNode(item.PrimaryBlock, item.ElseBlock, item.VariableNames,
                                   args[0], item.Filter, item.Recursive,
                                   item.EndParsingNode, item.WhiteSpaceControl));
            };

            var arr = CollectionEx.ToArray(evalObj) ?? Array.Empty <object>();

            if (item.Filter != null)
            {
                var filtered = new List <object?>();
                foreach (var arrItem in arr)
                {
                    var unpacked = ReflectionHelpers.Unpack(arrItem, item.VariableNames.Length);
                    Scopes.Push($"ForNode | Filter: {item.Filter} Item: {arrItem}");
                    for (var i = 0; i < unpacked.Length; ++i)
                    {
                        Scopes.Current.DefineAndSetVariable(item.VariableNames[i], unpacked[i]);
                    }
                    var result = Environment.Evaluation.EvaluateDynamic(item.Filter.Expression, Scopes);
                    Scopes.Pop($"ForNode | Filter: {item.Filter} Item: {arrItem}");
                    if (TypeCoercion.GetTruthy(result))
                    {
                        filtered.Add(arrItem);
                    }
                }
                arr = filtered.ToArray();
            }

            if (arr.Length == 0 && item.ElseBlock != null)
            {
                foreach (var output in item.ElseBlock.Transform(this))
                {
                    yield return(output);
                }
                yield break;
            }


            var depth = 0;

            if (Scopes.Current.TryGetVariable <LoopInfo>("loop", out var previousLoopInfo) && previousLoopInfo != null)
            {
                depth = previousLoopInfo.depth0 + 1;
            }

            var loopInfo = new LoopInfo(arr, loopFunction, depth);

            for (var index = 0; index < arr.Length; ++index)
            {
                loopInfo.index0 = index;
                var arrItem  = arr[index];
                var unpacked = ReflectionHelpers.Unpack(arrItem, item.VariableNames.Length);

                Scopes.Push($"ForNode: {item.Expression} Item: {arrItem}");
                for (var i = 0; i < unpacked.Length; ++i)
                {
                    Scopes.Current.DefineAndSetVariable(item.VariableNames[i], unpacked[i]);
                }
                Scopes.Current.DefineAndSetVariable("loop", loopInfo);
                foreach (var output in item.PrimaryBlock.Transform(this))
                {
                    yield return(output);
                }
                Scopes.Pop($"ForNode: {item.Expression} Item: {arrItem}");
            }
            yield break;
        }
Exemple #7
0
        public IThriftCodec Load(ThriftType type)
        {
            try
            {
                // When we need to load a codec for a type the first time, we push it on the
                // thread-local stack before starting the load, and pop it off afterwards,
                // so that we can detect recursive loads.
                _stack.Value.Push(type);

                switch (type.ProtocolType)
                {
                case ThriftProtocolType.Struct:
                {
                    return(_factory.GenerateThriftTypeCodec(this, type.StructMetadata));
                }

                case ThriftProtocolType.Map:
                {
                    var codecType = typeof(MapThriftCodec <,>).MakeGenericType(type.KeyTypeReference.CSharpType, type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.KeyTypeReference), GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.Set:
                {
                    var codecType = typeof(SetThriftCodec <>).MakeGenericType(type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.List:
                {
                    var codecType = typeof(ListThriftCodec <>).MakeGenericType(type.ValueTypeReference.CSharpType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type, GetElementCodec(type.ValueTypeReference)));
                }

                case ThriftProtocolType.Enum:
                {
                    var codecType = typeof(EnumThriftCodec <>).MakeGenericType(type.EnumMetadata.EnumType);
                    return((IThriftCodec)Activator.CreateInstance(codecType, type));
                }

                default:
                    if (type.IsCoerced)
                    {
                        var          codec    = GetCodec(type.UncoercedType);
                        TypeCoercion coercion = this.Catalog.GetDefaultCoercion(type.CSharpType);
                        var          coercionThriftCodecType = typeof(CoercionThriftCodec <>).MakeGenericType(type.UncoercedType.CSharpType);
                        return((IThriftCodec)Activator.CreateInstance(coercionThriftCodecType, codec, coercion));
                    }
                    else
                    {
                        return(GetCodec(type.UncoercedType));
                    }
                    throw new ThriftyException("Unsupported Thrift type " + type);
                }
            }
            finally
            {
                if (_stack.Value.Count > 0)
                {
                    ThriftType top = _stack.Value.Pop();
                    if (!type.Equals(top))
                    {
                        throw new ThriftyException(
                                  $"ThriftCatalog circularity detection stack is corrupt: expected {type}, but got {top}");
                    }
                }
            }
        }