From c96646cb388ba10addc847775536149454c92c13 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 10 Aug 2020 17:04:54 +0200 Subject: [PATCH] IMplemented and used ::get_obejct. --- src/form.c | 73 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/src/form.c b/src/form.c index 4220adf..1862883 100644 --- a/src/form.c +++ b/src/form.c @@ -335,52 +335,92 @@ zak_form_json_provider_finalize (GObject *gobject) static JsonObject *zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements) { - gchar *ret; + gchar **fields; + gchar **values; - GString *key; + guint k; + + guint i; + guint l; + guint j; GValue *value; - guint i; + gboolean ok; JsonNode *node; - JsonPath *path; JsonArray *ar; JsonObject *obj; ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider)); - key = g_string_new (""); + k = 0; + fields = g_malloc0 (sizeof (gchar *)); + values = g_malloc0 (sizeof (gchar *)); for (i = 0; i < elements->len; i++) { ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); if (zak_form_element_get_is_key (element)) { + if (k > 0) + { + fields = g_realloc (fields, (k + 1) * sizeof (gchar *)); + values = g_realloc (values, (k + 1) * sizeof (gchar *)); + } + value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element)); - g_string_append_printf (key, "|%s", g_value_get_string (value)); + + fields[k] = g_strdup (zak_form_element_get_name (element)); + values[k] = g_strdup (g_value_get_string (value)); + g_value_unset (value); + + k++; } } - if (key->len > 0) + if (k > 0) { - ret = g_strdup_printf ("KEY:%s", key->str + 1); + k--; } else { - ret = g_strdup ("KEY:"); + /* no key fields */ + return NULL; } - g_string_free (key, TRUE); + ok = FALSE; - path = json_path_new (); - json_path_compile (path, g_strdup_printf ("$[*].%s", ret), NULL); + ar = json_node_get_array (priv->root); + l = json_array_get_length (ar); + for (i = 0; i < l; i++) + { + JsonNode *jnode; - node = json_path_match (path, priv->root); + jnode = json_array_get_element (ar, i); + obj = json_node_get_object (jnode); - ar = json_node_get_array (node); - obj = json_array_get_object_element (ar, 0); + ok = TRUE; + for (j = 0; j < k; j++) + { + if (g_strcmp0 (json_object_get_string_member (obj, fields[k]), values[k]) != 0) + { + ok = FALSE; + break; + } + } + + if (ok) + { + break; + } + } + + if (!ok) + { + obj = NULL; + } return obj; } @@ -425,8 +465,7 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements) ret = TRUE; - //obj = zak_form_json_provider_get_object (provider, elements); - obj = NULL; + obj = zak_form_json_provider_get_object (provider, elements); if (obj == NULL) { -- 2.49.0