2021-09-07 05:15:45 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text.RegularExpressions;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using Gallium;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using NodaTime;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using Zio;
|
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
namespace Nitride.Temporal;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the instant in the file based on the path of the entity. This
|
|
|
|
/// defaults to a generous regular expression that handles most formats of
|
|
|
|
/// four digit year, a separator, two digit month, a separator, and a two
|
|
|
|
/// digit day of month.
|
|
|
|
/// </summary>
|
|
|
|
[WithProperties]
|
|
|
|
public partial class SetInstantFromPath : OperationBase
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
private readonly Timekeeper clock;
|
|
|
|
|
|
|
|
private readonly IValidator<SetInstantFromPath> validator;
|
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
public SetInstantFromPath(
|
|
|
|
IValidator<SetInstantFromPath> validator,
|
|
|
|
Timekeeper clock)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
|
|
|
this.validator = validator;
|
|
|
|
this.clock = clock;
|
|
|
|
this.PathRegex = new Regex(@"(?<year>\d{4})[/-](?<month>\d{2})[/-](?<day>\d{2})");
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
/// <summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
/// Gets or sets a regular expression that has three named expressions:
|
|
|
|
/// year, month, and day.
|
2021-09-07 05:15:45 +00:00
|
|
|
/// </summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
public Regex? PathRegex { get; set; }
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override IEnumerable<Entity> Run(IEnumerable<Entity> input)
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
this.validator.ValidateAndThrow(this);
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
return input.SelectEntity<UPath>(this.Set);
|
2022-06-05 18:44:51 +00:00
|
|
|
}
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-07-09 04:52:10 +00:00
|
|
|
private Entity Set(
|
|
|
|
Entity entity,
|
|
|
|
UPath path)
|
2022-06-05 18:44:51 +00:00
|
|
|
{
|
|
|
|
// See if the path matches the given expression.
|
|
|
|
Match match = this.PathRegex!.Match(path.ToString());
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
if (!match.Success)
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
return entity;
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
// Create an Instant from this.
|
|
|
|
Instant instant = this.clock.CreateInstant(
|
2022-07-09 04:52:10 +00:00
|
|
|
Convert.ToInt32(
|
|
|
|
match.Groups["year"]
|
|
|
|
.Value),
|
|
|
|
Convert.ToInt32(
|
|
|
|
match.Groups["month"]
|
|
|
|
.Value),
|
|
|
|
Convert.ToInt32(
|
|
|
|
match.Groups["day"]
|
|
|
|
.Value));
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-06-05 18:44:51 +00:00
|
|
|
return entity.Set(instant);
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
|
|
|
}
|