-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a timestampwrapper config utility, which should make this type easier to use in a good way in config. Make it accept RFC3339-like datetimes, but also shortened. We require a timezone, which IMO is a good idea, to avoid ambiguity.
- Loading branch information
Showing
5 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using Cognite.Extractor.Common; | ||
using YamlDotNet.Core; | ||
using YamlDotNet.Core.Events; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Cognite.Extractor.Configuration | ||
{ | ||
/// <summary> | ||
/// Wrapper around a timestamp, for configuration objects. | ||
/// </summary> | ||
public class TimestampWrapper | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="value">Raw value</param> | ||
/// <exception cref="ConfigurationException">If the provided value is non-null, and an invalid timestamp</exception> | ||
public TimestampWrapper(string? value) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(value) && CogniteTime.ParseTimestampString(value) is null) | ||
{ | ||
throw new ConfigurationException($"Invalid timestamp {value}, must be on the form 'yyyy-MM-dd[THH:mm:ss]Z', or N[ms|s|m|h|d](-ago)"); | ||
} | ||
RawValue = value; | ||
} | ||
|
||
/// <summary> | ||
/// The raw value of the timestamp, written to during deserialization. | ||
/// </summary> | ||
public string? RawValue { get; } | ||
/// <summary> | ||
/// Get the current datetime value. | ||
/// </summary> | ||
/// <returns></returns> | ||
public DateTime? Get() | ||
{ | ||
if (RawValue == null) return null; | ||
// Should never fail, but we throw an error here to be safe. | ||
return CogniteTime.ParseTimestampString(RawValue) ?? throw new ConfigurationException("Invalid timestamp"); | ||
} | ||
} | ||
|
||
internal class TimestampWrapperConverter : IYamlTypeConverter | ||
{ | ||
public bool Accepts(Type type) | ||
{ | ||
return type == typeof(TimestampWrapper); | ||
} | ||
|
||
public object? ReadYaml(IParser parser, Type type) | ||
{ | ||
var scalar = parser.Consume<Scalar>(); | ||
return new TimestampWrapper(scalar.Value); | ||
} | ||
|
||
public void WriteYaml(IEmitter emitter, object? value, Type type) | ||
{ | ||
var it = value as TimestampWrapper; | ||
emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, it?.RawValue ?? "", ScalarStyle.DoubleQuoted, false, true)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.29.0 | ||
1.30.0 |