Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 10, 2024
1 parent c9fe3f4 commit 1b9daf3
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using DotNetThoughts.Results;
using System.Text.Json;

namespace FartingUnicorn.Generated;

public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableNonOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_BoolType_NonNullableNonOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableNonOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.BoolType.NonNullableNonOptional_Tests.BlogPost();

List<IError> errors = new();
var isIsDraftPropertyDefined = jsonElement.TryGetProperty("IsDraft", out var jsonIsDraftProperty);
if (isIsDraftPropertyDefined)
{
// type = Boolean, isOption = False, isNullable = False
if (jsonIsDraftProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "IsDraft"]));
}
else if (jsonIsDraftProperty.ValueKind == JsonValueKind.True || jsonIsDraftProperty.ValueKind == JsonValueKind.False)
{
obj.IsDraft = jsonIsDraftProperty.GetBoolean();
}
else
{
errors.Add(new ValueHasWrongTypeError([.. path, "IsDraft"], "Boolean", jsonIsDraftProperty.ValueKind.ToString()));
}
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "IsDraft"]));
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableNonOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NonNullableNonOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using DotNetThoughts.Results;
using System.Text.Json;

namespace FartingUnicorn.Generated;

public static partial class Mappers
{
public static Result<FartingUnicorn.Tests.SingleField.BoolType.NullableNonOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_BoolType_NullableNonOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableNonOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.BoolType.NullableNonOptional_Tests.BlogPost();

List<IError> errors = new();
var isIsDraftPropertyDefined = jsonElement.TryGetProperty("IsDraft", out var jsonIsDraftProperty);
if (isIsDraftPropertyDefined)
{
// type = Nullable, isOption = False, isNullable = True
if (jsonIsDraftProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "IsDraft"]));
}
}
else
{
obj.IsDraft = null;
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableNonOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.BoolType.NullableNonOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
82 changes: 50 additions & 32 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,17 @@ public void NulledOptionalField(Func<JsonElement, Result<BlogPost>> map)
}
}

public class ValueType
public class BoolType
{
public class NonNullableNonOptional_Tests

{
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_BoolType_NonNullableNonOptional_Tests_BlogPost(x, null))]
];

[CreateMapper]
public class BlogPost
{
/// <summary>
Expand All @@ -317,60 +323,64 @@ public class BlogPost
public bool IsDraft { get; set; }
}

[Fact]
public void ValidSingleField()
[Theory]
[MemberData(nameof(GetMappers))]
public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"IsDraft": true
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
{
"IsDraft": true
}
""");
var blogPost = map(jsonElement);

Assert.True(blogPost.Success);
blogPost.Value.IsDraft.Should().BeTrue();
}

[Fact]
public void MissingNonNullableField()
[Theory]
[MemberData(nameof(GetMappers))]
public void MissingNonNullableField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
{
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<RequiredPropertyMissingError>();
blogPost.Errors.Single().Message.Should().Be("$.IsDraft is required");
}

[Fact]
public void NulledNonNullableField()
[Theory]
[MemberData(nameof(GetMappers))]
public void NulledNonNullableField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"IsDraft": null
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
{
"IsDraft": null
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
blogPost.Errors.Single().Should().BeOfType<RequiredValueMissingError>();
blogPost.Errors.Single().Message.Should().Be("$.IsDraft must have a value");
}

[Fact]
public void InvalidFieldType()
[Theory]
[MemberData(nameof(GetMappers))]
public void InvalidFieldType(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"IsDraft": 123456
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
{
"IsDraft": 123456
}
""");
var blogPost = map(jsonElement);

Assert.False(blogPost.Success);
blogPost.Errors.Should().ContainSingle();
Expand All @@ -381,6 +391,13 @@ public void InvalidFieldType()

public class NullableNonOptional_Tests
{
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_BoolType_NullableNonOptional_Tests_BlogPost(x, null))]
];

[CreateMapper]
public class BlogPost
{
/// <summary>
Expand All @@ -390,15 +407,16 @@ public class BlogPost
public bool? IsDraft { get; set; }
}

[Fact]
public void ValidSingleField()
[Theory]
[MemberData(nameof(GetMappers))]
public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"IsDraft": true
}
""");
var blogPost = Mapper.Map<BlogPost>(jsonElement);
var blogPost = map(jsonElement);

Assert.True(blogPost.Success);
blogPost.Value.IsDraft.Should().BeTrue();
Expand Down
22 changes: 21 additions & 1 deletion MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Collections;
using System.Collections.Immutable;
using System.IO;
using System.Text;

namespace MapperGenerator;
Expand Down Expand Up @@ -100,6 +99,27 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
sb.AppendLine($"errors.Add(new ValueHasWrongTypeError([.. path, \"{p.Name}\"], \"String\", json{p.Name}Property.ValueKind.ToString()));");
}
}
else if(p.Type == "Boolean")
{
sb.AppendLine($"else if (json{p.Name}Property.ValueKind == JsonValueKind.True || json{p.Name}Property.ValueKind == JsonValueKind.False)");
using (var _4 = sb.CodeBlock())
{
if (p.IsOption)
{
sb.AppendLine($"obj.{p.Name} = new Some<bool>(json{p.Name}Property.GetBoolean());");

}
else
{
sb.AppendLine($"obj.{p.Name} = json{p.Name}Property.GetBoolean();");
}
}
sb.AppendLine("else");
using (var _4 = sb.CodeBlock())
{
sb.AppendLine($"errors.Add(new ValueHasWrongTypeError([.. path, \"{p.Name}\"], \"Boolean\", json{p.Name}Property.ValueKind.ToString()));");
}
}
}
sb.AppendLine("else");
using (var _3 = sb.CodeBlock())
Expand Down

0 comments on commit 1b9daf3

Please sign in to comment.