Skip to content

Commit

Permalink
Fix up tests and compile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jun 4, 2024
1 parent 6830e75 commit 76460d1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ IEnumerator IEnumerable.GetEnumerator()
/// <summary>
/// Creates a view of these Ids that auto-casts every Id into a model of the given model type
/// </summary>
Entities<TModel> AsModels<TModel>(IDb db)
public Entities<TModel> AsModels<TModel>(IDb db)
where TModel : IReadOnlyModel<TModel>
{
return new Entities<TModel>(this, db);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using NexusMods.MnemonicDB.Abstractions.Models;

namespace NexusMods.MnemonicDB.Abstractions.IndexSegments;

/// <summary>
/// A wrapper around Values that auto-creates the given ReadModel on-the-fly
/// </summary>
public readonly struct ValueEntities<TModel> : IReadOnlyCollection<TModel>
where TModel : IReadOnlyModel<TModel>
{
private readonly Values<EntityId, ulong> _values;

/// <summary>
/// The database the models are read from
/// </summary>
private IDb Db { get; }

/// <summary>
/// Creates a new ValueEntities, from the given values, database, and entity id
/// </summary>
public ValueEntities(Values<EntityId, ulong> values, IDb db)
{
_values = values;
Db = db;
}

/// <inheritdoc />
public IEnumerator<TModel> GetEnumerator()
{
foreach (var value in _values)
{
yield return TModel.Create(Db, value);
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}


/// <inheritdoc />
public int Count => _values.Count;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ public static Entities<Values<EntityId, ulong>, TModel> As<TModel>(this Values<E
{
return new Entities<Values<EntityId, ulong>, TModel>(ids, db);
}

/// <summary>
/// Returns a view of the values as models whose ids are the given id
/// </summary>
public static ValueEntities<TModel> AsModels<TModel>(this Values<EntityId, ulong> values, IDb db)
where TModel : IReadOnlyModel<TModel>
{
return new ValueEntities<TModel>(values, db);
}
}
4 changes: 2 additions & 2 deletions src/NexusMods.MnemonicDB.SourceGenerator/Template.weave
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public partial class {{= model.Name}} {
{{each attr in model.Attributes}}
{{if attr.IsCollection && attr.IsReference && !attr.IsMarker}}
public __SEGMENTS__.Values<__ABSTRACTIONS__.EntityId, ulong> {{= attr.ContextualName}} => {{= attr.FieldName}}.Get(this);
public IEnumerable<{{= attr.ReferenceType.ToDisplayString()}}.ReadOnly> {{= attr.Name}} => {{= attr.FieldName}}.Get(this).Select(id => new {{= attr.ReferenceType.ToDisplayString()}}.ReadOnly(Db, id));
public __SEGMENTS__.ValueEntities<{{= attr.ReferenceType.ToDisplayString()}}.ReadOnly> {{= attr.Name}} => __SEGMENTS__.ValuesExtensions.AsModels<{{= attr.ReferenceType.ToDisplayString()}}.ReadOnly>({{= attr.FieldName}}.Get(this), Db);
{{elif attr.IsMarker}}
public bool Is{{= attr.ContextualName}} => {{= attr.FieldName}}.Contains(this);
{{else}}
Expand All @@ -281,7 +281,7 @@ public partial class {{= model.Name}} {

{{each backref in model.BackReferences}}

public __SEGMENTS__.Entities<{{= backref.OtherModel.ToDisplayString()}}.ReadOnly> {{= backref.Name}} => Db.GetBackRefs({{= backref.OtherAttribute.ToDisplayString()}}, this.Id).AsModels<{{= backRef.OtherModel.ToDisplayString()}}.ReadOnly>(Db);
public __SEGMENTS__.Entities<{{= backref.OtherModel.ToDisplayString()}}.ReadOnly> {{= backref.Name}} => Db.GetBackRefs({{= backref.OtherAttribute.ToDisplayString()}}, this.Id).AsModels<{{= backref.OtherModel.ToDisplayString()}}.ReadOnly>(Db);
{{/each}}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public async Task CanGetDatomsFromEntity()
mod.Contains(Mod.Source).Should().BeTrue();
mod.Contains(Loadout.Name).Should().BeFalse();

mod.ToString().Should().Be("Mod<200000000000002>");
mod.ToString().Should().Be("Mod<EId:200000000000002>");

await VerifyTable(mod);
}
Expand Down

0 comments on commit 76460d1

Please sign in to comment.