PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Database/Server/RavenFS/Extensions/ConfigurationExtension.cs

https://github.com/nwendel/ravendb
C# | 57 lines | 51 code | 6 blank | 0 comment | 6 complexity | be31ee4b871db74a652a70186eaa9857 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Text;
  5. using System.Linq;
  6. using Raven.Database.Server.RavenFS.Storage;
  7. using Raven.Database.Server.RavenFS.Util;
  8. using Raven.Imports.Newtonsoft.Json;
  9. using Raven.Abstractions.Extensions;
  10. using Raven.Json.Linq;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. namespace Raven.Database.Server.RavenFS.Extensions
  14. {
  15. public static class ConfigurationExtension
  16. {
  17. public static T GetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key)
  18. {
  19. var value = accessor.GetConfig(key);
  20. if (typeof(T).IsValueType || typeof(T) == typeof(string))
  21. return value.Value<T>("Value");
  22. return JsonExtensions.JsonDeserialization<T>(value);
  23. }
  24. public static IEnumerable<T> GetConfigurationValuesStartWithPrefix<T>(this IStorageActionsAccessor accessor, string prefix, int start, int take)
  25. {
  26. var values = accessor.GetConfigsStartWithPrefix(prefix, start, take);
  27. if (typeof(T).IsValueType || typeof(T) == typeof(string))
  28. {
  29. return values.Select(x => x.Value<T>("Value"));
  30. }
  31. return values.Select(x => JsonExtensions.JsonDeserialization<T>(x));
  32. }
  33. public static bool TryGetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, out T result)
  34. {
  35. try
  36. {
  37. result = GetConfigurationValue<T>(accessor, key);
  38. return true;
  39. }
  40. catch (FileNotFoundException)
  41. {
  42. result = default(T);
  43. return false;
  44. }
  45. }
  46. public static void SetConfigurationValue<T>(this IStorageActionsAccessor accessor, string key, T objectToSave)
  47. {
  48. accessor.SetConfig(key, JsonExtensions.ToJObject(objectToSave));
  49. }
  50. }
  51. }