From 885e01df845890dda98331ab11cc886ec248c6dd Mon Sep 17 00:00:00 2001 From: Mattias Nordqvist Date: Sun, 10 Nov 2024 22:43:37 +0100 Subject: [PATCH] . --- ...pe.NonNullableOptional_Tests.BlogPost.g.cs | 71 +++++ ...mType.NullableOptional_Tests.BlogPost.g.cs | 71 +++++ FartingUnicorn.Tests/SingleField.cs | 277 ++++++++++-------- 3 files changed, 289 insertions(+), 130 deletions(-) create mode 100644 FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.g.cs create mode 100644 FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.g.cs diff --git a/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.g.cs b/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.g.cs new file mode 100644 index 0000000..fdd1631 --- /dev/null +++ b/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.g.cs @@ -0,0 +1,71 @@ +using DotNetThoughts.Results; +using System.Text.Json; +using static FartingUnicorn.MapperOptions; + +namespace FartingUnicorn.Generated; + +public static partial class Mappers +{ + public static Result MapToFartingUnicorn_Tests_SingleField_EnumType_NonNullableOptional_Tests_BlogPost(JsonElement jsonElement, MapperOptions mapperOptions = null, string[] path = null) + { + if (mapperOptions is null) + { + mapperOptions = new MapperOptions(); + } + if (path is null) + { + path = ["$"]; + } + if (jsonElement.ValueKind != JsonValueKind.Object) + { + return Result.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString())); + } + var obj = new FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost(); + + List errors = new(); + var isStatusPropertyDefined = jsonElement.TryGetProperty("Status", out var jsonStatusProperty); + if (isStatusPropertyDefined) + { + // type = FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus, isOption = True, isNullable = False + if (jsonStatusProperty.ValueKind == JsonValueKind.Null) + { + obj.Status = new None(); + } + else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus), out IConverter customConverter)) + { + if (jsonStatusProperty.ValueKind != customConverter.ExpectedJsonValueKind) + { + errors.Add(new ValueHasWrongTypeError([.. path, "Status"], customConverter.ExpectedJsonValueKind.ToString(), jsonStatusProperty.ValueKind.ToString())); + } + else + { + var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]); + if (result.Success) + { + obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus)x).Value; + } + else + { + errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray()); + } + } + } + } + else + { + errors.Add(new RequiredPropertyMissingError([.. path, "Status"])); + } + if(errors.Any()) + { + return Result.Error(errors); + } + if(false)/*check if is option*/ + { + } + else + { + return Result.Ok(obj); + } + throw new NotImplementedException(); + } +} diff --git a/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.g.cs b/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.g.cs new file mode 100644 index 0000000..5637e0d --- /dev/null +++ b/FartingUnicorn.Tests/Generated/MapperGenerator/MapperGenerator.MapperGenerator/Mapper.FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.g.cs @@ -0,0 +1,71 @@ +using DotNetThoughts.Results; +using System.Text.Json; +using static FartingUnicorn.MapperOptions; + +namespace FartingUnicorn.Generated; + +public static partial class Mappers +{ + public static Result MapToFartingUnicorn_Tests_SingleField_EnumType_NullableOptional_Tests_BlogPost(JsonElement jsonElement, MapperOptions mapperOptions = null, string[] path = null) + { + if (mapperOptions is null) + { + mapperOptions = new MapperOptions(); + } + if (path is null) + { + path = ["$"]; + } + if (jsonElement.ValueKind != JsonValueKind.Object) + { + return Result.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString())); + } + var obj = new FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost(); + + List errors = new(); + var isStatusPropertyDefined = jsonElement.TryGetProperty("Status", out var jsonStatusProperty); + if (isStatusPropertyDefined) + { + // type = FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus, isOption = True, isNullable = True + if (jsonStatusProperty.ValueKind == JsonValueKind.Null) + { + obj.Status = new None(); + } + else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus), out IConverter customConverter)) + { + if (jsonStatusProperty.ValueKind != customConverter.ExpectedJsonValueKind) + { + errors.Add(new ValueHasWrongTypeError([.. path, "Status"], customConverter.ExpectedJsonValueKind.ToString(), jsonStatusProperty.ValueKind.ToString())); + } + else + { + var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]); + if (result.Success) + { + obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus)x).Value; + } + else + { + errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray()); + } + } + } + } + else + { + obj.Status = null; + } + if(errors.Any()) + { + return Result.Error(errors); + } + if(false)/*check if is option*/ + { + } + else + { + return Result.Ok(obj); + } + throw new NotImplementedException(); + } +} diff --git a/FartingUnicorn.Tests/SingleField.cs b/FartingUnicorn.Tests/SingleField.cs index 9a1ca76..b708da7 100644 --- a/FartingUnicorn.Tests/SingleField.cs +++ b/FartingUnicorn.Tests/SingleField.cs @@ -1055,135 +1055,152 @@ public void NulledNullableField(Func> map) } } - //public class NonNullableOptional_Tests - //{ - // public static IEnumerable GetMappers => - // [ - // [(Func>)(x => Map(x, null, null))], - // [(Func>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_IntType_NonNullableOptional_Tests_BlogPost(x, null))] - // ]; - // [CreateMapper] - // public class BlogPost - // { - // /// - // /// Field must exist - // /// Value can be null - // /// - // public Option Rating { get; set; } - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void ValidSingleField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // "Rating": 5 - // } - // """); - // var blogPost = map(jsonElement); - - // Assert.True(blogPost.Success); - // blogPost.Value.Rating.Should().BeOfType>(); - // var someIsDraft = (blogPost.Value.Rating as Some)!; - // someIsDraft.Value.Should().Be(5); - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void MissingNonNullableField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // } - // """); - // var blogPost = map(jsonElement); - - // Assert.False(blogPost.Success); - // blogPost.Errors.Should().ContainSingle(); - // blogPost.Errors.Single().Should().BeOfType(); - // blogPost.Errors.Single().Message.Should().Be("$.Rating is required"); - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void NulledOptionalField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // "Rating": null - // } - // """); - // var blogPost = map(jsonElement); - - // Assert.True(blogPost.Success); - // blogPost.Value.Rating.Should().BeOfType>(); - // } - //} - - //public class NullableOptional_Tests - //{ - // public static IEnumerable GetMappers => - // [ - // [(Func>)(x => Map(x, null, null))], - // [(Func>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_IntType_NullableOptional_Tests_BlogPost(x, null))] - // ]; - - // [CreateMapper] - // public class BlogPost - // { - // /// - // /// Field can be missing - // /// Value can be null - // /// - // public Option? Rating { get; set; } - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void ValidSingleField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // "Rating": 5 - // } - // """); - // var blogPost = map(jsonElement); - - // blogPost.Should().BeSuccessful(); - // blogPost.Value.Rating.Should().BeOfType>(); - // var someIsDraft = (blogPost.Value.Rating as Some)!; - // someIsDraft.Value.Should().Be(5); - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void MissingOptionalField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // } - // """); - // var blogPost = map(jsonElement); - - // blogPost.Should().BeSuccessful(); - // blogPost.Value.Rating.Should().BeNull(); - // } - - // [Theory] - // [MemberData(nameof(GetMappers))] - // public void NulledOptionalField(Func> map) - // { - // var jsonElement = JsonSerializer.Deserialize(""" - // { - // "Rating": null - // } - // """); - // var blogPost = map(jsonElement); - - // blogPost.Should().BeSuccessful(); - // blogPost.Value.Rating.Should().BeOfType>(); - // } - //} + public class NonNullableOptional_Tests + { + public static IEnumerable GetMappers + { + get + { + var mapperOptions = new MapperOptions(); + mapperOptions.AddConverter(new EnumAsStringConverter()); + return [ + [(Func>)(x => Map(x, mapperOptions, null))], + [(Func>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_EnumType_NonNullableOptional_Tests_BlogPost(x, mapperOptions, null))] + ]; + } + } + + [CreateMapper] + public class BlogPost + { + public enum BlogPostStatus { Draft, Published } + /// + /// Field must exist + /// Value can be null + /// + public Option Status { get; set; } + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void ValidSingleField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + "Status": "Published" + } + """); + var blogPost = map(jsonElement); + + Assert.True(blogPost.Success); + blogPost.Value.Status.Should().BeOfType>(); + var someIsDraft = (blogPost.Value.Status as Some)!; + someIsDraft.Value.Should().Be(BlogPost.BlogPostStatus.Published); + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void MissingNonNullableField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + } + """); + var blogPost = map(jsonElement); + + Assert.False(blogPost.Success); + blogPost.Errors.Should().ContainSingle(); + blogPost.Errors.Single().Should().BeOfType(); + blogPost.Errors.Single().Message.Should().Be("$.Status is required"); + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void NulledOptionalField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + "Status": null + } + """); + var blogPost = map(jsonElement); + + Assert.True(blogPost.Success); + blogPost.Value.Status.Should().BeOfType>(); + } + } + + public class NullableOptional_Tests + { + public static IEnumerable GetMappers + { + get + { + var mapperOptions = new MapperOptions(); + mapperOptions.AddConverter(new EnumAsStringConverter()); + return [ + [(Func>)(x => Map(x, mapperOptions, null))], + [(Func>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_EnumType_NullableOptional_Tests_BlogPost(x, mapperOptions, null))] + ]; + } + } + + [CreateMapper] + public class BlogPost + { + public enum BlogPostStatus { Draft, Published } + /// + /// Field can be missing + /// Value can be null + /// + public Option? Status { get; set; } + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void ValidSingleField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + "Status": "Published" + } + """); + var blogPost = map(jsonElement); + + blogPost.Should().BeSuccessful(); + blogPost.Value.Status.Should().BeOfType>(); + var someIsDraft = (blogPost.Value.Status as Some)!; + someIsDraft.Value.Should().Be(BlogPost.BlogPostStatus.Published); + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void MissingOptionalField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + } + """); + var blogPost = map(jsonElement); + + blogPost.Should().BeSuccessful(); + blogPost.Value.Status.Should().BeNull(); + } + + [Theory] + [MemberData(nameof(GetMappers))] + public void NulledOptionalField(Func> map) + { + var jsonElement = JsonSerializer.Deserialize(""" + { + "Status": null + } + """); + var blogPost = map(jsonElement); + + blogPost.Should().BeSuccessful(); + blogPost.Value.Status.Should().BeOfType>(); + } + } } }