using Json;
namespace Astralis {
///
/// Provides typed access to a nested configuration section.
/// Wraps a Json.Object and provides convenient getter methods with default values.
///
public class WebConfigSection : GLib.Object {
private Json.Object section_object;
///
/// Internal constructor. Creates a new WebConfigSection wrapping the given Json.Object.
///
/// The Json.Object to wrap
internal WebConfigSection(Json.Object obj) {
this.section_object = obj;
}
///
/// Gets a string value from the section.
///
/// The key to look up
/// The default value if key is missing or not a string
/// The string value or the default
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;
}
///
/// Gets an integer value from the section.
///
/// The key to look up
/// The default value if key is missing or not an integer
/// The integer value or the default
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();
}
///
/// Gets a 64-bit integer value from the section, without truncating
/// values outside the range of int (e.g. byte counts).
///
/// The key to look up
/// The default value if key is missing or not an integer
/// The 64-bit integer value or the default
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();
}
///
/// Gets a boolean value from the section.
///
/// The key to look up
/// The default value if key is missing or not a boolean
/// The boolean value or the default
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();
}
///
/// Gets a double value from the section.
///
/// The key to look up
/// The default value if key is missing or not a double
/// The double value or the default
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();
}
///
/// Gets a string array value from the section.
///
/// The key to look up
/// The default value if key is missing or not an array
/// The string array or the default
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;
}
///
/// Checks if a key exists in this section.
///
/// The key to check
/// True if the key exists, false otherwise
public bool has_key(string key) {
return section_object.has_member(key);
}
///
/// Gets all keys in this section.
///
/// A list of key names
public GLib.List get_keys() {
return section_object.get_members();
}
///
/// Gets a nested section from this section.
///
/// The key to look up
/// A WebConfigSection if the key exists and is an object, null otherwise
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());
}
}
}