From 855658554c908868cb3e8858184ce0a5e772809e Mon Sep 17 00:00:00 2001 From: Ivan Murashka Date: Wed, 19 Nov 2025 11:50:45 +0100 Subject: [PATCH] add readonly collections to storage --- Runtime/BinaryStorage.cs | 25 ++++++++++++++++++++++++ Tests/BinaryStorageTests.cs | 39 ++++++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/Runtime/BinaryStorage.cs b/Runtime/BinaryStorage.cs index 01f3ec5..e9a7333 100644 --- a/Runtime/BinaryStorage.cs +++ b/Runtime/BinaryStorage.cs @@ -254,6 +254,14 @@ public IDisposable MultipleChangeScope() /// Thrown if the type is not registered. public IList GetListOf(string key) => GetCollectionOf>(key); + /// Gets the read-only list associated with the specified key. + /// The type of the list elements. + /// The key to get the list for. + /// The read-only list associated with the key. + /// Thrown if the storage is disposed. + /// Thrown if the type is not registered. + public IReadOnlyList GetReadOnlyListOf(string key) => GetCollectionOf>(key); + /// Gets the set associated with the specified key. /// The type of the set elements. /// The key to get the set for. @@ -262,6 +270,14 @@ public IDisposable MultipleChangeScope() /// Thrown if the type is not registered. public ISet GetSetOf(string key) => GetCollectionOf>(key); + /// Gets the read-only set associated with the specified key. + /// The type of the set elements. + /// The key to get the set for. + /// The read-only set associated with the key. + /// Thrown if the storage is disposed. + /// Thrown if the type is not registered. + public IReadOnlyCollection GetReadOnlySetOf(string key) => GetCollectionOf>(key); + /// Gets the dictionary associated with the specified key. /// The type of the dictionary keys. /// The type of the dictionary values. @@ -271,6 +287,15 @@ public IDisposable MultipleChangeScope() /// Thrown if the type is not registered. public IDictionary GetDictionaryOf(string key) => GetCollectionOf, ReactiveDictionary>(key); + /// Gets the read-only dictionary associated with the specified key. + /// The type of the dictionary keys. + /// The type of the dictionary values. + /// The key to get the dictionary for. + /// The read-only dictionary associated with the key. + /// Thrown if the storage is disposed. + /// Thrown if the type is not registered. + public IReadOnlyDictionary GetReadOnlyDictionaryOf(string key) => GetCollectionOf, ReactiveDictionary>(key); + /// Determines whether the specified collection type is supported. /// The type of elements in the collection. /// The type of the collection. diff --git a/Tests/BinaryStorageTests.cs b/Tests/BinaryStorageTests.cs index c33f167..26c8128 100644 --- a/Tests/BinaryStorageTests.cs +++ b/Tests/BinaryStorageTests.cs @@ -166,6 +166,8 @@ public void WhenReactiveListChanged_ThenValuesInStorageCorrect() // Assert storage.GetListOf("numbers").Should().BeSameAs(list); storage.GetListOf("numbers").Should().Equal(list); + storage.GetReadOnlyListOf("numbers").Should().BeSameAs(list); + storage.GetReadOnlyListOf("numbers").Should().Equal(list); } [Test] @@ -208,10 +210,12 @@ public void WhenReactiveListChanged_AndStorageReloaded_ThenValuesInStorageCorrec { // Assert storage.Has("numbers"); - var list = storage.GetListOf("numbers"); - list.Count.Should().Be(2); - list[0].Should().Be(1); - list[1].Should().Be(2); + storage.GetListOf("numbers").Count.Should().Be(2); + storage.GetListOf("numbers")[0].Should().Be(1); + storage.GetListOf("numbers")[1].Should().Be(2); + storage.GetReadOnlyListOf("numbers").Count.Should().Be(2); + storage.GetReadOnlyListOf("numbers")[0].Should().Be(1); + storage.GetReadOnlyListOf("numbers")[1].Should().Be(2); } } @@ -249,6 +253,8 @@ public void WhenReactiveSetChanged_ThenValuesInStorageCorrect() // Assert storage.GetSetOf("numbers").Should().BeSameAs(set); storage.GetSetOf("numbers").Should().Equal(set); + storage.GetReadOnlySetOf("numbers").Should().BeSameAs(set); + storage.GetReadOnlySetOf("numbers").Should().Equal(set); } [Test] @@ -291,10 +297,12 @@ public void WhenReactiveSetChanged_AndStorageReloaded_ThenValuesInStorageCorrect { // Assert storage.Has("numbers"); - var set = storage.GetSetOf("numbers"); - set.Count.Should().Be(2); - set.Should().Contain(1); - set.Should().Contain(2); + storage.GetSetOf("numbers").Count.Should().Be(2); + storage.GetSetOf("numbers").Should().Contain(1); + storage.GetSetOf("numbers").Should().Contain(2); + storage.GetReadOnlySetOf("numbers").Count.Should().Be(2); + storage.GetReadOnlySetOf("numbers").Should().Contain(1); + storage.GetReadOnlySetOf("numbers").Should().Contain(2); } } @@ -334,6 +342,8 @@ public void WhenReactiveDictionaryChanged_ThenValuesInStorageCorrect() // Assert storage.GetDictionaryOf("numbers").Should().BeSameAs(map); storage.GetDictionaryOf("numbers").Should().Equal(map); + storage.GetReadOnlyDictionaryOf("numbers").Should().BeSameAs(map); + storage.GetReadOnlyDictionaryOf("numbers").Should().Equal(map); } [Test] @@ -379,11 +389,14 @@ public void WhenReactiveDictionaryChanged_AndStorageReloaded_ThenValuesInStorage { // Assert storage.Has("numbers"); - var map = storage.GetDictionaryOf("numbers"); - map.Count.Should().Be(2); - map.Should().ContainKeys(1, 2); - map[1].Should().Be("one"); - map[2].Should().Be("two"); + storage.GetDictionaryOf("numbers").Count.Should().Be(2); + storage.GetDictionaryOf("numbers").Should().ContainKeys(1, 2); + storage.GetDictionaryOf("numbers")[1].Should().Be("one"); + storage.GetDictionaryOf("numbers")[2].Should().Be("two"); + storage.GetReadOnlyDictionaryOf("numbers").Count.Should().Be(2); + storage.GetReadOnlyDictionaryOf("numbers").Should().ContainKeys(1, 2); + storage.GetReadOnlyDictionaryOf("numbers")[1].Should().Be("one"); + storage.GetReadOnlyDictionaryOf("numbers")[2].Should().Be("two"); } }