/// <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 override async Task <IEnumerable <DocumentItemExecution> > Render(IByteCounterStream outputStream, ContextObject context, ScopeData scopeData)
        {
            var c = await context.GetContextForPath(Value, scopeData);

            if (!await c.Exists())
            {
                return(Children.WithScope(c));
            }
            return(new DocumentItemExecution[0]);
        }
        /// <exception cref="IndexedParseException"></exception>
        /// <inheritdoc />
        public override async Task <IEnumerable <DocumentItemExecution> > Render(IByteCounterStream outputStream, ContextObject context, ScopeData scopeData)
        {
            //if we're in the same scope, just negating, then we want to use the same object
            var c = await context.GetContextForPath(Value, scopeData);

            if (!await c.Exists())
            {
                return(new DocumentItemExecution[0]);
            }

            if (!(c.Value is IEnumerable value) || value is string || value is IDictionary <string, object> )
            {
                var path   = new Stack <string>();
                var parent = context.Parent;
                while (parent != null)
                {
                    path.Push(parent.Key);
                    parent = parent.Parent;
                }

                throw new IndexedParseException(CharacterLocationExtended.Empty,
                                                string.Format("{1}'{0}' is used like an array by the template, but is a scalar value or object in your model." + " Complete Expression until Error:{2}",
                                                              Value, base.ExpressionStart, (path.Count == 0 ? "Empty" : path.Aggregate((e, f) => e + "\r\n" + f))));
            }

            var scopes = new List <DocumentItemExecution>();

            //Use this "lookahead" enumeration to allow the $last keyword
            var index      = 0;
            var enumerator = value.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(new DocumentItemExecution[0]);
            }

            var current = enumerator.Current;

            do
            {
                var next         = enumerator.MoveNext() ? enumerator.Current : null;
                var innerContext = new ContextCollection(index, next == null, context.Options, $"[{index}]", c)
                {
                    Value = current
                };
                scopes.AddRange(Children.WithScope(innerContext));
                index++;
                current = next;
            } while (current != null && ContinueBuilding(outputStream, context));

            return(scopes);
        }
Ejemplo n.º 5
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);
        }
        /// <inheritdoc />
        public override async Task <IEnumerable <DocumentItemExecution> > Render(IByteCounterStream outputStream,
                                                                                 ContextObject context,
                                                                                 ScopeData scopeData)
        {
            var contextObject = context.Parent ?? context;
            var c             = await context.GetContextForPath(Value, scopeData);

            if (await c.Exists())
            {
                scopeData.ExecuteElse = false;
                return(Children.WithScope(contextObject));
            }
            scopeData.ExecuteElse = true;
            return(new DocumentItemExecution[0]);
        }
        /// <inheritdoc />
        public override async Task <IEnumerable <DocumentItemExecution> > Render(IByteCounterStream outputStream,
                                                                                 ContextObject context,
                                                                                 ScopeData scopeData)
        {
            //we are checking the parent value not our current value
            var c = await context.GetContextForPath(Value, scopeData);

            if (await c.Exists())
            {
                scopeData.ExecuteElse = false;
                return(Children.WithScope(context.IsNaturalContext || context.Parent == null ? context : context.Parent));
            }
            scopeData.ExecuteElse = true;
            return(new DocumentItemExecution[0]);
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public override async Task <IEnumerable <DocumentItemExecution> > Render(IByteCounterStream outputStream, ContextObject context, ScopeData scopeData)
        {
            //try to locate the value in the context, if it exists, append it.
            var contextObject = context != null ? (await context.GetContextForPath(Value, scopeData)) : null;

            if (contextObject != null)
            {
                await contextObject.EnsureValue();

                if (EscapeValue && !context.Options.DisableContentEscaping)
                {
                    outputStream.Write(HtmlEncodeString(await contextObject.RenderToString()));
                }
                else
                {
                    outputStream.Write(await contextObject.RenderToString());
                }
            }

            return(Children.WithScope(contextObject));
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        public async Task <ContextObject> GetValue(ContextObject context, ScopeData scopeData)
        {
            await Task.CompletedTask;

            return(await context.GetContextForPath(Value, scopeData));
        }