Skip to content

Commit

Permalink
Missing key behavior (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
imurashka authored Jun 22, 2024
1 parent 2bb2a90 commit 258965d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions .BinaryPrefs/Appegy.BinaryStorage.csproj.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_002E_002E_005Cruntime_005Ccollections/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_002E_002E_005Cruntime_005Cexceptions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_002E_002E_005Cruntime_005Cserialization/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_002E_002E_005Cruntime_005Csettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_002E_002E_005Cruntime_005Cutilities/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
34 changes: 24 additions & 10 deletions Runtime/BinaryStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public partial class BinaryStorage : IDisposable
/// </summary>
public bool AutoSave { get; set; }

/// <summary>
/// Gets or sets the behavior when a requested key is not found in the storage.
/// </summary>
public MissingKeyBehavior MissingKeyBehavior { get; set; } = MissingKeyBehavior.ReturnDefaultValueOnly;

/// <summary>
/// Gets a value indicating whether there are unsaved changes.
/// </summary>
Expand Down Expand Up @@ -85,18 +90,27 @@ public virtual bool Supports<T>()
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="key">The key to get the value for.</param>
/// <param name="initValue">The initial value to use if the key does not exist.</param>
/// <param name="defaultValue">The default value to use if the key does not exist.</param>
/// <returns>The value associated with the key.</returns>
public virtual T Get<T>(string key, T initValue = default)
public virtual T Get<T>(string key, T defaultValue = default)
{
ThrowIfDisposed();
ThrowIfCollection<T>();
var record = GetRecord(key) ?? AddRecord(key, initValue);
if (record is not Record<T> typedRecord)
var record = GetRecord(key);
switch (record)
{
throw new UnexpectedTypeException(key, nameof(Get), record.Type, typeof(T));
case Record<T> typedRecord:
return typedRecord.Value;
case not null:
throw new UnexpectedTypeException(key, nameof(Get), record.Type, typeof(T));
case null:
return MissingKeyBehavior switch
{
MissingKeyBehavior.InitializeWithDefaultValue => AddRecord(key, defaultValue).Value,
MissingKeyBehavior.ReturnDefaultValueOnly => defaultValue,
_ => throw new UnexpectedEnumException(typeof(MissingKeyBehavior), MissingKeyBehavior)
};
}
return typedRecord.Value;
}

/// <summary>
Expand All @@ -105,9 +119,9 @@ public virtual T Get<T>(string key, T initValue = default)
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="key">The key to set the value for.</param>
/// <param name="value">The value to set.</param>
/// <param name="overrideTypeIfExists">Whether to override the type if the key already exists.</param>
/// <param name="overrideTypeMismatch">Whether to override the value if the key already exists but with another type.</param>
/// <returns>True if the value was set; otherwise, false.</returns>
public virtual bool Set<T>(string key, T value, bool overrideTypeIfExists = false)
public virtual bool Set<T>(string key, T value, bool overrideTypeMismatch = false)
{
ThrowIfDisposed();
ThrowIfCollection<T>();
Expand All @@ -124,7 +138,7 @@ public virtual bool Set<T>(string key, T value, bool overrideTypeIfExists = fals
return ChangeRecord(typedRecord, value);
}

if (!overrideTypeIfExists)
if (!overrideTypeMismatch)
{
throw new UnexpectedTypeException(key, nameof(Set), record.Type, typeof(T));
}
Expand Down Expand Up @@ -295,7 +309,7 @@ private TCollection GetCollectionOf<T, TCollection>(string key)
/// <param name="key">The key to add the record for.</param>
/// <param name="value">The value to add.</param>
/// <returns>The added record.</returns>
private Record AddRecord<T>(string key, T value)
private Record<T> AddRecord<T>(string key, T value)
{
var typeIndex = _supportedTypes.FindIndex(static c => c is TypedBinarySection<T>);
if (typeIndex == -1)
Expand Down
12 changes: 12 additions & 0 deletions Runtime/Exceptions/UnexpectedEnumException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Appegy.Storage
{
public class UnexpectedEnumException : Exception
{
public UnexpectedEnumException(Type actualType, Enum value)
: base($"Unexpected enum type {actualType.Name}.{value}")
{
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Exceptions/UnexpectedEnumException.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Settings.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Runtime/Settings/MissingKeyBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Appegy.Storage
{
/// <summary>
/// Specifies the behavior when a requested key is not found in the storage.
/// </summary>
public enum MissingKeyBehavior
{
/// <summary>
/// Initializes the key with the provided default value and stores it.
/// </summary>
InitializeWithDefaultValue,

/// <summary>
/// Returns the provided default value without storing it.
/// </summary>
ReturnDefaultValueOnly
}
}
3 changes: 3 additions & 0 deletions Runtime/Settings/MissingKeyBehavior.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 258965d

Please sign in to comment.