/// <summary>
        ///     Executes the specified formatter.
        /// </summary>
        public virtual async ObjectPromise Execute(
            FormatterCache formatter,
            object sourceType,
            FormatterArgumentType[] args)
        {
            Log(() => "Execute");

            var mapedValues = new object[formatter.TestedTypes.Arguments.Count];
            var i           = 0;

            foreach (var formatterArgumentMap in formatter.TestedTypes.Arguments)
            {
                var valueObtainValue = formatterArgumentMap.Value.ObtainValue(sourceType, args);
                if (formatterArgumentMap.Value.ConverterFunc != null)
                {
                    var convValue = formatterArgumentMap.Value.ConverterFunc(valueObtainValue);
                    valueObtainValue = convValue ?? valueObtainValue;
                }
                mapedValues[i++] = valueObtainValue;
            }

            var taskAlike = formatter.TestedTypes.PrepareInvoke(mapedValues)
                            .Invoke(
                formatter.Model.FunctionTarget,
                mapedValues);

            return(await taskAlike.UnpackFormatterTask());
        }
示例#2
0
        /// <summary>
        ///     Executes the specified formatter.
        /// </summary>
        public virtual ObjectPromise Execute(
            FormatterCache formatter,
            object sourceValue,
            ParserOptions parserOptions,
            FormatterArgumentType[] args)
        {
            Log(parserOptions, () => $"Execute the formatter {formatter.Model.Name} with arguments",
                () => args.ToDictionary(e => e.Name, e => (object)e));

            var mappedValues = formatter.ValueBuffer;

            var i = 0;

            foreach (var formatterArgumentMap in formatter.TestedTypes.Arguments)
            {
                var valueObtainValue = formatterArgumentMap.Value.ObtainValue(sourceValue, args);
                if (formatterArgumentMap.Value.ConverterFunc != null)
                {
                    var convValue = formatterArgumentMap.Value.ConverterFunc(valueObtainValue);
                    valueObtainValue = convValue ?? valueObtainValue;
                }
                mappedValues[i++] = valueObtainValue;
            }

            try
            {
                var functionTarget = formatter.Model.FunctionTarget;
                var method         = formatter.TestedTypes.PrepareInvoke(mappedValues);

                if (formatter.Model.LinkFunctionTarget && !method.IsStatic &&
                    method.DeclaringType == sourceValue.GetType())
                {
                    functionTarget = sourceValue;
                }

                var taskAlike = method.Invoke(functionTarget, mappedValues);
                return(taskAlike.UnpackFormatterTask());
            }
            catch (Exception e)
            {
                if (ExceptionHandling == FormatterServiceExceptionHandling.IgnoreSilently)
                {
                    return(AsyncHelper.EmptyPromise <object>());
                }

                if (ExceptionHandling == FormatterServiceExceptionHandling.PrintExceptions)
                {
                    return((e.ToString() as object).ToPromise());
                }

                throw;
            }
        }
        //public async ObjectPromise Execute(
        //	[NotNull] FormatterArgumentType[] args,
        //	object sourceValue,
        //	[CanBeNull] string name,
        //	ParserOptions options,
        //	ScopeData scope)
        //{
        //	var cacheItem = PrepareCallMostMatchingFormatter(sourceValue.GetType(), args, name, options, scope);
        //	if (cacheItem == null)
        //	{
        //		return FormatterFlow.Skip;
        //	}

        //	return await Execute(cacheItem.Value, sourceValue, args);
        //}

        /// <inheritdoc />
        public FormatterCache?PrepareCallMostMatchingFormatter(
            Type type,
            FormatterArgumentType[] arguments,
            string name,
            ParserOptions parserOptions,
            ScopeData scope)
        {
            var compareKey = new FormatterCacheCompareKey(type, arguments, name);

            if (Cache != null && Cache.TryGetValue(compareKey, out var cacheItem))
            {
                return(cacheItem);
            }
            var hasFormatter = PrepareGetMatchingFormatterOn(type, arguments, name)
                               .Where(e => e != null)
                               .ToArray();

            var services = new ServiceCollection(ServiceCollectionAccess);

            services.AddService(parserOptions);
            services.AddService(scope.Profiler);

            foreach (var morestachioFormatterModel in hasFormatter)
            {
                var tryCompose = PrepareComposeValues(morestachioFormatterModel,
                                                      type,
                                                      morestachioFormatterModel.Function,
                                                      services,
                                                      arguments
                                                      );
                if (tryCompose != null)
                {
                    var cache = new FormatterCache(morestachioFormatterModel, tryCompose);
                    if (Cache != null)
                    {
                        Cache[compareKey] = cache;
                    }

                    return(cache);
                }
            }

            if (Cache != null)
            {
                return(Cache[compareKey] = null);
            }

            return(null);
        }