Skip to content

Commit

Permalink
Merge pull request #65 from Nexus-Mods/nullable-value-attributes
Browse files Browse the repository at this point in the history
Fix nullable value types in the template
  • Loading branch information
halgari authored Jun 11, 2024
2 parents f7dda21 + eea9ec9 commit c9fe3c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/NexusMods.MnemonicDB.SourceGenerator/AnalyzedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public string ContextualName
public bool IsIndexed => Markers.Contains("IsIndexed");

Check warning on line 30 in src/NexusMods.MnemonicDB.SourceGenerator/AnalyzedAttribute.cs

View workflow job for this annotation

GitHub Actions / publish / build

Missing XML comment for publicly visible type or member 'AnalyzedAttribute.IsIndexed'
public bool IsOptional => Markers.Contains("IsOptional");

Check warning on line 31 in src/NexusMods.MnemonicDB.SourceGenerator/AnalyzedAttribute.cs

View workflow job for this annotation

GitHub Actions / publish / build

Missing XML comment for publicly visible type or member 'AnalyzedAttribute.IsOptional'

/// <summary>
/// True if this is a value type.
/// </summary>
public bool IsValueType => HighLevelType.IsValueType;

/// <summary>
/// The C# prefix for the attribute, for now this is just required or empty
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/NexusMods.MnemonicDB.SourceGenerator/Template.weave
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ public partial class {{= model.Name}} {
tx.Add(Id, {{= attr.FieldName}}, new __COMPARERS__.Null(), false);
}
{{else}}
{{if attr.IsOptional && !attr.IsReference}}
{{if attr.IsOptional && !attr.IsReference && !attr.IsValueType}}
if ({{= attr.ContextualName}} is not null) {
tx.Add(Id, {{= attr.FieldName}}, {{= attr.ContextualName}}!, false);
}
{{elif attr.IsOptional && attr.IsValueType && !attr.IsReference}}
if ({{= attr.ContextualName}}.HasValue) {
tx.Add(Id, {{= attr.FieldName}}, {{= attr.ContextualName}}.Value, false);
}
{{elif attr.IsOptional && attr.IsReference}}
if ({{= attr.ContextualName}}.Value != 0) {
tx.Add(Id, {{= attr.FieldName}}, {{= attr.ContextualName}}.Value, false);
Expand Down
1 change: 1 addition & 0 deletions tests/NexusMods.MnemonicDB.TestModel/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public partial class Mod : IModelDefinition
public static readonly BackReferenceAttribute<File> Files = new(File.Mod);
public static readonly MarkerAttribute Marked = new(Namespace, nameof(Marked)) { IsIndexed = true };
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 };
}

0 comments on commit c9fe3c2

Please sign in to comment.