public static async ValueTask <FluidValue> SortNatural(FluidValue input, FilterArguments arguments, TemplateContext context) { if (arguments.Count > 0) { var member = arguments.At(0).ToStringValue(); var values = new List <KeyValuePair <FluidValue, object> >(); foreach (var item in input.Enumerate()) { values.Add(new KeyValuePair <FluidValue, object>(item, (await item.GetValueAsync(member, context)).ToObjectValue())); } var orderedValues = values.OrderBy(x => x.Value).Select(x => x.Key).ToList(); return(new ArrayValue(orderedValues)); } else { return(new ArrayValue(input.Enumerate().OrderBy(x => { return x.ToStringValue(); }, StringComparer.OrdinalIgnoreCase))); } }
public static FluidValue Concat(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } if (arguments.At(0).Type != FluidValues.Array) { return(input); } var concat = new List <FluidValue>(); foreach (var item in input.Enumerate()) { concat.Add(item); } foreach (var item in arguments.At(0).Enumerate()) { concat.Add(item); } return(new ArrayValue(concat)); }
// https://github.com/Shopify/liquid/commit/842986a9721de11e71387732be51951285225977 public static FluidValue Where(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } // First argument is the property name to match var member = arguments.At(0).ToStringValue(); // Second argument is the value to match, or 'true' if none is defined var targetValue = arguments.At(1).Or(BooleanValue.True).ToObjectValue(); var list = new List <FluidValue>(); foreach (var item in input.Enumerate()) { var itemValue = item.GetValueAsync(member, context).GetAwaiter().GetResult(); if (itemValue.ToObjectValue().Equals(targetValue)) { list.Add(item); } } return(new ArrayValue(list)); }
public static ValueTask <FluidValue> Reverse(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type == FluidValues.Array) { return(new ArrayValue(input.Enumerate(context).Reverse())); } else if (input.Type == FluidValues.String) { var value = input.ToStringValue(); if (String.IsNullOrEmpty(value)) { return(StringValue.Empty); } else { var valueAsArray = value.ToCharArray(); Array.Reverse(valueAsArray); return(new ArrayValue(valueAsArray.Select(e => new StringValue(e.ToString())))); } } else { return(input); } }
public static FluidValue Json(FluidValue input, FilterArguments arguments, TemplateContext context) { var options = new JsonSerializerOptions { WriteIndented = arguments.At(0).ToBooleanValue() }; switch (input.Type) { case FluidValues.Array: return(new StringValue(JsonSerializer.Serialize(input.Enumerate().Select(o => o.ToObjectValue()), options))); case FluidValues.Boolean: return(new StringValue(JsonSerializer.Serialize(input.ToBooleanValue(), options))); case FluidValues.Nil: return(StringValue.Create("null")); case FluidValues.Number: return(new StringValue(JsonSerializer.Serialize(input.ToNumberValue(), options))); case FluidValues.DateTime: case FluidValues.Dictionary: case FluidValues.Object: return(new StringValue(JsonSerializer.Serialize(input.ToObjectValue(), options))); case FluidValues.String: return(new StringValue(JsonSerializer.Serialize(input.ToStringValue(), options))); } throw new NotSupportedException("Unrecognized FluidValue"); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx) { if (!ctx.AmbientValues.TryGetValue("Services", out var services)) { throw new ArgumentException("Services missing while invoking 'localization_set'"); } var innoFieldsService = ((IServiceProvider)services).GetRequiredService <IContentLocalizationManager>(); var locale = arguments.At(0).ToStringValue(); if (arguments.At(0).IsNil()) { locale = ctx.CultureInfo.Name; } if (input.Type == FluidValues.Array) { // List of content item ids var localizationSets = input.Enumerate().Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await innoFieldsService.GetItemsForSetsAsync(localizationSets, locale))); } else { var localizationSet = input.ToStringValue(); return(FluidValue.Create(await innoFieldsService.GetContentItemAsync(localizationSet, locale))); } }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx) { if (!ctx.AmbientValues.TryGetValue("Services", out var services)) { throw new ArgumentException("Services missing while invoking 'full_text_aspect'"); } var serviceProvider = (IServiceProvider)services; var contentManager = serviceProvider.GetRequiredService <IContentManager>(); var fullTextRecursionHelper = serviceProvider.GetRequiredService <IContentItemRecursionHelper <FullTextFilter> >(); if (input.Type == FluidValues.Array) { var contentItems = new List <ContentItem>(); foreach (var objValue in input.Enumerate()) { var contentItem = GetContentItem(objValue); if (contentItem != null) { if (!fullTextRecursionHelper.IsRecursive(contentItem)) { contentItems.Add(contentItem); } } } if (!contentItems.Any()) { return(NilValue.Instance); } var aspects = new List <FullTextAspect>(); foreach (var contentItem in contentItems) { aspects.Add(await contentManager.PopulateAspectAsync <FullTextAspect>(contentItem)); } // When returning segments seperate them so multiple segments are indexed individually. return(new ArrayValue(aspects.SelectMany(x => x.Segments).Where(x => !String.IsNullOrEmpty(x)).Select(x => new StringValue(x + ' ')))); } else { var contentItem = GetContentItem(input); if (contentItem == null || fullTextRecursionHelper.IsRecursive(contentItem)) { return(NilValue.Instance); } var fullTextAspect = await contentManager.PopulateAspectAsync <FullTextAspect>(contentItem); // Remove empty strings as display text is often unused in contained content items. return(new ArrayValue(fullTextAspect.Segments.Where(x => !String.IsNullOrEmpty(x)).Select(x => new StringValue(x)))); } }
public static FluidValue First(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } return(input.Enumerate().First()); }
public static FluidValue Sort(FluidValue input, FilterArguments arguments, TemplateContext context) { var member = arguments.At(0).ToStringValue(); return(new ArrayValue(input.Enumerate().OrderBy(x => { return x.GetValueAsync(member, context).GetAwaiter().GetResult().ToObjectValue(); }).ToArray())); }
public static FluidValue Join(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } return(new StringValue(String.Join(arguments.At(0).ToStringValue(), input.Enumerate().Select(x => x.ToStringValue()).ToArray()))); }
public static FluidValue Reverse(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } return(new ArrayValue(input.Enumerate().Reverse())); }
public static FluidValue Last(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } return(input.Enumerate().LastOrDefault() ?? NilValue.Instance); }
public static FluidValue SortNatural(FluidValue input, FilterArguments arguments, TemplateContext context) { if (arguments.Count > 0) { var member = arguments.At(0).ToStringValue(); return(new ArrayValue(input.Enumerate().OrderBy(x => { return x.GetValueAsync(member, context).GetAwaiter().GetResult().ToObjectValue(); }).ToArray())); } else { return(new ArrayValue(input.Enumerate().OrderBy(x => { return x.ToStringValue(); }, StringComparer.OrdinalIgnoreCase).ToArray())); } }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context) { var locations = new List <string>(); foreach (var value in input.Enumerate(context)) { locations.Add((await value.GetValueAsync("LeverPostingPart.Location", context)).ToStringValue()); } return(new StringValue(StringUtils.GetOptions(locations.Distinct().OrderBy(x => x).ToList(), _httpContextAccessor.HttpContext.Request.Query["location"].FirstOrDefault()))); }
private string?InputToString(FluidValue input) { return(input.Type switch { FluidValues.Array => JsonConvert.SerializeObject(input.Enumerate().Select(o => o.ToObjectValue())), FluidValues.Boolean => input.ToBooleanValue().ToString(), FluidValues.Nil => null, FluidValues.Number => input.ToNumberValue().ToString(CultureInfo.InvariantCulture), FluidValues.DateTime or FluidValues.Dictionary or FluidValues.Object => JsonConvert.SerializeObject(input.ToObjectValue()), FluidValues.String => input.ToStringValue(), _ => throw new ArgumentOutOfRangeException(nameof(input), "Unrecognized FluidValue") });
public static ValueTask <FluidValue> Join(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } var separator = arguments.At(0).ToStringValue(); var values = input.Enumerate(context).Select(x => x.ToStringValue()); var joined = string.Join(separator, values); return(new StringValue(joined)); }
public static ValueTask <FluidValue> Compact(FluidValue input, FilterArguments arguments, TemplateContext context) { var compacted = new List <FluidValue>(); foreach (var value in input.Enumerate()) { if (!value.IsNil()) { compacted.Add(value); } } return(new ArrayValue(compacted)); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) { if (input.Type == FluidValues.Array) { // List of user ids var userIds = input.Enumerate(ctx).Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await _session.Query <User, UserIndex>(x => x.UserId.IsIn(userIds)).ListAsync(), ctx.Options)); } var userId = input.ToStringValue(); return(FluidValue.Create(await _session.Query <User, UserIndex>(x => x.UserId == userId).FirstOrDefaultAsync(), ctx.Options)); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext context) { var commitments = new List <string>(); foreach (var value in input.Enumerate(context)) { commitments.AddRange((await value.GetValueAsync("LeverPostingPart.Commitment", context)) .ToStringValue().Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Trim()).ToList() ); } return(new StringValue(StringUtils.GetOptions(commitments.Distinct().OrderBy(x => x).ToList(), _httpContextAccessor.HttpContext.Request.Query["commitment"].FirstOrDefault()))); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) { if (input.Type == FluidValues.Array) { // List of content item ids var contentItemIds = input.Enumerate(ctx).Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await _contentManager.GetAsync(contentItemIds), ctx.Options)); } else { var contentItemId = input.ToStringValue(); return(FluidValue.Create(await _contentManager.GetAsync(contentItemId), ctx.Options)); } }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) { if (input.Type == FluidValues.Array) { var contentItems = new List <ContentItem>(); foreach (var objValue in input.Enumerate()) { var contentItem = GetContentItem(objValue); if (contentItem != null) { if (!_fullTextRecursionHelper.IsRecursive(contentItem)) { contentItems.Add(contentItem); } } } if (!contentItems.Any()) { return(NilValue.Instance); } var aspects = new List <FullTextAspect>(); foreach (var contentItem in contentItems) { aspects.Add(await _contentManager.PopulateAspectAsync <FullTextAspect>(contentItem)); } // When returning segments seperate them so multiple segments are indexed individually. return(new ArrayValue(aspects.SelectMany(x => x.Segments).Where(x => !String.IsNullOrEmpty(x)).Select(x => new StringValue(x + ' ')))); } else { var contentItem = GetContentItem(input); if (contentItem == null || _fullTextRecursionHelper.IsRecursive(contentItem)) { return(NilValue.Instance); } var fullTextAspect = await _contentManager.PopulateAspectAsync <FullTextAspect>(contentItem); // Remove empty strings as display text is often unused in contained content items. return(new ArrayValue(fullTextAspect.Segments.Where(x => !String.IsNullOrEmpty(x)).Select(x => new StringValue(x)))); } }
public static FluidValue Map(FluidValue input, FilterArguments arguments, TemplateContext context) { if (input.Type != FluidValues.Array) { return(input); } var member = arguments.At(0).ToStringValue(); var list = new List <FluidValue>(); foreach (var item in input.Enumerate()) { list.Add(item.GetValueAsync(member, context).GetAwaiter().GetResult()); } return(new ArrayValue(list)); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) { var session = ctx.Services.GetRequiredService <ISession>(); var roleManager = ctx.Services.GetRequiredService <RoleManager <IRole> >(); if (input.Type == FluidValues.Array) { // List of roles passed var userRoles = input.Enumerate().Select(x => roleManager.NormalizeKey(x.ToStringValue())).ToArray(); var query = session.Query <User>(); query.With <UserByRoleNameIndex>(x => x.RoleName.IsIn(userRoles)); return(FluidValue.Create(await query.ListAsync(), ctx.Options)); } else { var normalizedRoleName = roleManager.NormalizeKey(input.ToStringValue()); return(FluidValue.Create(await session.Query <User, UserByRoleNameIndex>(u => u.RoleName == normalizedRoleName).ListAsync(), ctx.Options)); } }
public static FluidValue Sort(FluidValue input, FilterArguments arguments, TemplateContext context) { var member = arguments.At(0).ToStringValue(); return(new ArrayValue(input.Enumerate().OrderBy(x => { if (member.Contains(".")) { foreach (var prop in member.Split('.')) { x = x.GetValue(prop, context); } return x.ToObjectValue(); } else { return x.GetValue(member, context).ToObjectValue(); } }).ToArray())); }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx) { if (!ctx.AmbientValues.TryGetValue("Services", out var services)) { throw new ArgumentException("Services missing while invoking 'user'"); } var session = ((IServiceProvider)services).GetRequiredService <ISession>(); if (input.Type == FluidValues.Array) { // List of user ids var userIds = input.Enumerate().Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await(session.Query <User, UserIndex>(x => x.UserId.IsIn(userIds)).ListAsync()))); } else { var userId = input.ToStringValue(); return(FluidValue.Create(await session.Query <User, UserIndex>(x => x.UserId == userId).FirstOrDefaultAsync())); } }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, TemplateContext ctx) { if (!ctx.AmbientValues.TryGetValue("Services", out var services)) { throw new ArgumentException("Services missing while invoking 'content_item_id'"); } var contentManager = ((IServiceProvider)services).GetRequiredService <IContentManager>(); if (input.Type == FluidValues.Array) { // List of content item ids var contentItemIds = input.Enumerate().Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await contentManager.GetAsync(contentItemIds))); } else { var contentItemId = input.ToStringValue(); return(FluidValue.Create(await contentManager.GetAsync(contentItemId))); } }
public async ValueTask <FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx) { var locale = arguments.At(0).ToStringValue(); if (arguments.At(0).IsNil()) { locale = ctx.CultureInfo.Name; } if (input.Type == FluidValues.Array) { // List of content item ids var localizationSets = input.Enumerate().Select(x => x.ToStringValue()).ToArray(); return(FluidValue.Create(await _contentLocalizationManager.GetItemsForSetsAsync(localizationSets, locale), ctx.Options)); } else { var localizationSet = input.ToStringValue(); return(FluidValue.Create(await _contentLocalizationManager.GetContentItemAsync(localizationSet, locale), ctx.Options)); } }
public static ValueTask <FluidValue> Json(FluidValue input, FilterArguments arguments, TemplateContext context) { var formatting = Formatting.None; if (arguments.At(0).ToBooleanValue()) { formatting = Formatting.Indented; } switch (input.Type) { case FluidValues.Array: return(new ValueTask <FluidValue>(new StringValue(JsonConvert.SerializeObject(input.Enumerate().Select(o => o.ToObjectValue()), formatting)))); case FluidValues.Boolean: return(new ValueTask <FluidValue>(new StringValue(JsonConvert.SerializeObject(input.ToBooleanValue(), formatting)))); case FluidValues.Nil: return(new ValueTask <FluidValue>(StringValue.Create("null"))); case FluidValues.Number: return(new ValueTask <FluidValue>(new StringValue(JsonConvert.SerializeObject(input.ToNumberValue(), formatting)))); case FluidValues.DateTime: case FluidValues.Dictionary: case FluidValues.Object: return(new ValueTask <FluidValue>(new StringValue(JsonConvert.SerializeObject(input.ToObjectValue(), formatting)))); case FluidValues.String: var stringValue = input.ToStringValue(); if (string.IsNullOrWhiteSpace(stringValue)) { return(new ValueTask <FluidValue>(input)); } return(new ValueTask <FluidValue>(new StringValue(JsonConvert.SerializeObject(stringValue, formatting)))); } throw new NotSupportedException("Unrecognized FluidValue"); }
public static FluidValue Uniq(FluidValue input, FilterArguments arguments, TemplateContext context) { return(new ArrayValue(input.Enumerate().Distinct().ToArray())); }
private static async ValueTask WriteJson(Utf8JsonWriter writer, FluidValue input, TemplateContext ctx) { switch (input.Type) { case FluidValues.Array: writer.WriteStartArray(); foreach (var item in input.Enumerate()) { await WriteJson(writer, item, ctx); } writer.WriteEndArray(); break; case FluidValues.Boolean: writer.WriteBooleanValue(input.ToBooleanValue()); break; case FluidValues.Nil: writer.WriteNullValue(); break; case FluidValues.Number: writer.WriteNumberValue(input.ToNumberValue()); break; case FluidValues.Dictionary: if (input.ToObjectValue() is IFluidIndexable dic) { writer.WriteStartObject(); foreach (var key in dic.Keys) { writer.WritePropertyName(key); if (dic.TryGetValue(key, out var value)) { await WriteJson(writer, value, ctx); } else { await WriteJson(writer, NilValue.Instance, ctx); } } writer.WriteEndObject(); } break; case FluidValues.Object: var obj = input.ToObjectValue(); if (obj != null) { writer.WriteStartObject(); var type = obj.GetType(); var properties = type.GetProperties(); var strategy = ctx.Options.MemberAccessStrategy; var conv = strategy.MemberNameStrategy; foreach (var property in properties) { var name = conv(property); var access = strategy.GetAccessor(type, name); if (access == null) { continue; } object value; if (access is IAsyncMemberAccessor asyncMemberAccessor) { value = await asyncMemberAccessor.GetAsync(obj, name, ctx); } else { value = access.Get(obj, name, ctx); } var fluidValue = FluidValue.Create(value, ctx.Options); writer.WritePropertyName(name); await WriteJson(writer, fluidValue, ctx); } writer.WriteEndObject(); } break; case FluidValues.DateTime: var objValue = input.ToObjectValue(); if (objValue is DateTime dateTime) { writer.WriteStringValue(dateTime); } else if (objValue is DateTimeOffset dateTimeOffset) { writer.WriteStringValue(dateTimeOffset); } else { writer.WriteStringValue(Convert.ToDateTime(objValue)); } break; case FluidValues.String: writer.WriteStringValue(input.ToStringValue()); break; default: throw new NotSupportedException("Unrecognized FluidValue"); } }