Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute mapping added #13

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Tomlet/TomlPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Tomlet
{
[System.AttributeUsage(System.AttributeTargets.Property)]
public class TomlPropertyAttribute : System.Attribute
{
private readonly string _mapFrom;

public TomlPropertyAttribute(string mapFrom)
{
this._mapFrom = mapFrom;
}

public string GetMappedString()
{
return _mapFrom;
}
}
}
4 changes: 3 additions & 1 deletion Tomlet/TomlSerializationMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ internal static Deserialize<object> GetCompositeDeserializer(Type type)
var name = field.Name;
var m = System.Text.RegularExpressions.Regex.Match(field.Name, "<(.*)>k__BackingField");
if (m.Success)
name = m.Groups[1].Value;
name = m.Groups[1].Value;

var name1 = name;
name = type.GetProperties().First(p => p.Name == name1).GetCustomAttribute<TomlPropertyAttribute>()?.GetMappedString() ?? name;
Wulfheart marked this conversation as resolved.
Show resolved Hide resolved
if (!table.ContainsKey(name))
continue; //TODO: Do we want to make this configurable? As in, throw exception if data is missing?

Expand Down