Skip to content

Commit

Permalink
Fix source generators for collection attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Aug 1, 2024
1 parent ab8a798 commit 91548a1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/NexusMods.MnemonicDB.SourceGenerator/Template.weave
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ public partial class {{= model.Name}} : __MODELS__.IModelFactory<{{= model.Name}
{{= attr.Comments}}
{{if attr.IsReference && !attr.IsCollection}}
public {{= attr.Prefix}} {{= attr.ReferenceType.ToDisplayString()}}Id {{= attr.ContextualName}} { get; set; }
{{elif attr.IsReference && attr.IsCollection}}
public {{= attr.Prefix}} {{= attr.HighLevelType.ToDisplayString()}}[] {{= attr.ContextualName}} { get; set; }
{{elif attr.IsCollection}}
public IEnumerable<{{= attr.HighLevelType.ToDisplayString()}}> {{= attr.ContextualName}} { get; set; } = [];
{{elif attr.IsReference}}
public {{= attr.Prefix}} {{= attr.HighLevelType.ToDisplayString()}} {{= attr.ContextualName}} { get; set; }
{{elif attr.IsMarker}}
public bool Is{{= attr.ContextualName}} { get; set; } = false;
{{else}}
Expand Down Expand Up @@ -356,6 +358,8 @@ public partial class {{= model.Name}} : __MODELS__.IModelFactory<{{= model.Name}
{{if attr.IsCollection && attr.IsReference && !attr.IsMarker}}
public __SEGMENTS__.Values<__ABSTRACTIONS__.EntityId, ulong> {{= attr.ContextualName}} => {{= attr.FieldName}}.Get(this);
public __SEGMENTS__.ValueEntities<{{= attr.ReferenceType.ToDisplayString()}}.ReadOnly> {{= attr.Name}} => __SEGMENTS__.ValuesExtensions.AsModels<{{= attr.ReferenceType.ToDisplayString()}}.ReadOnly>({{= attr.FieldName}}.Get(this), Db);
{{elif attr.IsCollection}}
public __SEGMENTS__.Values<{{=attr.HighLevelType.ToDisplayString()}}, {{=attr.LowLevelType.ToDisplayString()}}> {{= attr.ContextualName}} => {{= attr.FieldName}}.Get(this);
{{elif attr.IsMarker}}
public bool Is{{= attr.ContextualName}} => {{= attr.FieldName}}.Contains(this);
{{elif attr.IsReference}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NexusMods.MnemonicDB.Abstractions;
using NexusMods.MnemonicDB.Abstractions.Attributes;
using NexusMods.MnemonicDB.Abstractions.ElementComparers;

namespace NexusMods.MnemonicDB.TestModel.Attributes;

public class StringsAttribute(string ns, string name) : CollectionAttribute<string, string>(ValueTags.Utf8, ns, name)
{
protected override string ToLowLevel(string value)
{
return value;
}

protected override string FromLowLevel(string value, ValueTags tag, RegistryId registryId)
{
return value;
}
}
1 change: 1 addition & 0 deletions tests/NexusMods.MnemonicDB.TestModel/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public partial class Mod : IModelDefinition
public static readonly StringAttribute Description = new(Namespace, nameof(Description)) { IsOptional = true };
public static readonly HashAttribute OptionalHash = new(Namespace, nameof(OptionalHash)) { IsOptional = true };
public static readonly ReferenceAttribute<Mod> LoadAfter = new(Namespace, nameof(LoadAfter)) { IsOptional = true };
public static readonly StringsAttribute Tags = new(Namespace, nameof(Tags));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+ | 0100000000000003 | (0008) Timestamp | DateTime : 0 | 0100000000000003
+ | 0200000000000001 | (001C) Name | Parent A | 0100000000000003
+ | 0200000000000001 | (001D) Name | Parent B | 0100000000000003
+ | 0200000000000001 | (001E) Name | Test Child | 0100000000000003
+ | 0200000000000001 | (001D) Name | Parent A | 0100000000000003
+ | 0200000000000001 | (001E) Name | Parent B | 0100000000000003
+ | 0200000000000001 | (001F) Name | Test Child | 0100000000000003
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+ | 0100000000000003 | (0008) Timestamp | DateTime : 0 | 0100000000000003
+ | 0200000000000001 | (001C) Name | Parent A | 0100000000000003
+ | 0200000000000001 | (001D) Name | Parent B | 0100000000000003
+ | 0200000000000001 | (001E) Name | Test Child | 0100000000000003
+ | 0200000000000001 | (001D) Name | Parent A | 0100000000000003
+ | 0200000000000001 | (001E) Name | Parent B | 0100000000000003
+ | 0200000000000001 | (001F) Name | Test Child | 0100000000000003
25 changes: 25 additions & 0 deletions tests/NexusMods.MnemonicDB.Tests/DbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,4 +853,29 @@ public async Task CanGetAttributesThatRequireDI()

loadout.GamePath.Should().Be(path);
}

[Fact]
public async Task CollectionAttributesAreSupportedOnModels()
{
using var tx = Connection.BeginTransaction();

var loadout1 = new Loadout.New(tx)
{
Name = "Test Loadout"
};

var mod = new Mod.New(tx)
{
Name = "Test Mod",
Source = new Uri("http://test.com"),
LoadoutId = loadout1,
Tags = ["A", "B", "C"]
};

var result = await tx.Commit();

var modRO = result.Remap(mod);

modRO.Tags.Should().BeEquivalentTo(["A", "B", "C"]);
}
}

0 comments on commit 91548a1

Please sign in to comment.