variant_utils.vala 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2025 Mcp-Vala Project
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. * Author: Mcp-Vala Project
  19. */
  20. /**
  21. * Utility functions for GLib.Variant operations.
  22. *
  23. * This namespace provides helper methods for common Variant operations
  24. * to reduce code duplication and improve robustness.
  25. */
  26. namespace Mcp.Types.VariantUtils {
  27. /**
  28. * Creates a new empty dictionary variant.
  29. *
  30. * @return A new empty dictionary variant (a{sv})
  31. */
  32. public static Variant new_empty_dict () {
  33. return new Variant ("a{sv}", new VariantBuilder (new VariantType ("a{sv}")));
  34. }
  35. /**
  36. * Creates a new dictionary variant with a single key-value pair.
  37. *
  38. * @param key The key for dictionary entry
  39. * @param value The value for dictionary entry
  40. * @return A new dictionary variant with key-value pair
  41. */
  42. public static Variant new_single_entry_dict (string key, Variant value) {
  43. var builder = new VariantBuilder (VariantType.VARDICT);
  44. builder.add ("{sv}", key, value);
  45. return builder.end ();
  46. }
  47. /**
  48. * Creates a new string variant with null checking.
  49. *
  50. * @param str The string value (can be null)
  51. * @return A new string variant or null if input was null
  52. */
  53. public static Variant? new_string_variant (string? str) {
  54. if (str == null) {
  55. return null;
  56. }
  57. return new Variant.string (str);
  58. }
  59. /**
  60. * Safely gets a string value from a variant with null checking.
  61. *
  62. * @param variant The variant to extract from
  63. * @param key The key to look up
  64. * @return The string value or null if not found
  65. */
  66. public static string? get_string_safe (Variant variant, string key) {
  67. var value = variant.lookup_value (key, VariantType.STRING);
  68. return value != null ? value.get_string () : null;
  69. }
  70. /**
  71. * Safely gets a boolean value from a variant with null checking.
  72. *
  73. * @param variant The variant to extract from
  74. * @param key The key to look up
  75. * @param default_value The default value if not found
  76. * @return The boolean value
  77. */
  78. public static bool get_boolean_safe (Variant variant, string key, bool default_value = false) {
  79. var value = variant.lookup_value (key, VariantType.BOOLEAN);
  80. return value != null ? value.get_boolean () : default_value;
  81. }
  82. /**
  83. * Safely gets an int64 value from a variant with null checking.
  84. *
  85. * @param variant The variant to extract from
  86. * @param key The key to look up
  87. * @param default_value The default value if not found
  88. * @return The int64 value
  89. */
  90. public static int64 get_int64_safe (Variant variant, string key, int64 default_value = 0) {
  91. var value = variant.lookup_value (key, VariantType.INT64);
  92. return value != null ? value.get_int64 () : default_value;
  93. }
  94. /**
  95. * Safely gets a dictionary value from a variant with null checking.
  96. *
  97. * @param variant The variant to extract from
  98. * @param key The key to look up
  99. * @return The dictionary variant or null if not found
  100. */
  101. public static Variant? get_dict_safe (Variant variant, string key) {
  102. return variant.lookup_value (key, VariantType.VARDICT);
  103. }
  104. /**
  105. * Adds a string value to a dictionary builder if string is not null.
  106. *
  107. * @param builder The dictionary builder
  108. * @param key The key for entry
  109. * @param value The string value (can be null)
  110. */
  111. public static void add_string_if_not_null (VariantBuilder builder, string key, string? value) {
  112. if (value != null) {
  113. builder.add ("{sv}", key, new Variant.string (value));
  114. }
  115. }
  116. /**
  117. * Adds a variant value to a dictionary builder if variant is not null.
  118. *
  119. * @param builder The dictionary builder
  120. * @param key The key for entry
  121. * @param value The variant value (can be null)
  122. */
  123. public static void add_variant_if_not_null (VariantBuilder builder, string key, Variant? value) {
  124. if (value != null) {
  125. builder.add ("{sv}", key, value);
  126. }
  127. }
  128. /**
  129. * Creates a new array builder for dictionaries.
  130. *
  131. * @return A new array builder for dictionary arrays (aa{sv})
  132. */
  133. public static VariantBuilder new_dict_array_builder () {
  134. return new VariantBuilder (new VariantType ("aa{sv}"));
  135. }
  136. /**
  137. * Creates a new dictionary builder.
  138. *
  139. * @return A new dictionary builder (a{sv})
  140. */
  141. public static VariantBuilder new_dict_builder () {
  142. return new VariantBuilder (VariantType.VARDICT);
  143. }
  144. /**
  145. * Safely creates a maybe string variant.
  146. *
  147. * @param str The string value (can be null)
  148. * @return A maybe string variant
  149. */
  150. public static Variant new_maybe_string (string? str) {
  151. if (str == null) {
  152. return new Variant.maybe (VariantType.STRING, null);
  153. } else {
  154. return new Variant.maybe (VariantType.STRING, new Variant.string (str));
  155. }
  156. }
  157. /**
  158. * Checks if a variant is null or represents a null value.
  159. *
  160. * @param variant The variant to check
  161. * @return True if variant is null or represents null
  162. */
  163. public static bool is_null_or_empty (Variant? variant) {
  164. if (variant == null) {
  165. return true;
  166. }
  167. // Check if it's a maybe type and is nothing
  168. if (variant.is_of_type (new VariantType ("m*"))) {
  169. return variant.get_maybe () == null;
  170. }
  171. return false;
  172. }
  173. /**
  174. * Creates a response variant with standard structure.
  175. *
  176. * @param data The data to include in response
  177. * @return A formatted response variant
  178. */
  179. public static Variant create_response_variant (Variant data) {
  180. var builder = new_dict_builder ();
  181. builder.add ("{sv}", "result", data);
  182. return builder.end ();
  183. }
  184. /**
  185. * Creates an error response variant.
  186. *
  187. * @param code The error code
  188. * @param message The error message
  189. * @return An error response variant
  190. */
  191. public static Variant create_error_variant (int code, string message) {
  192. var builder = new_dict_builder ();
  193. builder.add ("{sv}", "code", new Variant.int32 (code));
  194. builder.add ("{sv}", "message", new Variant.string (message));
  195. return builder.end ();
  196. }
  197. }