DBProcessData.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace DataService;
  2. public class DBProcessData<T_Hierarchy>(char spilter, int skip)
  3. where T_Hierarchy : IDictionary<string, object>, new()
  4. {
  5. public bool ToDictionary(IDictionary<string, object> input, out T_Hierarchy? output)
  6. {
  7. output = default;
  8. if (input is null)
  9. return false;
  10. T_Hierarchy cache = [];
  11. foreach (KeyValuePair<string, object> rawData in input)
  12. {
  13. Span<string> source = rawData.Key.Split(spilter).AsSpan()[skip..];
  14. ColumAnalizer(cache, source, rawData.Value);
  15. }
  16. output = cache;
  17. return true;
  18. }
  19. private static void ColumAnalizer(/*ref*/ T_Hierarchy cache, Span<string> seprated, object value)
  20. {
  21. if (seprated.Length <= 1)
  22. {
  23. cache[seprated[0]] = value;
  24. return;
  25. }
  26. if (!cache.TryGetValue(seprated[0], out object? output) || output is not T_Hierarchy hierarchy)
  27. {
  28. hierarchy = [];
  29. cache[seprated[0]] = hierarchy;
  30. }
  31. cache = hierarchy;
  32. ColumAnalizer(cache, seprated[1..], value);
  33. }
  34. }
  35. public class GeneralProcessData(int skip = 0) : DBProcessData<Dictionary<string, object>>('.', skip)
  36. {
  37. }