WebConfigSection.vala 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Json;
  2. namespace Astralis {
  3. /// <summary>
  4. /// Provides typed access to a nested configuration section.
  5. /// Wraps a Json.Object and provides convenient getter methods with default values.
  6. /// </summary>
  7. public class WebConfigSection : GLib.Object {
  8. private Json.Object section_object;
  9. /// <summary>
  10. /// Internal constructor. Creates a new WebConfigSection wrapping the given Json.Object.
  11. /// </summary>
  12. /// <param name="obj">The Json.Object to wrap</param>
  13. internal WebConfigSection(Json.Object obj) {
  14. this.section_object = obj;
  15. }
  16. /// <summary>
  17. /// Gets a string value from the section.
  18. /// </summary>
  19. /// <param name="key">The key to look up</param>
  20. /// <param name="default">The default value if key is missing or not a string</param>
  21. /// <returns>The string value or the default</returns>
  22. public string get_string(string key, string default = "") {
  23. if (!section_object.has_member(key)) {
  24. return default;
  25. }
  26. var node = section_object.get_member(key);
  27. if (node.get_node_type() != NodeType.VALUE) {
  28. return default;
  29. }
  30. return node.get_string() ?? default;
  31. }
  32. /// <summary>
  33. /// Gets an integer value from the section.
  34. /// </summary>
  35. /// <param name="key">The key to look up</param>
  36. /// <param name="default">The default value if key is missing or not an integer</param>
  37. /// <returns>The integer value or the default</returns>
  38. public int get_int(string key, int default = 0) {
  39. if (!section_object.has_member(key)) {
  40. return default;
  41. }
  42. var node = section_object.get_member(key);
  43. if (node.get_node_type() != NodeType.VALUE) {
  44. return default;
  45. }
  46. return (int) node.get_int();
  47. }
  48. /// <summary>
  49. /// Gets a 64-bit integer value from the section, without truncating
  50. /// values outside the range of int (e.g. byte counts).
  51. /// </summary>
  52. /// <param name="key">The key to look up</param>
  53. /// <param name="default">The default value if key is missing or not an integer</param>
  54. /// <returns>The 64-bit integer value or the default</returns>
  55. public int64 get_int64(string key, int64 default = 0) {
  56. if (!section_object.has_member(key)) {
  57. return default;
  58. }
  59. var node = section_object.get_member(key);
  60. if (node.get_node_type() != NodeType.VALUE) {
  61. return default;
  62. }
  63. return node.get_int();
  64. }
  65. /// <summary>
  66. /// Gets a boolean value from the section.
  67. /// </summary>
  68. /// <param name="key">The key to look up</param>
  69. /// <param name="default">The default value if key is missing or not a boolean</param>
  70. /// <returns>The boolean value or the default</returns>
  71. public bool get_bool(string key, bool default = false) {
  72. if (!section_object.has_member(key)) {
  73. return default;
  74. }
  75. var node = section_object.get_member(key);
  76. if (node.get_node_type() != NodeType.VALUE) {
  77. return default;
  78. }
  79. return node.get_boolean();
  80. }
  81. /// <summary>
  82. /// Gets a double value from the section.
  83. /// </summary>
  84. /// <param name="key">The key to look up</param>
  85. /// <param name="default">The default value if key is missing or not a double</param>
  86. /// <returns>The double value or the default</returns>
  87. public double get_double(string key, double default = 0.0) {
  88. if (!section_object.has_member(key)) {
  89. return default;
  90. }
  91. var node = section_object.get_member(key);
  92. if (node.get_node_type() != NodeType.VALUE) {
  93. return default;
  94. }
  95. return node.get_double();
  96. }
  97. /// <summary>
  98. /// Gets a string array value from the section.
  99. /// </summary>
  100. /// <param name="key">The key to look up</param>
  101. /// <param name="default">The default value if key is missing or not an array</param>
  102. /// <returns>The string array or the default</returns>
  103. public string[] get_string_array(string key, string[] default = {}) {
  104. if (!section_object.has_member(key)) {
  105. return default;
  106. }
  107. var node = section_object.get_member(key);
  108. if (node.get_node_type() != NodeType.ARRAY) {
  109. return default;
  110. }
  111. var array = node.get_array();
  112. var result = new string[array.get_length()];
  113. for (uint i = 0; i < array.get_length(); i++) {
  114. result[i] = array.get_string_element(i) ?? "";
  115. }
  116. return result;
  117. }
  118. /// <summary>
  119. /// Checks if a key exists in this section.
  120. /// </summary>
  121. /// <param name="key">The key to check</param>
  122. /// <returns>True if the key exists, false otherwise</returns>
  123. public bool has_key(string key) {
  124. return section_object.has_member(key);
  125. }
  126. /// <summary>
  127. /// Gets all keys in this section.
  128. /// </summary>
  129. /// <returns>A list of key names</returns>
  130. public GLib.List<weak string> get_keys() {
  131. return section_object.get_members();
  132. }
  133. /// <summary>
  134. /// Gets a nested section from this section.
  135. /// </summary>
  136. /// <param name="key">The key to look up</param>
  137. /// <returns>A WebConfigSection if the key exists and is an object, null otherwise</returns>
  138. public WebConfigSection? get_section(string key) {
  139. if (!section_object.has_member(key)) {
  140. return null;
  141. }
  142. var node = section_object.get_member(key);
  143. if (node.get_node_type() != NodeType.OBJECT) {
  144. return null;
  145. }
  146. return new WebConfigSection(node.get_object());
  147. }
  148. }
  149. }