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 59edee0 commit c9fe3f4
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static partial class Mappers
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "Title"]));
obj.Title = null;
}
if(errors.Any())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static partial class Mappers
}
else
{
errors.Add(new RequiredPropertyMissingError([.. path, "Title"]));
obj.Title = null;
}
if(errors.Any())
{
Expand Down
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.StringType.NonNullableNonOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_StringType_NonNullableNonOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NonNullableNonOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.StringType.NonNullableNonOptional_Tests.BlogPost();

List<IError> errors = new();
var isTitlePropertyDefined = jsonElement.TryGetProperty("Title", out var jsonTitleProperty);
if (isTitlePropertyDefined)
{
// type = 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.SingleField.StringType.NonNullableNonOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NonNullableNonOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
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.StringType.NonNullableOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_StringType_NonNullableOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NonNullableOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.StringType.NonNullableOptional_Tests.BlogPost();

List<IError> errors = new();
var isTitlePropertyDefined = jsonElement.TryGetProperty("Title", out var jsonTitleProperty);
if (isTitlePropertyDefined)
{
// type = String, isOption = True, isNullable = False
if (jsonTitleProperty.ValueKind == JsonValueKind.Null)
{
obj.Title = new None<String>();
}
else if (jsonTitleProperty.ValueKind == JsonValueKind.String)
{
obj.Title = new Some<string>(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.SingleField.StringType.NonNullableOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NonNullableOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
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.StringType.NullableNonOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_StringType_NullableNonOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableNonOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.StringType.NullableNonOptional_Tests.BlogPost();

List<IError> errors = new();
var isTitlePropertyDefined = jsonElement.TryGetProperty("Title", out var jsonTitleProperty);
if (isTitlePropertyDefined)
{
// type = String, isOption = False, isNullable = True
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
{
obj.Title = null;
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableNonOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableNonOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
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.StringType.NullableOptional_Tests.BlogPost> MapToFartingUnicorn_Tests_SingleField_StringType_NullableOptional_Tests_BlogPost(JsonElement jsonElement, string[] path = null)
{
if(path is null)
{
path = ["$"];
}
/*object*/
{
if (jsonElement.ValueKind != JsonValueKind.Object)
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableOptional_Tests.BlogPost>.Error(new ValueHasWrongTypeError(path, "Object", jsonElement.ValueKind.ToString()));
}
}
var obj = new FartingUnicorn.Tests.SingleField.StringType.NullableOptional_Tests.BlogPost();

List<IError> errors = new();
var isTitlePropertyDefined = jsonElement.TryGetProperty("Title", out var jsonTitleProperty);
if (isTitlePropertyDefined)
{
// type = String, isOption = True, isNullable = True
if (jsonTitleProperty.ValueKind == JsonValueKind.Null)
{
obj.Title = new None<String>();
}
else if (jsonTitleProperty.ValueKind == JsonValueKind.String)
{
obj.Title = new Some<string>(jsonTitleProperty.GetString());
}
else
{
errors.Add(new ValueHasWrongTypeError([.. path, "Title"], "String", jsonTitleProperty.ValueKind.ToString()));
}
}
else
{
obj.Title = null;
}
if(errors.Any())
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableOptional_Tests.BlogPost>.Error(errors);
}
if(false)/*check if is option*/
{
}
else
{
return Result<FartingUnicorn.Tests.SingleField.StringType.NullableOptional_Tests.BlogPost>.Ok(obj);
}
throw new NotImplementedException();
}
}
10 changes: 5 additions & 5 deletions FartingUnicorn.Tests/SingleField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace FartingUnicorn.Tests;

public class SingleField
{
public class ReferenceType
public class StringType
{
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_ReferenceType_NonNullableNonOptional_Tests_BlogPost(x, null))]
[(Func<JsonElement, Result<BlogPost>>)(x => FartingUnicorn.Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_StringType_NonNullableNonOptional_Tests_BlogPost(x, null))]

];

Expand Down Expand Up @@ -104,7 +104,7 @@ public class NullableNonOptional_Tests
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_ReferenceType_NullableNonOptional_Tests_BlogPost(x, null))]
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_StringType_NullableNonOptional_Tests_BlogPost(x, null))]
];

[CreateMapper]
Expand Down Expand Up @@ -174,7 +174,7 @@ public class NonNullableOptional_Tests
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_ReferenceType_NonNullableOptional_Tests_BlogPost(x, null))]
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_StringType_NonNullableOptional_Tests_BlogPost(x, null))]
];

[CreateMapper]
Expand Down Expand Up @@ -242,7 +242,7 @@ public class NullableOptional_Tests
public static IEnumerable<object[]> GetMappers =>
[
[(Func<JsonElement, Result<BlogPost>>)(x => Map<BlogPost>(x, null, null))],
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_ReferenceType_NullableOptional_Tests_BlogPost(x, null))]
[(Func<JsonElement, Result<BlogPost>>)(x => Generated.Mappers.MapToFartingUnicorn_Tests_SingleField_StringType_NullableOptional_Tests_BlogPost(x, null))]
];
[CreateMapper]
public class BlogPost
Expand Down
14 changes: 9 additions & 5 deletions MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
using (var _2 = sb.CodeBlock())
{
sb.AppendLine("if(path is null)");
using(var _3 = sb.CodeBlock())
using (var _3 = sb.CodeBlock())
{
sb.AppendLine("path = [\"$\"];");
}
Expand Down Expand Up @@ -104,10 +104,14 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
sb.AppendLine("else");
using (var _3 = sb.CodeBlock())
{
//if nullable
//sb.AppendLine($"obj.{p.Name} = null;");

sb.AppendLine($"errors.Add(new RequiredPropertyMissingError([.. path, \"{p.Name}\"]));");
if (p.IsNullable)
{
sb.AppendLine($"obj.{p.Name} = null;");
}
else
{
sb.AppendLine($"errors.Add(new RequiredPropertyMissingError([.. path, \"{p.Name}\"]));");
}
}
}

Expand Down

0 comments on commit c9fe3f4

Please sign in to comment.