Skip to content

Commit

Permalink
Merge pull request #231 from Cysharp/hadashiA/fix-lookup
Browse files Browse the repository at this point in the history
Fix deserialized behavior  ILookUp<>[] key is missing
  • Loading branch information
hadashiA authored Mar 14, 2024
2 parents f3abd3d + a647e9d commit e3eebc6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ public IEnumerable<TElement> this[TKey key]
{
get
{
return this.groupings[key];
return this.groupings.TryGetValue(key, out var value) ? value : Enumerable.Empty<TElement>();
}
}

Expand Down
11 changes: 9 additions & 2 deletions tests/MemoryPack.Tests/InterfaceFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public void Lookup()
{
(1, 2), (1, 100), (3, 42), (45, 30), (3, 10)
};
var lookup = seq.ToLookup(x => x.Item1, x => x.Item2);

var lookup = seq.ToLookup(x => x.Item1, x => x.Item2);
{
var bin = MemoryPackSerializer.Serialize(lookup);
MemoryPackSerializer.Deserialize<ILookup<int, int>>(bin)
Expand All @@ -115,7 +115,14 @@ public void Lookup()
var bin = MemoryPackSerializer.Serialize(grouping);
var g2 = MemoryPackSerializer.Deserialize<IGrouping<int, int>>(bin);
g2!.Key.Should().Be(grouping.Key);
g2!.AsEnumerable().Should().BeEquivalentTo(grouping.AsEnumerable());
g2.AsEnumerable().Should().BeEquivalentTo(grouping.AsEnumerable());
}

var emptyLookup = Array.Empty<int>().ToLookup(x => x, x => x);
{
var bin = MemoryPackSerializer.Serialize(emptyLookup);
var deserialized = MemoryPackSerializer.Deserialize<ILookup<int, int>>(bin);
deserialized![0].Should().BeEmpty();
}
}

Expand Down

0 comments on commit e3eebc6

Please sign in to comment.