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 98f9bca commit b64953c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 14 deletions.
28 changes: 19 additions & 9 deletions FartingUnicorn.Tests/Converters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotNetThoughts.Results;
using DotNetThoughts.FartingUnicorn;
using DotNetThoughts.Results;

using FluentAssertions;

Expand Down Expand Up @@ -46,14 +47,22 @@ public Result<object> Convert(Type clrType, JsonElement jsonElement, MapperOptio
}
}

[CreateMapper]
public class BlogPost
{
public required Id Id { get; set; }
public required string Title { get; set; }
public Id Id { get; set; }
public string Title { get; set; }
}

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

[Theory]
[MemberData(nameof(GetMappers))]
public void Valid(Func<JsonElement, MapperOptions, Result<BlogPost>> map)
{
var mapperOptions = new MapperOptions();
mapperOptions.AddConverter(new IdConverter());
Expand All @@ -63,14 +72,15 @@ public void Valid()
"Title": "Farting Unicorns"
}
""");
var blogPost = Mapper.Map<BlogPost>(json, mapperOptions);
var blogPost = map(json, mapperOptions);
blogPost.Should().BeSuccessful();
blogPost.Value.Id.Value.Should().Be(123456);
blogPost.Value.Title.Should().Be("Farting Unicorns");
}

[Fact]
public void InvalidId()
[Theory]
[MemberData(nameof(GetMappers))]
public void InvalidId(Func<JsonElement, MapperOptions, Result<BlogPost>> map)
{
var mapperOptions = new MapperOptions();
mapperOptions.AddConverter(new IdConverter());
Expand All @@ -80,7 +90,7 @@ public void InvalidId()
"Title": "Farting Unicorns"
}
""");
var blogPost = Mapper.Map<BlogPost>(json, mapperOptions);
var blogPost = map(json, mapperOptions);
blogPost.Success.Should().BeFalse();
blogPost.Errors.Should().ContainSingle(e => e.Message == "Failed to map $.Id: 'Not a number' is not a valid Id");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using DotNetThoughts.Results;
using System.Text.Json;
using static FartingUnicorn.MapperOptions;

namespace FartingUnicorn.Generated;

public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.Converters.BlogPost> MapToFartingUnicorn_Tests_Converters_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.Converters.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
var obj = new FartingUnicorn.Tests.Converters.BlogPost();

List<IError> errors = new();
var isIdPropertyDefined = jsonElement.TryGetProperty("Id", out var jsonIdProperty);
if (isIdPropertyDefined)
{
// type = FartingUnicorn.Tests.Converters.Id, isOption = False, isNullable = False
if (jsonIdProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "Id"]));
}
else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.Converters.Id), out IConverter customConverter))
{
if (jsonIdProperty.ValueKind != customConverter.ExpectedJsonValueKind)
{
errors.Add(new ValueHasWrongTypeError([.. path, "Id"], customConverter.ExpectedJsonValueKind.ToString(), jsonIdProperty.ValueKind.ToString()));
}
else
{
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.Converters.Id), jsonIdProperty, mapperOptions, [.. path, "Id"]);
if (result.Success)
{
obj.Id = result.Map(x => (FartingUnicorn.Tests.Converters.Id)x).Value;
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Id"], x.Message)).ToArray());
}
}
}
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "Id"]));
}
var isTitlePropertyDefined = jsonElement.TryGetProperty("Title", out var jsonTitleProperty);
if (isTitlePropertyDefined)
{
// type = System.String, isOption = False, isNullable = False
if (jsonTitleProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "Title"]));
}
else if (jsonTitleProperty.ValueKind == JsonValueKind.String)
{
obj.Title = jsonTitleProperty.GetString();
}
else
{
errors.Add(new ValueHasWrongTypeError([.. path, "Title"], "String", jsonTitleProperty.ValueKind.ToString()));
}
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "Title"]));
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.Converters.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.Converters.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Mappers
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray());
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Status"], x.Message)).ToArray());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Mappers
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray());
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Status"], x.Message)).ToArray());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Mappers
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray());
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Status"], x.Message)).ToArray());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static partial class Mappers
}
else
{
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "{p.Name}"], x.Message)).ToArray());
errors.AddRange(result.Errors.Select(x => new MappingError([.. path, "Status"], x.Message)).ToArray());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
sb.AppendLine("else");
using (var _6 = sb.CodeBlock())
{
sb.AppendLine("errors.AddRange(result.Errors.Select(x => new MappingError([.. path, \"{p.Name}\"], x.Message)).ToArray());");
sb.AppendLine($"errors.AddRange(result.Errors.Select(x => new MappingError([.. path, \"{p.Name}\"], x.Message)).ToArray());");
}
}
}
Expand Down

0 comments on commit b64953c

Please sign in to comment.