Skip to content

Commit

Permalink
option enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Nov 10, 2024
1 parent 885e01d commit 98f9bca
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static partial class Mappers
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]);
if (result.Success)
{
obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus)x).Value;
obj.Status = new Some<FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus>(result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NonNullableOptional_Tests.BlogPost.BlogPostStatus)x).Value);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static partial class Mappers
var result = customConverter.Convert(typeof(FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus), jsonStatusProperty, mapperOptions, [.. path, "Status"]);
if (result.Success)
{
obj.Status = result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus)x).Value;
obj.Status = new Some<FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus>(result.Map(x => (FartingUnicorn.Tests.SingleField.EnumType.NullableOptional_Tests.BlogPost.BlogPostStatus)x).Value);
}
else
{
Expand Down
11 changes: 10 additions & 1 deletion FartingUnicorn/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ public static Result<object> MapElement(Type t, ContextualPropertyInfo? contextu
var result = customConverter.Convert(type, jsonElement, mapperOptions, path);
if (result.Success)
{
return result;
if(isOption)
{
var someType = typeof(Some<>).MakeGenericType(type);
var someInstance = Activator.CreateInstance(someType, result.Value);
return Result<object>.Ok(someInstance);
}
else
{
return result;
}
}
else
{
Expand Down
7 changes: 5 additions & 2 deletions MapperGenerator/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
sb.AppendLine($"errors.Add(new ValueHasWrongTypeError([.. path, \"{p.Name}\"], \"Number\", json{p.Name}Property.ValueKind.ToString()));");
}
}
else
else // custom converter?
{
sb.AppendLine($"else if (mapperOptions.TryGetConverter(typeof({p.Type}), out IConverter customConverter))");
using(var _4 = sb.CodeBlock())
Expand All @@ -160,7 +160,10 @@ public static SourceText GenerateExtensionClass(ClassToGenerateMapperFor classTo
sb.AppendLine("if (result.Success)");
using (var _6 = sb.CodeBlock())
{
sb.AppendLine($"obj.{p.Name} = result.Map(x => ({p.Type})x).Value;");
if(p.IsOption)
sb.AppendLine($"obj.{p.Name} = new Some<{p.Type}>(result.Map(x => ({p.Type})x).Value);");
else
sb.AppendLine($"obj.{p.Name} = result.Map(x => ({p.Type})x).Value;");
}
sb.AppendLine("else");
using (var _6 = sb.CodeBlock())
Expand Down

0 comments on commit 98f9bca

Please sign in to comment.