예제 #1
0
        public static async ValueTask <Completion> WriteToAsync(ValueTuple <Expression, List <FilterArgument> > arguments, TextWriter writer, TextEncoder encoder, TemplateContext context)
        {
            var objectValue = (await arguments.Item1.EvaluateAsync(context)).ToObjectValue();

            if (objectValue is IShape shape)
            {
                var expressions = new NamedExpressionList(arguments.Item2);

                var metadata = shape.Metadata;

                if (expressions.HasNamed("cache_id"))
                {
                    metadata.Cache((await expressions["cache_id"].EvaluateAsync(context)).ToStringValue());
                }

                if (expressions.HasNamed("cache_context"))
                {
                    var contexts = (await expressions["cache_context"].EvaluateAsync(context)).ToStringValue().Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                    metadata.Cache().AddContext(contexts);
                }

                if (expressions.HasNamed("cache_tag"))
                {
                    var tags = (await expressions["cache_tag"].EvaluateAsync(context)).ToStringValue().Split(Separators, StringSplitOptions.RemoveEmptyEntries);
                    metadata.Cache().AddTag(tags);
                }

                if (expressions.HasNamed("cache_fixed_duration"))
                {
                    if (TimeSpan.TryParse((await expressions["cache_fixed_duration"].EvaluateAsync(context)).ToStringValue(), out var timespan))
                    {
                        metadata.Cache().WithExpiryAfter(timespan);
                    }
                }

                if (expressions.HasNamed("cache_sliding_duration"))
                {
                    if (TimeSpan.TryParse((await expressions["cache_sliding_duration"].EvaluateAsync(context)).ToStringValue(), out var timespan))
                    {
                        metadata.Cache().WithExpirySliding(timespan);
                    }
                }
            }

            return(Completion.Normal);
        }
예제 #2
0
        public static async ValueTask <Completion> WriteToAsync(ValueTuple <Expression, List <FilterArgument> > arguments, TextWriter writer, TextEncoder encoder, TemplateContext context)
        {
            var objectValue = (await arguments.Item1.EvaluateAsync(context)).ToObjectValue() as dynamic;

            if (objectValue is Shape shape)
            {
                if (shape.Metadata.Type == "PagerSlim")
                {
                    foreach (var argument in arguments.Item2)
                    {
                        var propertyName = argument.Name.ToPascalCaseUnderscore();

                        if (_properties.Contains(propertyName))
                        {
                            objectValue[propertyName] = (await argument.Expression.EvaluateAsync(context)).ToStringValue();
                        }
                    }
                }

                var expressions = new NamedExpressionList(arguments.Item2);

                if (shape.Metadata.Type == "PagerSlim" || shape.Metadata.Type == "Pager")
                {
                    if (expressions.HasNamed("item_classes"))
                    {
                        var itemClasses = await expressions["item_classes"].EvaluateAsync(context);

                        if (itemClasses.Type == FluidValues.String)
                        {
                            var values = itemClasses.ToStringValue().Split(' ', StringSplitOptions.RemoveEmptyEntries);

                            foreach (var value in values)
                            {
                                objectValue.ItemClasses.Add(value);
                            }
                        }
                        else if (itemClasses.Type == FluidValues.Array)
                        {
                            foreach (var value in itemClasses.Enumerate())
                            {
                                objectValue.ItemClasses.Add(value.ToStringValue());
                            }
                        }
                    }

                    if (expressions.HasNamed("classes"))
                    {
                        var classes = await expressions["classes"].EvaluateAsync(context);

                        if (classes.Type == FluidValues.String)
                        {
                            var values = classes.ToStringValue().Split(' ', StringSplitOptions.RemoveEmptyEntries);

                            foreach (var value in values)
                            {
                                objectValue.Classes.Add(value);
                            }
                        }
                        else if (classes.Type == FluidValues.Array)
                        {
                            foreach (var value in classes.Enumerate())
                            {
                                objectValue.Classes.Add(value.ToStringValue());
                            }
                        }
                    }

                    if (expressions.HasNamed("attributes"))
                    {
                        var attributes = await expressions["attributes"].EvaluateAsync(context);

                        if (attributes.Type == FluidValues.String)
                        {
                            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(attributes.ToStringValue());
                            foreach (var value in values)
                            {
                                objectValue.Attributes.TryAdd(value.Key, value.Value);
                            }
                        }
                    }

                    if (expressions.HasNamed("item_attributes"))
                    {
                        var itemAttributes = await expressions["item_attributes"].EvaluateAsync(context);

                        if (itemAttributes.Type == FluidValues.String)
                        {
                            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(itemAttributes.ToStringValue());
                            foreach (var value in values)
                            {
                                objectValue.ItemAttributes.TryAdd(value.Key, value.Value);
                            }
                        }
                    }
                }
            }

            return(Completion.Normal);
        }