Skip to content

Commit

Permalink
Use struct instead of dynamic tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jul 11, 2023
1 parent 4f768f5 commit 42e4dfd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/ConfigFactory.Core/ConfigModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.ComponentModel;
using ConfigFactory.Core.Attributes;
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Models;
using System.Linq.Expressions;
Expand Down Expand Up @@ -92,11 +91,11 @@ public void Save()

public bool Validate() => Validate(out _, out _);
public bool Validate(out string? message) => Validate(out message, out _);
public bool Validate(out string? message, out (PropertyInfo? info, ConfigAttribute? attribute) target)
public bool Validate(out string? message, out ConfigProperty target)
{
foreach ((var name, (var validate, var errorMessage)) in Validators) {
target = Properties[name];
PropertyInfo propertyInfo = target.info!;
PropertyInfo propertyInfo = target.Property;
if (validate(propertyInfo.GetValue(this)) is bool isValid) {
ValidationInterface?.SetValidationColor(propertyInfo, isValid ? SuccessColor : FailureColor);

Expand All @@ -107,7 +106,7 @@ public bool Validate(out string? message, out (PropertyInfo? info, ConfigAttribu
}
}

target = (null, null);
target = new();
message = "Validation Successful";
return true;
}
Expand Down
6 changes: 2 additions & 4 deletions src/ConfigFactory.Core/IConfigModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using ConfigFactory.Core.Attributes;
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Components;
using ConfigFactory.Core.Models;
using System.Reflection;

namespace ConfigFactory.Core;

Expand Down Expand Up @@ -50,5 +48,5 @@ public interface IConfigModule
public virtual bool Validate(out string? message) => Validate(out message, out _);

/// <inheritdoc cref="Validate()"/>
public bool Validate(out string? message, out (PropertyInfo? info, ConfigAttribute? attribute) target);
public bool Validate(out string? message, out ConfigProperty target);
}
4 changes: 2 additions & 2 deletions src/ConfigFactory.Core/Models/ConfigProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ConfigFactory.Core.Models;

public class ConfigProperties : Dictionary<string, (PropertyInfo info, ConfigAttribute attribute)>
public class ConfigProperties : Dictionary<string, ConfigProperty>
{
public static ConfigProperties Generate<T>()
{
Expand Down Expand Up @@ -33,7 +33,7 @@ public ConfigProperties() { }
private ConfigProperties(IEnumerable<(PropertyInfo info, ConfigAttribute? attribute)> map)
{
foreach ((var info, var attribute) in map) {
Add(info.Name, (info, attribute!));
Add(info.Name, new(info, attribute!));
}
}
}
22 changes: 22 additions & 0 deletions src/ConfigFactory.Core/Models/ConfigProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using ConfigFactory.Core.Attributes;
using System.Reflection;

namespace ConfigFactory.Core.Models;

public readonly struct ConfigProperty
{
public PropertyInfo Property { get; }
public ConfigAttribute Attribute { get; }

public void Deconstruct(out PropertyInfo property, out ConfigAttribute attribute)
{
property = Property;
attribute = Attribute;
}

public ConfigProperty(PropertyInfo property, ConfigAttribute attribute)
{
Property = property;
Attribute = attribute;
}
}

0 comments on commit 42e4dfd

Please sign in to comment.