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)); }
public static bool TryAccessMember(BindingContext context, object instance, ChainSegment chainSegment, out object value) { if (instance == null) { value = null; return(false); } chainSegment = ResolveMemberName(instance, chainSegment, context.Configuration); return(new ObjectAccessor(instance).TryGetValue(chainSegment, out value)); }
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); return(new ObjectAccessor(instance).TryGetValue(chainSegment, out value)); }
private static ChainSegment[] GetPathChain(Substring segmentString) { var insideEscapeBlock = false; var pathChainParts = Substring.Split(segmentString, '.', StringSplitOptions.RemoveEmptyEntries); var extendedEnumerator = ExtendedEnumerator <Substring> .Create(pathChainParts); if (!extendedEnumerator.Any && segmentString == ".") { return new[] { ChainSegment.This } } ; var chainSegments = new List <ChainSegment>(); while (extendedEnumerator.MoveNext()) { var next = extendedEnumerator.Current.Value; 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.ToArray()); }
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.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); }
private static bool TryResolveValue(bool isVariable, BindingContext context, ChainSegment chainSegment, object instance, out object value) { if (isVariable) { return(context.TryGetContextVariable(chainSegment, out value)); } if (chainSegment.IsThis) { value = context.Value; return(true); } if (context.TryGetVariable(chainSegment, out value) || TryAccessMember(context, instance, chainSegment, out value)) { return(true); } return(chainSegment.IsValue && context.TryGetContextVariable(chainSegment, out value)); }
private static bool ProcessSegment(BindingContext context, PathInfo pathInfo, bool isVariable, ChainSegment chainSegment, HashParameterDictionary hashParameters, ref object instance) { instance = ResolveValue(isVariable, context, instance, chainSegment); if (!(instance is UndefinedBindingResult undefined)) { return(true); } if (hashParameters == null || hashParameters.ContainsKey(chainSegment) || context.ParentContext == null) { if (context.Configuration.ThrowOnUnresolvedBindingExpression) { Throw.Undefined(pathInfo, undefined); } return(false); // return instance } instance = ResolveValue(isVariable, context.ParentContext, context.ParentContext.Value, chainSegment); if (!(instance is UndefinedBindingResult result)) { return(true); } if (context.Configuration.ThrowOnUnresolvedBindingExpression) { Throw.Undefined(pathInfo, result); } return(false); // return instance }
private static object ResolveValue(bool isVariable, BindingContext context, object instance, ChainSegment chainSegment) { object resolvedValue; if (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)); }