Skip to content

Commit

Permalink
Add extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Nov 13, 2024
1 parent 2b30487 commit e847497
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;

namespace NexusMods.MnemonicDB.Abstractions.Attributes;

/// <summary>
/// An attribute that holds a float32 value.
/// </summary>
[PublicAPI]
public sealed class Float32Attribute(string ns, string name) : ScalarAttribute<float, float, Float32Serializer>(ns, name)
{
/// <inheritdoc />
protected override float ToLowLevel(float value) => value;

/// <inheritdoc />
protected override float FromLowLevel(float value, AttributeResolver resolver) => value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;

namespace NexusMods.MnemonicDB.Abstractions.Attributes;

/// <summary>
/// An attribute that holds a float64 value.
/// </summary>
[PublicAPI]
public sealed class Float64Attribute(string ns, string name) : ScalarAttribute<double, double, Float64Serializer>(ns, name)
{
/// <inheritdoc />
protected override double ToLowLevel(double value) => value;

/// <inheritdoc />
protected override double FromLowLevel(double value, AttributeResolver resolver) => value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;

namespace NexusMods.MnemonicDB.Abstractions.Attributes;

/// <summary>
/// An attribute that holds an int128 value.
/// </summary>
[PublicAPI]
public sealed class Int128Attribute(string ns, string name) : ScalarAttribute<Int128, Int128, Int128Serializer>(ns, name)
{
/// <inheritdoc />
protected override Int128 ToLowLevel(Int128 value) => value;

/// <inheritdoc />
protected override Int128 FromLowLevel(Int128 value, AttributeResolver resolver) => value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.ValueSerializers;

namespace NexusMods.MnemonicDB.Abstractions.Attributes;

/// <summary>
/// An attribute that holds an uint128 value.
/// </summary>
[PublicAPI]
public sealed class UInt128Attribute(string ns, string name) : ScalarAttribute<UInt128, UInt128, UInt128Serializer>(ns, name)
{
/// <inheritdoc />
protected override UInt128 ToLowLevel(UInt128 value) => value;

/// <inheritdoc />
protected override UInt128 FromLowLevel(UInt128 value, AttributeResolver resolver) => value;
}
27 changes: 27 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/EntityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using NexusMods.MnemonicDB.Abstractions.BuiltInEntities;
using NexusMods.MnemonicDB.Abstractions.Models;

namespace NexusMods.MnemonicDB.Abstractions;

/// <summary>
/// Extension methods for entities.
/// </summary>
[PublicAPI]
public static class EntityExtensions
{
/// <summary>
/// Gets the timestamp of the transaction that created the model.
/// </summary>
public static DateTimeOffset GetCreatedAt<T>(this T model, DateTimeOffset defaultValue = default)
where T : IReadOnlyModel<T>
{
if (model.Count == 0) return defaultValue;
var minTx = model.Min(m => m.T);

var tx = new Transaction.ReadOnly(model.Db, EntityId.From(minTx.Value));
return Transaction.Timestamp.Get(tx);
}
}
26 changes: 25 additions & 1 deletion src/NexusMods.MnemonicDB.Abstractions/EntityId.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using TransparentValueObjects;
using System;
using System.Globalization;
using JetBrains.Annotations;
using TransparentValueObjects;

namespace NexusMods.MnemonicDB.Abstractions;

/// <summary>
/// A unique identifier for an entity.
/// </summary>
[ValueObject<ulong>]
[PublicAPI]
public readonly partial struct EntityId : IAugmentWith<JsonAugment>
{
/// <summary>
Expand Down Expand Up @@ -38,4 +42,24 @@ public override string ToString()
{
return "EId:" + Value.ToString("X");
}

/// <summary>
/// Tries to parse a hex string as an entity ID.
/// </summary>
public static bool TryParseFromHex(ReadOnlySpan<char> input, out EntityId id)
{
const string prefix = "EId:";

if (input.StartsWith(prefix))
input = input[prefix.Length..];

if (ulong.TryParse(input, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
{
id = EntityId.From(result);
return true;
}

id = default;
return false;
}
}

0 comments on commit e847497

Please sign in to comment.