Пример #1
0
        public object let(TemplateScopeContext scope, object target, object scopeBindings) //from filter
        {
            var objs = target as IEnumerable;

            if (objs != null)
            {
                var scopedParams = scope.GetParamsWithItemBindingOnly(nameof(let), null, scopeBindings, out string itemBinding);

                var to = new List <ScopeVars>();
                var i  = 0;
                foreach (var item in objs)
                {
                    scope.ScopedParams[TemplateConstants.Index] = i++;
                    scope.ScopedParams[itemBinding]             = item;

                    // Copy over previous let bindings into new let bindings
                    var itemBindings = new ScopeVars();
                    if (item is object[] tuple)
                    {
                        foreach (var a in tuple)
                        {
                            if (a is IDictionary <string, object> aArgs)
                            {
                                foreach (var entry in aArgs)
                                {
                                    itemBindings[entry.Key] = entry.Value;
                                }
                            }
                        }
                    }

                    foreach (var entry in scopedParams)
                    {
                        var bindTo = entry.Key;
                        if (!(entry.Value is string bindToLiteral))
                        {
                            throw new NotSupportedException($"'{nameof(let)}' in '{scope.Page.VirtualPath}' expects a string Expression for its value but received '{entry.Value}' instead");
                        }

                        bindToLiteral.ToStringSegment().ParseNextToken(out object value, out JsBinding binding);
                        var bindValue = scope.Evaluate(value, binding);
                        scope.ScopedParams[bindTo] = bindValue;
                        itemBindings[bindTo]       = bindValue;
                    }
                    to.Add(itemBindings);
                }

                return(to);
            }
            if (target != null)
            {
                throw new NotSupportedException($"'{nameof(let)}' in '{scope.Page.VirtualPath}' requires an IEnumerable but received a '{target.GetType()?.Name}' instead");
            }

            return(null);
        }
Пример #2
0
        public object let(TemplateScopeContext scope, object target, object scopeBindings) //from filter
        {
            if (target is IEnumerable objs)
            {
                string itemBinding;
                Dictionary <string, object> scopedParams = null;

                var arrowExpr = scopeBindings as JsArrowFunctionExpression;
                if (arrowExpr != null)
                {
                    itemBinding = arrowExpr.Params[0].Name;
                }
                else
                {
                    scopedParams = scope.GetParamsWithItemBindingOnly(nameof(@let), null, scopeBindings, out itemBinding);
                }

                var to = new List <ScopeVars>();
                var i  = 0;
                foreach (var item in objs)
                {
                    scope.ScopedParams[TemplateConstants.Index] = i++;
                    scope.ScopedParams[itemBinding]             = item;

                    // Copy over previous let bindings into new let bindings
                    var itemBindings = new ScopeVars();
                    if (item is object[] tuple)
                    {
                        foreach (var a in tuple)
                        {
                            if (a is IDictionary <string, object> aArgs)
                            {
                                foreach (var entry in aArgs)
                                {
                                    itemBindings[entry.Key] = entry.Value;
                                }
                            }
                        }
                    }

                    if (arrowExpr != null)
                    {
                        var value = arrowExpr.Body.Evaluate(scope);
                        if (value is Dictionary <string, object> bindingVars)
                        {
                            foreach (var bindingVar in bindingVars)
                            {
                                itemBindings[bindingVar.Key] = bindingVar.Value;
                            }
                        }
                    }
                    else
                    {
                        foreach (var entry in scopedParams)
                        {
                            var bindTo = entry.Key;
                            if (!(entry.Value is string bindToLiteral))
                            {
                                throw new NotSupportedException($"'{nameof(let)}' in '{scope.Page.VirtualPath}' expects a string Expression for its value but received '{entry.Value}' instead");
                            }

                            bindToLiteral.ParseJsExpression(out JsToken token);

                            var bindValue = token.Evaluate(scope);
                            scope.ScopedParams[bindTo] = bindValue;
                            itemBindings[bindTo]       = bindValue;
                        }
                    }

                    to.Add(itemBindings);
                }

                return(to);
            }
            if (target != null)
            {
                throw new NotSupportedException($"'{nameof(let)}' in '{scope.Page.VirtualPath}' requires an IEnumerable but received a '{target.GetType()?.Name}' instead");
            }

            return(null);
        }