private static void IterateEnumerable(BindingContext context, BlockParamsValueProvider blockParamsValueProvider, IEnumerable target, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using (var iterator = IteratorValueProvider.Create()) { blockParamsValueProvider?.Configure(BlockParamsEnumerableConfiguration, iterator); iterator.Index = 0; var enumerable = new ExtendedEnumerable <object>(target); bool enumerated = false; foreach (var enumerableValue in enumerable) { enumerated = true; iterator.Value = enumerableValue.Value; iterator.First = enumerableValue.IsFirst; iterator.Last = enumerableValue.IsLast; using (var innerContext = context.CreateChildContext(iterator.Value)) { innerContext.RegisterValueProvider(iterator); template(context, context.TextWriter, innerContext); innerContext.UnregisterValueProvider(iterator); } } if (iterator.Index == 0 && !enumerated) { ifEmpty(context, context.TextWriter, context.Value); } } }
private static void IterateList(BindingContext context, BlockParamsValueProvider blockParamsValueProvider, IList target, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using (var iterator = IteratorValueProvider.Create()) { blockParamsValueProvider?.Configure(BlockParamsEnumerableConfiguration, iterator); var count = target.Count; for (iterator.Index = 0; iterator.Index < count; iterator.Index++) { iterator.Value = target[iterator.Index]; iterator.First = iterator.Index == 0; iterator.Last = iterator.Index == count - 1; using (var innerContext = context.CreateChildContext(iterator.Value)) { innerContext.RegisterValueProvider(iterator); template(context, context.TextWriter, innerContext); innerContext.UnregisterValueProvider(iterator); } } if (iterator.Index == 0) { ifEmpty(context, context.TextWriter, context.Value); } } }
private static void IterateObject(BindingContext context, ObjectDescriptor descriptor, ChainSegment[] blockParamsVariables, object target, IEnumerable properties, Type targetType, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using var innerContext = context.CreateChildContext(); var iterator = new ObjectIteratorValues(innerContext); var blockParams = new BlockParamsValues(innerContext, blockParamsVariables); blockParams.CreateProperty(0, out var _0); blockParams.CreateProperty(1, out var _1); var accessor = new MemberAccessor(target, descriptor); var enumerable = new ExtendedEnumerator <object>(properties.GetEnumerator()); var enumerated = false; object iteratorValue; ChainSegment iteratorKey; while (enumerable.MoveNext()) { enumerated = true; var enumerableValue = enumerable.Current; iteratorKey = ChainSegment.Create(enumerableValue.Value); iterator.Key = iteratorKey; iterator.Index = enumerableValue.Index; if (enumerableValue.Index == 1) { iterator.First = BoxedValues.False; } if (enumerableValue.IsLast) { iterator.Last = BoxedValues.True; } iteratorValue = accessor[iteratorKey]; iterator.Value = iteratorValue; innerContext.Value = iteratorValue; blockParams[_0] = iteratorValue; blockParams[_1] = iteratorKey; template(context, context.TextWriter, innerContext); } if (!enumerated) { ifEmpty(context, context.TextWriter, context.Value); } }
private static void IterateEnumerable(BindingContext context, ChainSegment[] blockParamsVariables, IEnumerable target, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using var innerContext = context.CreateChildContext(); var iterator = new IteratorValues(innerContext); var blockParams = new BlockParamsValues(innerContext, blockParamsVariables); blockParams.CreateProperty(0, out var _0); blockParams.CreateProperty(1, out var _1); var enumerator = new ExtendedEnumerator <object>(target.GetEnumerator()); var enumerated = false; object boxedIndex; object iteratorValue; while (enumerator.MoveNext()) { enumerated = true; var enumerableValue = enumerator.Current; if (enumerableValue.Index == 1) { iterator.First = BoxedValues.False; } if (enumerableValue.IsLast) { iterator.Last = BoxedValues.True; } boxedIndex = enumerableValue.Index; iteratorValue = enumerableValue.Value; iterator.Value = iteratorValue; iterator.Index = boxedIndex; blockParams[_0] = iteratorValue; blockParams[_1] = boxedIndex; innerContext.Value = iteratorValue; template(context, context.TextWriter, innerContext); } if (!enumerated) { ifEmpty(context, context.TextWriter, context.Value); } }
private static void IterateObject(BindingContext context, ObjectDescriptor descriptor, BlockParamsValueProvider blockParamsValueProvider, object target, IEnumerable <object> properties, Type targetType, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using (var iterator = ObjectEnumeratorValueProvider.Create(context.Configuration)) { blockParamsValueProvider?.Configure(BlockParamsObjectEnumeratorConfiguration, iterator); iterator.Index = 0; var accessor = descriptor.MemberAccessor; var enumerable = new ExtendedEnumerable <object>(properties); bool enumerated = false; foreach (var enumerableValue in enumerable) { enumerated = true; iterator.Key = enumerableValue.Value.ToString(); var key = iterator.Key.Intern(); iterator.Value = accessor.TryGetValue(target, targetType, key, out var value) ? value : null; iterator.First = enumerableValue.IsFirst; iterator.Last = enumerableValue.IsLast; iterator.Index = enumerableValue.Index; using (var innerContext = context.CreateChildContext(iterator.Value)) { innerContext.RegisterValueProvider(iterator); template(context, context.TextWriter, innerContext); innerContext.UnregisterValueProvider(iterator); } } if (iterator.Index == 0 && !enumerated) { ifEmpty(context, context.TextWriter, context.Value); } } }
private static void IterateObjectWithStaticProperties(BindingContext context, ObjectDescriptor descriptor, BlockParamsValueProvider blockParamsValueProvider, object target, IList properties, Type targetType, Action <BindingContext, TextWriter, object> template, Action <BindingContext, TextWriter, object> ifEmpty) { using (var iterator = ObjectEnumeratorValueProvider.Create(context.Configuration)) { blockParamsValueProvider?.Configure(BlockParamsObjectEnumeratorConfiguration, iterator); var accessor = descriptor.MemberAccessor; var count = properties.Count; for (iterator.Index = 0; iterator.Index < count; iterator.Index++) { iterator.Key = properties[iterator.Index].ToString(); iterator.Value = accessor.TryGetValue(target, targetType, iterator.Key, out var value) ? value : null; iterator.First = iterator.Index == 0; iterator.Last = iterator.Index == count - 1; using (var innerContext = context.CreateChildContext(iterator.Value)) { innerContext.RegisterValueProvider(iterator); template(context, context.TextWriter, innerContext); innerContext.UnregisterValueProvider(iterator); } } if (iterator.Index == 0) { ifEmpty(context, context.TextWriter, context.Value); } } }