generated from Nexus-Mods/NexusMods.App.Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from Nexus-Mods/dbattributes-fallback
Add fallback for cases when an attribute doesn't exist in DI
- Loading branch information
Showing
3 changed files
with
185 additions
and
1 deletion.
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
166 changes: 166 additions & 0 deletions
166
src/NexusMods.MnemonicDB.Abstractions/Attributes/UnknownAttribute.cs
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,166 @@ | ||
using System; | ||
using NexusMods.MnemonicDB.Abstractions.ElementComparers; | ||
|
||
namespace NexusMods.MnemonicDB.Abstractions.Attributes; | ||
|
||
/// <summary> | ||
/// An unknown attribute is an attribute that exists in the database but is not known to the application. This is | ||
/// either because the attribute was removed, or didn't show up in the DI system. | ||
/// </summary> | ||
/// <param name="dbAttribute"></param> | ||
/// <typeparam name="TLowLevel"></typeparam> | ||
public class UnknownAttribute<TLowLevel>(DbAttribute dbAttribute) : | ||
Attribute<TLowLevel, TLowLevel>(dbAttribute.LowLevelType, dbAttribute.UniqueId.Namespace, dbAttribute.UniqueId.Name) | ||
{ | ||
/// <inheritdoc /> | ||
protected override TLowLevel ToLowLevel(TLowLevel value) | ||
{ | ||
return value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(byte value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(ushort value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(uint value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(ulong value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(short value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(int value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(long value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(float value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(double value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override TLowLevel FromLowLevel(string value, ValueTags tags) | ||
{ | ||
if (tags != LowLevelType) | ||
{ | ||
throw new ArgumentException($"Cannot convert {tags} to {LowLevelType}"); | ||
} | ||
|
||
return (TLowLevel)(object)value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Helper class to create unknown attributes | ||
/// </summary> | ||
public static class UnknownAttribute | ||
{ | ||
/// <summary> | ||
/// Creates an unknown attribute from a database attribute | ||
/// </summary> | ||
public static IAttribute Create(DbAttribute dbAttribute) | ||
{ | ||
return dbAttribute.LowLevelType switch | ||
{ | ||
ValueTags.Null => new UnknownAttribute<Null>(dbAttribute), | ||
ValueTags.UInt8 => new UnknownAttribute<byte>(dbAttribute), | ||
ValueTags.UInt16 => new UnknownAttribute<ushort>(dbAttribute), | ||
ValueTags.UInt32 => new UnknownAttribute<uint>(dbAttribute), | ||
ValueTags.UInt64 => new UnknownAttribute<ulong>(dbAttribute), | ||
ValueTags.UInt128 => new UnknownAttribute<ulong>(dbAttribute), | ||
ValueTags.Int16 => new UnknownAttribute<short>(dbAttribute), | ||
ValueTags.Int32 => new UnknownAttribute<int>(dbAttribute), | ||
ValueTags.Int64 => new UnknownAttribute<long>(dbAttribute), | ||
ValueTags.Int128 => new UnknownAttribute<long>(dbAttribute), | ||
ValueTags.Float32 => new UnknownAttribute<float>(dbAttribute), | ||
ValueTags.Float64 => new UnknownAttribute<double>(dbAttribute), | ||
ValueTags.Ascii => new UnknownAttribute<string>(dbAttribute), | ||
ValueTags.Utf8 => new UnknownAttribute<string>(dbAttribute), | ||
ValueTags.Utf8Insensitive => new UnknownAttribute<string>(dbAttribute), | ||
ValueTags.Blob => new UnknownAttribute<byte[]>(dbAttribute), | ||
ValueTags.HashedBlob => new UnknownAttribute<byte[]>(dbAttribute), | ||
ValueTags.Reference => new UnknownAttribute<EntityId>(dbAttribute), | ||
_ => throw new ArgumentOutOfRangeException(nameof(dbAttribute.LowLevelType), dbAttribute.LowLevelType, null) | ||
}; | ||
} | ||
} |
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