-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ThrowIfMissing to allow making arbitrary fields optional (#177)
- Loading branch information
Showing
7 changed files
with
95 additions
and
4 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
53 changes: 53 additions & 0 deletions
53
...erdeImplRoslynGenerator/Serde.Test.JsonDeserializeTests.ThrowMissingFalse.IDeserialize.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,53 @@ | ||
| ||
#nullable enable | ||
using System; | ||
using Serde; | ||
|
||
namespace Serde.Test | ||
{ | ||
partial class JsonDeserializeTests | ||
{ | ||
partial record ThrowMissingFalse : Serde.IDeserialize<Serde.Test.JsonDeserializeTests.ThrowMissingFalse> | ||
{ | ||
static Serde.Test.JsonDeserializeTests.ThrowMissingFalse Serde.IDeserialize<Serde.Test.JsonDeserializeTests.ThrowMissingFalse>.Deserialize(IDeserializer deserializer) | ||
{ | ||
string _l_present = default !; | ||
bool _l_missing = default !; | ||
byte _r_assignedValid = 0; | ||
var _l_typeInfo = ThrowMissingFalseSerdeTypeInfo.TypeInfo; | ||
var typeDeserialize = deserializer.DeserializeType(_l_typeInfo); | ||
int _l_index_; | ||
while ((_l_index_ = typeDeserialize.TryReadIndex(_l_typeInfo, out var _l_errorName)) != IDeserializeType.EndOfType) | ||
{ | ||
switch (_l_index_) | ||
{ | ||
case 0: | ||
_l_present = typeDeserialize.ReadValue<string, StringWrap>(_l_index_); | ||
_r_assignedValid |= ((byte)1) << 0; | ||
break; | ||
case 1: | ||
_l_missing = typeDeserialize.ReadValue<bool, BoolWrap>(_l_index_); | ||
_r_assignedValid |= ((byte)1) << 1; | ||
break; | ||
case Serde.IDeserializeType.IndexNotFound: | ||
break; | ||
default: | ||
throw new InvalidOperationException("Unexpected index: " + _l_index_); | ||
} | ||
} | ||
|
||
if ((_r_assignedValid & 0b1) != 0b1) | ||
{ | ||
throw new Serde.InvalidDeserializeValueException("Not all members were assigned"); | ||
} | ||
|
||
var newType = new Serde.Test.JsonDeserializeTests.ThrowMissingFalse() | ||
{ | ||
Present = _l_present, | ||
Missing = _l_missing, | ||
}; | ||
return newType; | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...erdeImplRoslynGenerator/Serde.Test.JsonDeserializeTests.ThrowMissingFalseSerdeTypeInfo.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,14 @@ | ||
namespace Serde.Test; | ||
partial class JsonDeserializeTests | ||
{ | ||
internal static class ThrowMissingFalseSerdeTypeInfo | ||
{ | ||
internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( | ||
"ThrowMissingFalse", | ||
Serde.TypeInfo.TypeKind.CustomType, | ||
new (string, System.Reflection.MemberInfo)[] { | ||
("present", typeof(Serde.Test.JsonDeserializeTests.ThrowMissingFalse).GetProperty("Present")!), | ||
("missing", typeof(Serde.Test.JsonDeserializeTests.ThrowMissingFalse).GetProperty("Missing")!) | ||
}); | ||
} | ||
} |