-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better constructor support for deserialization (#32)
- Loading branch information
Showing
24 changed files
with
308 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Tomlet.Tests; | ||
|
||
public class ClassWithValuesSetOnConstructor | ||
{ | ||
public ClassWithValuesSetOnConstructor(string myString) | ||
{ | ||
MyString = "Modified on constructor!"; | ||
} | ||
|
||
public string MyString { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public abstract class AbstractClass | ||
{ | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
Tomlet.Tests/TestModelClasses/ClassWithMultipleParameterizedConstructors.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public class ClassWithMultipleParameterizedConstructors | ||
{ | ||
public ClassWithMultipleParameterizedConstructors(string myString) | ||
{ | ||
MyString = myString; | ||
} | ||
|
||
public ClassWithMultipleParameterizedConstructors(string myString, int age) | ||
{ | ||
MyString = myString; | ||
} | ||
|
||
public string MyString { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
Tomlet.Tests/TestModelClasses/ClassWithParameterlessConstructor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public class ClassWithParameterlessConstructor : ClassWithMultipleParameterizedConstructors | ||
{ | ||
public ClassWithParameterlessConstructor() : base(string.Empty, int.MinValue) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
using System; | ||
|
||
namespace Tomlet.Tests.TestModelClasses | ||
{ | ||
public record SimpleTestRecord | ||
{ | ||
public string MyString { get; init; } | ||
public float MyFloat { get; init; } | ||
public bool MyBool { get; init; } | ||
public DateTime MyDateTime { get; init; } | ||
} | ||
} | ||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public record SimpleTestRecord(string MyString, float MyFloat, bool MyBool, DateTime MyDateTime); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,8 @@ | ||
using System; | ||
|
||
namespace Tomlet.Exceptions | ||
namespace Tomlet.Exceptions | ||
{ | ||
public class TomlInstantiationException : TomlException | ||
{ | ||
private readonly Type _type; | ||
|
||
public TomlInstantiationException(Type type) | ||
{ | ||
_type = type; | ||
} | ||
|
||
public override string Message => $"Could not find a no-argument constructor for type {_type.FullName}"; | ||
public override string Message => | ||
"Deserialization of types without a parameterless constructor or a singular parameterized constructor is not supported."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Tomlet.Exceptions | ||
{ | ||
public class TomlParameterTypeMismatchException : TomlTypeMismatchException | ||
{ | ||
private readonly Type _typeBeingInstantiated; | ||
private readonly ParameterInfo _paramBeingDeserialized; | ||
|
||
public TomlParameterTypeMismatchException(Type typeBeingInstantiated, ParameterInfo paramBeingDeserialized, TomlTypeMismatchException cause) : base(cause.ExpectedType, cause.ActualType, paramBeingDeserialized.ParameterType) | ||
{ | ||
_typeBeingInstantiated = typeBeingInstantiated; | ||
_paramBeingDeserialized = paramBeingDeserialized; | ||
} | ||
|
||
public override string Message => $"While deserializing an object of type {_typeBeingInstantiated}, found parameter {_paramBeingDeserialized.Name} expecting a type of {ExpectedTypeName}, but value in TOML was of type {ActualTypeName}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Tomlet.Extensions | ||
{ | ||
internal static class ReflectionExtensions | ||
{ | ||
internal static bool TryGetBestMatchConstructor(this Type type, out ConstructorInfo? bestMatchConstructor) | ||
{ | ||
var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance); | ||
if (constructors.Length == 0) | ||
{ | ||
bestMatchConstructor = null; | ||
return false; | ||
} | ||
|
||
var parameterlessConstructor = constructors.FirstOrDefault(c => c.GetParameters().Length == 0); | ||
if (parameterlessConstructor != null) | ||
{ | ||
bestMatchConstructor = parameterlessConstructor; | ||
return true; | ||
} | ||
|
||
var parameterizedConstructors = constructors.Where(c => c.GetParameters().Length > 0).ToArray(); | ||
if (parameterizedConstructors.Length > 1) | ||
{ | ||
bestMatchConstructor = null; | ||
return false; | ||
} | ||
|
||
bestMatchConstructor = parameterizedConstructors.Single(); | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Text; | ||
|
||
namespace Tomlet.Extensions | ||
{ | ||
internal static class StringExtensions | ||
{ | ||
internal static string ToPascalCase(this string str) | ||
{ | ||
var sb = new StringBuilder(str.Length); | ||
|
||
if (str.Length > 0) | ||
{ | ||
sb.Append(char.ToUpper(str[0])); | ||
} | ||
|
||
for (var i = 1; i < str.Length; i++) | ||
{ | ||
sb.Append(char.IsWhiteSpace(str[i - 1]) ? char.ToUpper(str[i]) : str[i]); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Tomlet.Extensions; | ||
|
||
namespace Tomlet.Models | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.