-
-
Notifications
You must be signed in to change notification settings - Fork 202
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 #268 from Cysharp/hadashiA/skip-overwrite-by-default
Add attr to not overwrite Member with default value
- Loading branch information
Showing
9 changed files
with
159 additions
and
52 deletions.
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
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
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
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
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
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
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,30 @@ | ||
using MemoryPack.Tests.Models; | ||
|
||
namespace MemoryPack.Tests; | ||
|
||
public class DefaultValueTest | ||
{ | ||
[Fact] | ||
public void SuppressDefaultInitialization() | ||
{ | ||
var bin = MemoryPackSerializer.Serialize(new DefaultValuePlaceholder { X = 1 }); | ||
var expected = new HasDefaultValue(); | ||
var deserializedValue = MemoryPackSerializer.Deserialize<HasDefaultValue>(bin)!; | ||
deserializedValue.Y.Should().Be(default); | ||
deserializedValue.Z.Should().Be(default); | ||
deserializedValue.Y2.Should().Be(expected.Y2); | ||
deserializedValue.Z2.Should().Be(expected.Z2); | ||
} | ||
|
||
[Fact] | ||
public void SuppressDefaultInitialization_VersionTolerant() | ||
{ | ||
var bin = MemoryPackSerializer.Serialize(new DefaultValuePlaceholderWithVersionTolerant { X = 1 }); | ||
var expected = new HasDefaultValueWithVersionTolerant(); | ||
var deserializedValue = MemoryPackSerializer.Deserialize<HasDefaultValueWithVersionTolerant>(bin)!; | ||
deserializedValue.Y.Should().Be(default); | ||
deserializedValue.Z.Should().Be(default); | ||
deserializedValue.Y2.Should().Be(expected.Y2); | ||
deserializedValue.Z2.Should().Be(expected.Z2); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,69 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MemoryPack.Tests.Models; | ||
|
||
enum TestEnum | ||
{ | ||
A, B, C | ||
} | ||
|
||
[MemoryPackable] | ||
partial class DefaultValuePlaceholder | ||
{ | ||
public int X { get; set; } | ||
} | ||
|
||
[MemoryPackable] | ||
partial class FieldDefaultValue | ||
[MemoryPackable(GenerateType.VersionTolerant, SerializeLayout.Sequential)] | ||
partial class DefaultValuePlaceholderWithVersionTolerant | ||
{ | ||
public int X; | ||
public int Y = 12345; | ||
public float Z = 678.9f; | ||
public string S = "aaaaaaaaa"; | ||
public bool B = true; | ||
public int X { get; set; } | ||
} | ||
|
||
[MemoryPackable] | ||
partial class PropertyDefaultValue | ||
[MemoryPackable(GenerateType.VersionTolerant, SerializeLayout.Sequential)] | ||
partial class HasDefaultValueWithVersionTolerant | ||
{ | ||
internal enum NestedEnum | ||
{ | ||
A, B | ||
} | ||
public int X; | ||
|
||
public int X { get; set; } | ||
public int Y { get; set; } = 12345; | ||
public int Y = 12345; | ||
public float Z { get; set; } = 678.9f; | ||
public string S { get; set; } = "aaaaaaaaa"; | ||
public bool B { get; set; } = true; | ||
public List<string> Alpha { get; set; } = new List<string>(new HashSet<string>()); | ||
public TestEnum E { get; set; } = TestEnum.A; | ||
public NestedEnum E2 { get; set; } = NestedEnum.A; | ||
public (TestEnum, List<string>) Tuple { get; set; } = (TestEnum.A, new List<string>(new HashSet<string>())); | ||
public DateTime Struct { get; set; } = default!; | ||
|
||
[SuppressDefaultInitialization] | ||
public int Y2 = 12345; | ||
|
||
[SuppressDefaultInitialization] | ||
public float Z2 { get; set; } = 678.9f; | ||
} | ||
|
||
[MemoryPackable] | ||
partial class CtorParamDefaultValue | ||
partial class HasDefaultValue | ||
{ | ||
public int X; | ||
public int Y; | ||
public float Z; | ||
public string S; | ||
public bool B; | ||
public decimal D; | ||
public DateTime StructValue; | ||
|
||
[MemoryPackConstructor] | ||
public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true, decimal d = 99M, DateTime structValue = default) | ||
{ | ||
X = x; | ||
Y = y; | ||
Z = z; | ||
S = s; | ||
B = b; | ||
D = d; | ||
StructValue = structValue; | ||
} | ||
public int Y = 12345; | ||
public float Z { get; set; } = 678.9f; | ||
|
||
[SuppressDefaultInitialization] | ||
public int Y2 = 12345; | ||
|
||
[SuppressDefaultInitialization] | ||
public float Z2 { get; set; } = 678.9f; | ||
} |