Skip to content

Commit

Permalink
Oprions 類別提供 DefaultPolicy 供使用者呼叫
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzh1999tw committed Jan 16, 2024
1 parent 5825d62 commit e8480c3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
34 changes: 29 additions & 5 deletions Excely/TableConverters/ClassListTableConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public ClassListTableConverter(ClassListTableConverterOptions<TClass> options)
}
#endregion


/// <summary>
/// 將指定的 Table 轉換為 Class list。
/// </summary>
Expand Down Expand Up @@ -183,25 +182,33 @@ public class ClassListTableConverterOptions<TClass>
/// 決定 Property 作為欄位時的名稱。
/// 預設為 PropertyInfo.Name
/// </summary>
public PropertyNamePolicyDelegate PropertyNamePolicy { get; set; } = property => property.Name;
public PropertyNamePolicyDelegate PropertyNamePolicy { get; set; }

/// <summary>
/// 取得 Property 出現在表頭時的位置。
/// 預設為依類別內預設排序。
/// </summary>
public PropertyIndexPolicyDelegate PropertyIndexPolicy { get; set; } = (propertys, property) => Array.IndexOf(propertys, property);
public PropertyIndexPolicyDelegate PropertyIndexPolicy { get; set; }

/// <summary>
/// 決定將值寫入至 Property 時應寫入的值。
/// 預設為原值。
/// </summary>
public PropertyValueSettingPolicyDelegate PropertyValueSettingPolicy { get; set; } = (prop, obj) => obj;
public PropertyValueSettingPolicyDelegate PropertyValueSettingPolicy { get; set; }

/// <summary>
/// 將值輸入進物件發生錯誤時,決定錯誤處理方式。
/// 預設為不處理錯誤。
/// </summary>
public ErrorHandlingPolicyDelegate ErrorHandlingPolicy { get; set; } = (_, _, _, _, _) => false;
public ErrorHandlingPolicyDelegate ErrorHandlingPolicy { get; set; }

public ClassListTableConverterOptions()
{
PropertyNamePolicy = DefaultPropertyNamePolicy;
PropertyIndexPolicy = DefaultPropertyIndexPolicy;
PropertyValueSettingPolicy = DefaultPropertyValueSettingPolicy;
ErrorHandlingPolicy = DefaultErrorHandlingPolicyDelegate;
}

#region ===== Policy delegates =====

Expand Down Expand Up @@ -245,5 +252,22 @@ public delegate bool ErrorHandlingPolicyDelegate(
Exception exception);

#endregion ===== Policy delegates =====

#region ===== Default policies =====

public static string? DefaultPropertyNamePolicy(PropertyInfo property) => property.Name;

public static int? DefaultPropertyIndexPolicy(PropertyInfo[] allProperties, PropertyInfo property) => Array.IndexOf(allProperties, property);

public static object? DefaultPropertyValueSettingPolicy(PropertyInfo property, object? originalValue) => originalValue;

public static bool DefaultErrorHandlingPolicyDelegate(
CellLocation cellLocation,
TClass writtingObject,
PropertyInfo writtingProperty,
object? writtingValue,
Exception exception) => false;

#endregion ===== Default policies =====
}
}
24 changes: 21 additions & 3 deletions Excely/TableConverters/DictionaryListTableConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Excely.TableConverters
using System;
using System.Formats.Asn1;
using System.Reflection;

namespace Excely.TableConverters
{
/// <summary>
/// 將 Table 轉換為字典列表。
Expand Down Expand Up @@ -87,13 +91,19 @@ public class DictionaryListTableConverterOptions
/// 若 HasSchema 為 false 時,欄位名稱將是 null。
/// 預設為 (欄位名稱 ?? 欄位index)。
/// </summary>
public CustomKeyNamePolicyDelegate CustomKeyNamePolicy { get; set; } = (index, fieldName) => fieldName ?? index.ToString();
public CustomKeyNamePolicyDelegate CustomKeyNamePolicy { get; set; }

/// <summary>
/// 決定將值寫入至 Vale 時應寫入的值。
/// 預設為原值。
/// </summary>
public CustomValuePolicyDelegate CustomValuePolicy { get; set; } = (key, value) => value;
public CustomValuePolicyDelegate CustomValuePolicy { get; set; }

public DictionaryListTableConverterOptions()
{
CustomKeyNamePolicy = DefaultCustomKeyNamePolicy;
CustomValuePolicy = DefaultCustomValuePolicy;
}

#region ===== Policy delegates =====

Expand All @@ -114,5 +124,13 @@ public class DictionaryListTableConverterOptions
public delegate object? CustomValuePolicyDelegate(string key, object? originalValue);

#endregion ===== Policy delegates =====

#region ===== Default policies =====

public static string? DefaultCustomKeyNamePolicy(int fieldIndex, string? fieldName) => fieldName ?? fieldIndex.ToString();

public static object? DefaultCustomValuePolicy(string key, object? originalValue) => originalValue;

#endregion ===== Default policies =====
}
}
28 changes: 24 additions & 4 deletions Excely/TableFactories/ClassListTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,33 @@ public class ClassListTableFactoryOptions<TClass>
/// 決定 Property 是否應作為欄位匯出。
/// 預設為全部欄位都匯出。
/// </summary>
public PropertyShowPolicyDelegate PropertyShowPolicy { get; set; } = _ => true;
public PropertyShowPolicyDelegate PropertyShowPolicy { get; set; }

