/// <inheritdoc />
        public async Task <ContextObject> GetValue(ContextObject context, ScopeData scopeData)
        {
            var c = await context.GetContextForPath(Value, scopeData);

            if (FormatString != null && FormatString.Any())
            {
                var argList = new List <KeyValuePair <string, object> >();

                foreach (var formatterArgument in FormatString)
                {
                    var value = context.FindNextNaturalContextObject().CloneForEdit();
                    value = await formatterArgument.Item2.GetValue(value, scopeData);

                    if (value == null)
                    {
                        argList.Add(new KeyValuePair <string, object>(formatterArgument.Item1.ArgumentName, null));
                    }
                    else
                    {
                        await value.EnsureValue();

                        argList.Add(new KeyValuePair <string, object>(formatterArgument.Item1.ArgumentName, value.Value));
                    }
                }
                //we do NOT await the task here. We await the task only if we need the value
                context.Value = c.Format(TargetFormatterName, argList.ToArray());
            }
            else
            {
                context.Value = c.Format(TargetFormatterName, new KeyValuePair <string, object> [0]);
            }
            return(context);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task <ContextObject> GetValue(ContextObject contextObject, ScopeData scopeData)
        {
            var contextForPath = await contextObject.GetContextForPath(PathParts, scopeData);

            if (!Formats.Any() && FormatterName == null)
            {
                return(contextForPath);
            }

            var argList = new List <KeyValuePair <string, object> >();

            foreach (var formatterArgument in Formats)
            {
                var value = contextObject.FindNextNaturalContextObject().CloneForEdit();
                value = await formatterArgument.MorestachioExpression.GetValue(value, scopeData);

                if (value == null)
                {
                    argList.Add(new KeyValuePair <string, object>(formatterArgument.Name, null));
                }
                else
                {
                    await value.EnsureValue();

                    argList.Add(new KeyValuePair <string, object>(formatterArgument.Name, value.Value));
                }
            }

            var formatterdContext = contextForPath.CloneForEdit();

            formatterdContext.Value = await formatterdContext.Format(FormatterName, argList.ToArray());

            return(formatterdContext);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public async ContextObjectPromise GetValue(ContextObject contextObject, ScopeData scopeData)
        {
            if (!PathParts.Any() && Formats.Count == 1 && FormatterName == "")
            {
                //indicates the usage of brackets
                return(await Formats[0].GetValue(contextObject, scopeData));
            }

            var contextForPath = contextObject.GetContextForPath(PathParts, scopeData, this);

            if (!Formats.Any() && FormatterName == null)
            {
                return(contextForPath);
            }

            if (contextForPath == contextObject)
            {
                contextForPath = contextObject.CloneForEdit();
            }

            var arguments    = new FormatterArgumentType[Formats.Count];
            var naturalValue = contextObject.FindNextNaturalContextObject();

            for (var index = 0; index < Formats.Count; index++)
            {
                var formatterArgument = Formats[index];
                var value             = await formatterArgument.MorestachioExpression.GetValue(naturalValue, scopeData);

                arguments[index] = new FormatterArgumentType(index, formatterArgument.Name, value?.Value);
            }
            //contextForPath.Value = await contextForPath.Format(FormatterName, argList, scopeData);

            if (Cache == null)
            {
                Cache = contextForPath.PrepareFormatterCall(
                    contextForPath.Value?.GetType() ?? typeof(object),
                    FormatterName,
                    arguments,
                    scopeData);
            }

            if (Cache != null /* && !Equals(Cache.Value, default(FormatterCache))*/)
            {
                contextForPath.Value = await scopeData.ParserOptions.Formatters.Execute(Cache, contextForPath.Value, scopeData.ParserOptions, arguments);

                contextForPath.MakeSyntetic();
            }
            return(contextForPath);
        }