Beispiel #1
0
        private static object ResolveValue(BindingContext context, object instance, ChainSegment chainSegment)
        {
            object resolvedValue;

            if (chainSegment.IsVariable)
            {
                return(context.TryGetContextVariable(chainSegment, out resolvedValue)
                    ? resolvedValue
                    : UndefinedBindingResult.Create(chainSegment));
            }

            if (chainSegment.IsThis)
            {
                return(instance);
            }

            if (context.TryGetVariable(chainSegment, out resolvedValue) ||
                TryAccessMember(instance, chainSegment, context.Configuration, out resolvedValue))
            {
                return(resolvedValue);
            }

            if (chainSegment.IsValue && context.TryGetContextVariable(chainSegment, out resolvedValue))
            {
                return(resolvedValue);
            }

            return(UndefinedBindingResult.Create(chainSegment));
        }
Beispiel #2
0
        private static ChainSegment ResolveMemberName(object instance, ChainSegment memberName,
                                                      ICompiledHandlebarsConfiguration configuration)
        {
            var resolver = configuration.ExpressionNameResolver;

            if (resolver == null)
            {
                return(memberName);
            }

            return(resolver.ResolveExpressionName(instance, memberName.TrimmedValue));
        }
Beispiel #3
0
        internal static bool TryAccessMember(object instance, ChainSegment chainSegment, ICompiledHandlebarsConfiguration configuration, out object value)
        {
            if (instance == null)
            {
                value = UndefinedBindingResult.Create(chainSegment);
                return(false);
            }

            chainSegment = ResolveMemberName(instance, chainSegment, configuration);

            value = null;
            return(ObjectDescriptor.TryCreate(instance, configuration, out var descriptor) &&
                   descriptor.MemberAccessor.TryGetValue(instance, chainSegment, out value));
        }
Beispiel #4
0
        private static IReadOnlyList <ChainSegment> GetPathChain(Substring segmentString)
        {
            var insideEscapeBlock = false;
            var pathChainParts    = Substring.Split(segmentString, '.', StringSplitOptions.RemoveEmptyEntries);

            if (pathChainParts.Count == 0 && segmentString == ".")
            {
                return new[] { ChainSegment.Create("this") }
            }
            ;

            var chainSegments = new List <ChainSegment>();

            var count = pathChainParts.Count;

            for (int index = 0; index < count; index++)
            {
                var next = pathChainParts[index];
                if (insideEscapeBlock)
                {
                    if (next.EndsWith(']'))
                    {
                        insideEscapeBlock = false;
                    }

                    chainSegments[chainSegments.Count - 1] = ChainSegment.Create($"{chainSegments[chainSegments.Count - 1]}.{next.ToString()}");
                    continue;
                }

                if (next.StartsWith('['))
                {
                    insideEscapeBlock = true;
                }

                if (next.EndsWith(']'))
                {
                    insideEscapeBlock = false;
                }

                chainSegments.Add(ChainSegment.Create(next.ToString()));
            }

            return(chainSegments);
        }
Beispiel #5
0
        private static IEnumerable <ChainSegment> GetPathChain(string segmentString)
        {
            var insideEscapeBlock = false;
            var pathChainParts    = segmentString.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (pathChainParts.Length == 0 && segmentString == ".")
            {
                return new[] { ChainSegment.Create("this") }
            }
            ;

            var pathChain = pathChainParts.Aggregate(new List <ChainSegment>(), (list, next) =>
            {
                if (insideEscapeBlock)
                {
                    if (next.EndsWith("]"))
                    {
                        insideEscapeBlock = false;
                    }

                    list[list.Count - 1] = ChainSegment.Create($"{list[list.Count - 1]}.{next}");
                    return(list);
                }

                if (next.StartsWith("["))
                {
                    insideEscapeBlock = true;
                }

                if (next.EndsWith("]"))
                {
                    insideEscapeBlock = false;
                }

                list.Add(ChainSegment.Create(next));
                return(list);
            });

            return(pathChain);
        }