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)); }
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)); }
internal UndefinedBindingResult GetUndefinedBindingResult(ICompiledHandlebarsConfiguration configuration) { if (_undefinedBindingResult != null) { return(_undefinedBindingResult); } lock (_lock) { return(_undefinedBindingResult ?? (_undefinedBindingResult = new UndefinedBindingResult(this, configuration))); } }
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.ObjectDescriptorProvider, out var descriptor) && descriptor.MemberAccessor.TryGetValue(instance, chainSegment, out value)); }
private object ResolveValue(BindingContext context, object instance, string segment) { object resolvedValue = new UndefinedBindingResult(segment, CompilationContext.Configuration); if (segment.StartsWith("@")) { var contextValue = context.GetContextVariable(segment.Substring(1)); if (contextValue != null) { resolvedValue = contextValue; } } else if (segment == "this" || segment == string.Empty) { resolvedValue = instance; } else { resolvedValue = AccessMember(instance, segment); } return(resolvedValue); }
public ObjectIteratorValues(BindingContext bindingContext) : this() { var configuration = bindingContext.Configuration; _data = bindingContext.ContextDataObject; _supportLastInObjectIterations = configuration.Compatibility.SupportLastInObjectIterations; _wellKnownVariables = bindingContext.WellKnownVariables; if (!_supportLastInObjectIterations) { var undefined = UndefinedBindingResult.Create(ChainSegment.Last); _data.AddOrReplace(ChainSegment.Last, undefined, out _wellKnownVariables[(int)ChainSegment.Last.WellKnownVariable]); } else { _data.AddOrReplace(ChainSegment.Last, BoxedValues.False, out _wellKnownVariables[(int)ChainSegment.Last.WellKnownVariable]); } _data.AddOrReplace(ChainSegment.Key, null, out _wellKnownVariables[(int)ChainSegment.Key.WellKnownVariable]); _data.AddOrReplace(ChainSegment.Value, null, out _wellKnownVariables[(int)ChainSegment.Value.WellKnownVariable]); _data.AddOrReplace(ChainSegment.First, BoxedValues.True, out _wellKnownVariables[(int)ChainSegment.First.WellKnownVariable]); _data.AddOrReplace(ChainSegment.Index, BoxedValues.Zero, out _wellKnownVariables[(int)ChainSegment.Index.WellKnownVariable]); }
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); }
public static void Undefined(PathInfo pathInfo, UndefinedBindingResult undefinedBindingResult) => throw new HandlebarsUndefinedBindingException(pathInfo, undefinedBindingResult);
/// <summary> /// In case it's an UndefinedBindingResult, just try to convert the value using Json. /// This logic adds functionality like parsing a list. /// </summary> /// <param name="undefinedBindingResult">The property value</param> /// <param name="parsedValue">The parsed value</param> /// <returns>true in case parsing is ok, else false</returns> private static bool TryParseUndefinedBindingResult(UndefinedBindingResult undefinedBindingResult, out List <object?>?parsedValue) { return(ArrayUtils.TryParseAsObjectList(undefinedBindingResult.Value, out parsedValue)); }