1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* Copyright (c) 2024 Attila Szakacs
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "filterx/object-dict-interface.h"
#include "filterx/object-string.h"
#include "filterx/object-json.h"
gboolean
filterx_dict_iter(FilterXObject *s, FilterXDictIterFunc func, gpointer user_data)
{
FilterXDict *self = (FilterXDict *) s;
if (!self->iter)
return FALSE;
return self->iter(self, func, user_data);
}
static gboolean
_len(FilterXObject *s, guint64 *len)
{
FilterXDict *self = (FilterXDict *) s;
*len = self->len(self);
return TRUE;
}
static FilterXObject *
_get_subscript(FilterXObject *s, FilterXObject *key)
{
FilterXDict *self = (FilterXDict *) s;
if (!key)
{
msg_error("FilterX: Failed to get element of dict, key is mandatory");
return NULL;
}
return self->get_subscript(self, key);
}
static gboolean
_set_subscript(FilterXObject *s, FilterXObject *key, FilterXObject **new_value)
{
FilterXDict *self = (FilterXDict *) s;
if (!key)
{
msg_error("FilterX: Failed to set element of dict, key is mandatory");
return FALSE;
}
return self->set_subscript(self, key, new_value);
}
static gboolean
_is_key_set(FilterXObject *s, FilterXObject *key)
{
FilterXDict *self = (FilterXDict *) s;
if (!key)
{
msg_error("FilterX: Failed to check key of dict, key is mandatory");
return FALSE;
}
if (self->is_key_set)
return self->is_key_set(self, key);
FilterXObject *value = self->get_subscript(self, key);
filterx_object_unref(value);
return !!value;
}
static gboolean
_unset_key(FilterXObject *s, FilterXObject *key)
{
FilterXDict *self = (FilterXDict *) s;
if (!key)
{
msg_error("FilterX: Failed to unset element of dict, key is mandatory");
return FALSE;
}
return self->unset_key(self, key);
}
static FilterXObject *
_getattr(FilterXObject *s, FilterXObject *attr)
{
FilterXDict *self = (FilterXDict *) s;
if (!self->support_attr)
return NULL;
FilterXObject *result = self->get_subscript(self, attr);
return result;
}
static gboolean
_setattr(FilterXObject *s, FilterXObject *attr, FilterXObject **new_value)
{
FilterXDict *self = (FilterXDict *) s;
if (!self->support_attr)
return FALSE;
gboolean result = self->set_subscript(self, attr, new_value);
return result;
}
static gboolean
_add_elem_to_json_object(FilterXObject *key_obj, FilterXObject *value_obj, gpointer user_data)
{
struct json_object *object = (struct json_object *) user_data;
/* FilterX strings are always NUL terminated. */
const gchar *key = filterx_string_get_value(key_obj, NULL);
if (!key)
return FALSE;
struct json_object *value = NULL;
FilterXObject *assoc_object = NULL;
if (!filterx_object_map_to_json(value_obj, &value, &assoc_object))
return FALSE;
filterx_json_associate_cached_object(object, assoc_object);
filterx_object_unref(assoc_object);
if (json_object_object_add(object, key, value) != 0)
{
json_object_put(value);
return FALSE;
}
return TRUE;
}
static gboolean
_map_to_json(FilterXObject *s, struct json_object **object, FilterXObject **assoc_object)
{
*object = json_object_new_object();
if (!filterx_dict_iter(s, _add_elem_to_json_object, *object))
{
json_object_put(*object);
*object = NULL;
return FALSE;
}
*assoc_object = filterx_json_new_from_object(json_object_get(*object));
return TRUE;
}
void
filterx_dict_init_instance(FilterXDict *self, FilterXType *type)
{
g_assert(type->is_mutable);
g_assert(type->len == _len);
g_assert(type->get_subscript == _get_subscript);
g_assert(type->set_subscript == _set_subscript);
g_assert(type->is_key_set == _is_key_set);
g_assert(type->unset_key == _unset_key);
g_assert(type->getattr == _getattr);
g_assert(type->setattr == _setattr);
filterx_object_init_instance(&self->super, type);
self->support_attr = TRUE;
}
FILTERX_DEFINE_TYPE(dict, FILTERX_TYPE_NAME(object),
.is_mutable = TRUE,
.len = _len,
.get_subscript = _get_subscript,
.set_subscript = _set_subscript,
.is_key_set = _is_key_set,
.unset_key = _unset_key,
.getattr = _getattr,
.setattr = _setattr,
.map_to_json = _map_to_json,
);
|