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 fa8806f commit 3bdb8b7
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 64 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.SingleField.EnumType.NullableNonOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_EnumType_NullableNonOptional_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<FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
var obj = new FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_Tests.BlogPost();

List<IError> errors = new();
var isStatusPropertyDefined = jsonElement.TryGetProperty("Status", out var jsonStatusProperty);
if (isStatusPropertyDefined)
{
// type = FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_Tests.BlogPost.BlogPostStatus, isOption = False, isNullable = True
if (jsonStatusProperty.ValueKind == JsonValueKind.Null)
{
errors.Add(new RequiredValueMissingError([.. path, "Status"]));
}
else if (mapperOptions.TryGetConverter(typeof(FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_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.NullableNonOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]);
if (result.Success)
{
obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_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<FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.EnumType.NullableNonOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
137 changes: 73 additions & 64 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,75 +976,84 @@ public void InvalidFieldType(Func<JsonElement, Result<BlogPost>> map)
}
}

//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_IntType_NullableNonOptional_Tests_BlogPost(x, null))]
// ];

// [CreateMapper]
// public class BlogPost
// {
// /// <summary>
// /// Field can be missing
// /// Value cannot be null
// /// </summary>
// public int? Rating { get; set; }
// }

// [Theory]
// [MemberData(nameof(GetMappers))]
// public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
// {
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// "Rating": 5
// }
// """);
// var blogPost = map(jsonElement);
public class NullableNonOptional_Tests
{
public static IEnumerable<object[]> GetMappers
{
get
{
var mapperOptions = new MapperOptions();
mapperOptions.AddConverter(new EnumAsStringConverter());
return [
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, mapperOptions, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_EnumType_NullableNonOptional_Tests_BlogPost(x, mapperOptions, null))]
];
}
}

// Assert.True(blogPost.Success);
// blogPost.Value.Rating.Should().Be(5);
// }
[CreateMapper]
public class BlogPost
{
public enum BlogPostStatus { Draft, Published }

// [Theory]
// [MemberData(nameof(GetMappers))]
// public void MissingNullableField(Func<JsonElement, Result<BlogPost>> map)
// {
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// }
// """);
// var blogPost = map(jsonElement);
/// <summary>
/// Field can be missing
/// Value cannot be null
/// </summary>
public BlogPostStatus? Status { get; set; }
}

// Assert.True(blogPost.Success);
// blogPost.Value.Rating.Should().BeNull();
// }
[Theory]
[MemberData(nameof(GetMappers))]
public void ValidSingleField(Func<JsonElement, Result<BlogPost>> map)
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
{
"Status": "Published"
}
""");
var blogPost = map(jsonElement);

// [Theory]
// [MemberData(nameof(GetMappers))]
// public void NulledNullableField(Func<JsonElement, Result<BlogPost>> map)
// {
// // Might seem counterintuitive, but remember, we said that
// // clr-null should reflect a missing field. In this case, the field exists, but does not have a value.
// // This would be equivalent to None in an Option type.
// // Therefor, this is not valid json for a nullable field.
// // If the field exists, it must have a value!
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// "Rating": null
// }
// """);
// var blogPost = map(jsonElement);
Assert.True(blogPost.Success);
blogPost.Value.Status.Should().Be(BlogPost.BlogPostStatus.Published);
}

// Assert.False(blogPost.Success);
// blogPost.Errors.Should().ContainSingle();
// blogPost.Errors.Single().Should().BeOfType<RequiredValueMissingError>();
// blogPost.Errors.Single().Message.Should().Be("$.Rating must have a value");
// }
//}
//[Theory]
//[MemberData(nameof(GetMappers))]
//public void MissingNullableField(Func<JsonElement, Result<BlogPost>> map)
//{
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// }
// """);
// var blogPost = map(jsonElement);

// Assert.True(blogPost.Success);
// blogPost.Value.Rating.Should().BeNull();
//}

//[Theory]
//[MemberData(nameof(GetMappers))]
//public void NulledNullableField(Func<JsonElement, Result<BlogPost>> map)
//{
// // Might seem counterintuitive, but remember, we said that
// // clr-null should reflect a missing field. In this case, the field exists, but does not have a value.
// // This would be equivalent to None in an Option type.
// // Therefor, this is not valid json for a nullable field.
// // If the field exists, it must have a value!
// var jsonElement = JsonSerializer.Deserialize<JsonElement>("""
// {
// "Rating": 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("$.Rating must have a value");
//}
}

//public class NonNullableOptional_Tests
//{
Expand Down

0 comments on commit 3bdb8b7

Please sign in to comment.