1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- namespace DataService;
- public class DBProcessData<T_Output, T_PrimaryKey, T_DicValue>(Func<object, T_PrimaryKey?> KeyConverter, char spilter, string keyName)
- where T_PrimaryKey : unmanaged
- where T_Output : IDictionary<string, object>, new()
- where T_DicValue : IDictionary<T_PrimaryKey, object>, new()
- {
- public bool ToDictionary(IEnumerable<dynamic> inputs, out T_Output? output)
- {
- output = default;
- if (inputs is null)
- return false;
- T_Output cache = [];
- foreach (dynamic input in inputs)
- {
- if (input is null)
- return false;
- if ((input as IDictionary<string, object>)?.TryGetValue(keyName, out object? primaryKeyValue) != true || primaryKeyValue is null)
- return false;
- if (KeyConverter?.Invoke(primaryKeyValue) is not T_PrimaryKey primaryKey)
- return false;
- (input as IDictionary<string, object>)?.Remove(keyName);
- foreach (KeyValuePair<string, object> rawData in input)
- ColumAnalizer(cache, rawData.Key.Split(spilter), primaryKey, rawData.Value);
- }
- output = cache;
- return true;
- }
- private static bool ColumAnalizer(/*ref*/ T_Output cache, Span<string> seprated, T_PrimaryKey key, object value)
- {
- cache = TryGetValueElseCreateNew<T_Output>(cache, seprated[0]);
- _ = seprated.Length switch
- {
- 1 => TryGetValueElseCreateNew<T_DicValue>(cache, seprated[0]).TryAdd(key, value),//Recursion End
- _ => ColumAnalizer(cache, seprated[1..], key, value),//Recursion
- };
- return true;
- }
- private static T TryGetValueElseCreateNew<T>(T_Output cache, string key) where T : new()
- {
- if (!cache.TryGetValue(key, out object? output) || output is not T dic)
- {
- dic = new();
- cache[key] = dic;
- }
- return dic;
- }
- }
- class GeneralHeaderAnalizer
- {
- public static DateTime? LongToDateTime(object o)
- {
- if (o is not long l)
- return null;
- return new(l);
- }
- }
- public class GeneralProcessData : DBProcessData<Dictionary<string, object>, DateTime, Dictionary<DateTime, object>>
- {
- public GeneralProcessData() : base(GeneralHeaderAnalizer.LongToDateTime, '.', "time")
- {
- }
- }
|