/// <summary>
/// 決定 Property 作為欄位時的名稱。
/// 預設為 PropertyInfo.Name。
/// </summary>
public PropertyNamePolicyDelegate PropertyNamePolicy { get; set; } = property => property.Name;
public PropertyNamePolicyDelegate PropertyNamePolicy { get; set; }

/// <summary>
/// 決定 Property 作為欄位時的順序。
/// 預設為依類別內預設排序。
/// </summary>
public PropertyOrderPolicyDelegate PropertyOrderPolicy { get; set; } = (properties, property) => Array.IndexOf(properties, property);
public PropertyOrderPolicyDelegate PropertyOrderPolicy { get; set; }

/// <summary>
/// 決定資料寫入欄位時的值。
/// 預設為該 Property 之 Value。
/// </summary>
public CustomValuePolicyDelegate CustomValuePolicy { get; set; } = (property, obj) => property.GetValue(obj);
public CustomValuePolicyDelegate CustomValuePolicy { get; set; }

public ClassListTableFactoryOptions()
{
PropertyShowPolicy = DefaultPropertyShowPolicy;
PropertyNamePolicy = DefaultPropertyNamePolicy;
PropertyOrderPolicy = DefaultPropertyOrderPolicy;
CustomValuePolicy = DefaultCustomValuePolicy;
}

#region ===== Policy delegates =====

Expand Down Expand Up @@ -141,5 +149,17 @@ public class ClassListTableFactoryOptions<TClass>
public delegate object? CustomValuePolicyDelegate(PropertyInfo property, TClass obj);

#endregion ===== Policy delegates =====

#region ===== Default policies =====

public static bool DefaultPropertyShowPolicy(PropertyInfo property) => true;

public static string? DefaultPropertyNamePolicy(PropertyInfo property) => property.Name;

public static int? DefaultPropertyOrderPolicy(PropertyInfo[] allProperties, PropertyInfo property) => Array.IndexOf(allProperties, property);

public static object? DefaultCustomValuePolicy(PropertyInfo property, TClass obj) => property.GetValue(obj);

#endregion ===== Default policies =====
}
}
33 changes: 28 additions & 5 deletions Excely/TableFactories/DictionaryListTableFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Excely.TableFactories
using System.Formats.Asn1;
using System.Reflection;

namespace Excely.TableFactories
{
/// <summary>
/// 提供以字典 Key 為欄位,將字典集合傾印至表格的功能。
Expand Down Expand Up @@ -77,25 +80,33 @@ public class DictionaryListTableFactoryOptions
/// 決定 key 是否應作為欄位匯出。
/// 預設為全部欄位都匯出。
/// </summary>
public KeyShowPolicyDelegate KeyShowPolicy { get; set; } = _ => true;
public KeyShowPolicyDelegate KeyShowPolicy { get; set; }

/// <summary>
/// 決定 key 作為欄位時的名稱。
/// 預設為 key。
/// </summary>
public KeyNamePolicyDelegate KeyNamePolicy { get; set; } = key => key;
public KeyNamePolicyDelegate KeyNamePolicy { get; set; }

/// <summary>
/// 決定 key 作為欄位時的權重(越小越靠前)。
/// 預設為 key 的預設順序。
/// </summary>
public KeyOrderPolicyDelegate KeyOrderPolicy { get; set; } = (allKeys, key) => Array.IndexOf(allKeys, key);
public KeyOrderPolicyDelegate KeyOrderPolicy { get; set; }

/// <summary>
/// 決定資料寫入欄位時的值。
/// 預設為 Value。
/// </summary>
public CustomValuePolicyDelegate CustomValuePolicy { get; set; } = (key, dict) => dict.GetValueOrDefault(key, null);
public CustomValuePolicyDelegate CustomValuePolicy { get; set; }

public DictionaryListTableFactoryOptions()
{
KeyShowPolicy = DefaultKeyShowPolicy;
KeyNamePolicy = DefaultKeyNamePolicy;
KeyOrderPolicy = DefaultKeyOrderPolicy;
CustomValuePolicy = DefaultCustomValuePolicy;
}

#region ===== Policy delegates =====

Expand Down Expand Up @@ -130,5 +141,17 @@ public class DictionaryListTableFactoryOptions
public delegate object? CustomValuePolicyDelegate(string key, Dictionary<string, object?> writtingDict);

#endregion ===== Policy delegates =====

#region ===== Default policies =====

public static bool DefaultKeyShowPolicy(string key) => true;

public static string? DefaultKeyNamePolicy(string key) => key;

public static int DefaultKeyOrderPolicy(string[] allKeys, string key) => Array.IndexOf(allKeys, key);

public static object? DefaultCustomValuePolicy(string key, Dictionary<string, object?> writtingDict) => writtingDict.GetValueOrDefault(key, null);

#endregion ===== Default policies =====
}
}

0 comments on commit e8480c3

Please sign in to comment.