Example #1
0
        public static object ResolvePath(BindingContext context, PathInfo pathInfo)
        {
            if (!pathInfo.HasValue)
            {
                return(null);
            }
            if (pathInfo.IsPureThis)
            {
                return(context.Value);
            }

            var instance = context.Value;
            var throwOnUnresolvedBindingExpression = context !.Configuration.ThrowOnUnresolvedBindingExpression;

            var segments = pathInfo.Segments;

            for (var segmentIndex = 0; segmentIndex < segments.Length; segmentIndex++)
            {
                var segment = segments[segmentIndex];
                if (segment.IsThis)
                {
                    continue;
                }
                if (segment.IsParent)
                {
                    context = context !.ParentContext;
                    if (context == null)
                    {
                        instance = UndefinedBindingResult.Create("..");
                        goto undefined;
                    }

                    instance = context.Value;
                    throwOnUnresolvedBindingExpression = context.Configuration.ThrowOnUnresolvedBindingExpression;
                    continue;
                }

                var pathChain = segment.PathChain;
                if (!TryResolveValue(pathInfo.IsVariable, context, pathChain[0], instance, out instance))
                {
                    instance = UndefinedBindingResult.Create(pathChain[0]);
                    goto undefined;
                }

                for (var index = 1; index < pathChain.Length; index++)
                {
                    if (TryAccessMember(context, instance, pathChain[index], out instance))
                    {
                        continue;
                    }

                    instance = UndefinedBindingResult.Create(pathChain[index]);
                    goto undefined;
                }
            }

            return(instance);

undefined:
            if (throwOnUnresolvedBindingExpression)
            {
                Throw.Undefined(pathInfo, (UndefinedBindingResult)instance);
            }

            return(instance);
        }
Example #2
0
 public static void Undefined(PathInfo pathInfo, UndefinedBindingResult undefinedBindingResult) => throw new HandlebarsUndefinedBindingException(pathInfo, undefinedBindingResult);