| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using Json;
- namespace Astralis {
- /// <summary>
- /// Provides typed access to a nested configuration section.
- /// Wraps a Json.Object and provides convenient getter methods with default values.
- /// </summary>
- public class WebConfigSection : GLib.Object {
- private Json.Object section_object;
- /// <summary>
- /// Internal constructor. Creates a new WebConfigSection wrapping the given Json.Object.
- /// </summary>
- /// <param name="obj">The Json.Object to wrap</param>
- internal WebConfigSection(Json.Object obj) {
- this.section_object = obj;
- }
- /// <summary>
- /// Gets a string value from the section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not a string</param>
- /// <returns>The string value or the default</returns>
- public string get_string(string key, string default = "") {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.VALUE) {
- return default;
- }
- return node.get_string() ?? default;
- }
- /// <summary>
- /// Gets an integer value from the section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not an integer</param>
- /// <returns>The integer value or the default</returns>
- public int get_int(string key, int default = 0) {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.VALUE) {
- return default;
- }
- return (int) node.get_int();
- }
- /// <summary>
- /// Gets a 64-bit integer value from the section, without truncating
- /// values outside the range of int (e.g. byte counts).
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not an integer</param>
- /// <returns>The 64-bit integer value or the default</returns>
- public int64 get_int64(string key, int64 default = 0) {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.VALUE) {
- return default;
- }
- return node.get_int();
- }
- /// <summary>
- /// Gets a boolean value from the section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not a boolean</param>
- /// <returns>The boolean value or the default</returns>
- public bool get_bool(string key, bool default = false) {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.VALUE) {
- return default;
- }
- return node.get_boolean();
- }
- /// <summary>
- /// Gets a double value from the section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not a double</param>
- /// <returns>The double value or the default</returns>
- public double get_double(string key, double default = 0.0) {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.VALUE) {
- return default;
- }
- return node.get_double();
- }
- /// <summary>
- /// Gets a string array value from the section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <param name="default">The default value if key is missing or not an array</param>
- /// <returns>The string array or the default</returns>
- public string[] get_string_array(string key, string[] default = {}) {
- if (!section_object.has_member(key)) {
- return default;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.ARRAY) {
- return default;
- }
- var array = node.get_array();
- var result = new string[array.get_length()];
- for (uint i = 0; i < array.get_length(); i++) {
- result[i] = array.get_string_element(i) ?? "";
- }
- return result;
- }
- /// <summary>
- /// Checks if a key exists in this section.
- /// </summary>
- /// <param name="key">The key to check</param>
- /// <returns>True if the key exists, false otherwise</returns>
- public bool has_key(string key) {
- return section_object.has_member(key);
- }
- /// <summary>
- /// Gets all keys in this section.
- /// </summary>
- /// <returns>A list of key names</returns>
- public GLib.List<weak string> get_keys() {
- return section_object.get_members();
- }
- /// <summary>
- /// Gets a nested section from this section.
- /// </summary>
- /// <param name="key">The key to look up</param>
- /// <returns>A WebConfigSection if the key exists and is an object, null otherwise</returns>
- public WebConfigSection? get_section(string key) {
- if (!section_object.has_member(key)) {
- return null;
- }
- var node = section_object.get_member(key);
- if (node.get_node_type() != NodeType.OBJECT) {
- return null;
- }
- return new WebConfigSection(node.get_object());
- }
- }
- }
|