Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 11, 2024
1 parent 04db682 commit 51a4e6f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<FartingUnicorn.Tests.Objects.Nullable.BlogPost> MapToFartingUnicorn_Tests_Objects_Nullable_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<FartingUnicorn.Tests.Objects.Nullable.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
var obj = new FartingUnicorn.Tests.Objects.Nullable.BlogPost();

List<IError> errors = new();
var isAuthorPropertyDefined = jsonElement.TryGetProperty("Author", out var jsonAuthorProperty);
if (isAuthorPropertyDefined)
{
// type = FartingUnicorn.Tests.Objects.Nullable.Author, isOption = False, isNullable = True
if (jsonAuthorProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "Author"]));
}
else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.Objects.Nullable.Author), out IConverter customConverter))
{
if (jsonAuthorProperty.ValueKind != customConverter.ExpectedJsonValueKind)
{
errors.Add(new ValueHasWrongTypeError([.. path, "Author"], customConverter.ExpectedJsonValueKind.ToString(), jsonAuthorProperty.ValueKind.ToString()));
}
else
{
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.Objects.Nullable.Author), jsonAuthorProperty, mapperOptions, [.. path, "Author"]);
if (result.Success)
{
obj.Author = result.Map(x => (FartingUnicorn.Tests.Objects.Nullable.Author)x).Value;
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Author"], x.Message)).ToArray());
}
}
}
}
else
{
obj.Author = null;
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.Objects.Nullable.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.Objects.Nullable.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
26 changes: 19 additions & 7 deletions FartingUnicorn.Tests/Objects.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using FluentAssertions;
using DotNetThoughts.FartingUnicorn;
using DotNetThoughts.Results;

using FluentAssertions;

using System.Text.Json;

Expand Down Expand Up @@ -266,6 +269,7 @@ public void AuthorIsWrongType()

public class Nullable
{
[CreateMapper]
public class BlogPost
{
public Author? Author { get; set; }
Expand All @@ -276,8 +280,15 @@ public class Author
public Option<int> Age { get; set; }
}

[Fact]
public void Valid()
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Mapper.Map<BlogPost>(x))],
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_Objects_Nullable_BlogPost(x))]
];

[Theory]
[MemberData(nameof(GetMappers))]
public void Valid(Func<JsonElement, Result<BlogPost>> map)
{
var json = JsonSerializer.Deserialize<JsonElement>("""
{
Expand All @@ -287,21 +298,22 @@ public void Valid()
}
}
""");
var blogPost = Mapper.Map<BlogPost>(json);
var blogPost = map(json);
blogPost.Should().BeSuccessful();
blogPost.Value.Author.Should().NotBeNull();
blogPost.Value.Author!.Name.Should().Be("John Doe");
blogPost.Value.Author!.Age.Should().BeOfType<Some<int>>();
}

[Fact]
public void Missing_OK()
[Theory]
[MemberData(nameof(GetMappers))]
public void Missing_OK(Func<JsonElement, Result<BlogPost>> map)
{
var json = JsonSerializer.Deserialize<JsonElement>("""
{
}
""");
var blogPost = Mapper.Map<BlogPost>(json);
var blogPost = map(json);
blogPost.Should().BeSuccessful();
blogPost.Value.Author.Should().BeNull();
}
Expand Down

0 comments on commit 51a4e6f

Please sign in to comment.