]> saetta.ns0.it Git - zakform/json/commitdiff
IMplemented and used ::get_obejct.
authorAndrea Zagli <azagli@libero.it>
Mon, 10 Aug 2020 15:04:54 +0000 (17:04 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 10 Aug 2020 15:04:54 +0000 (17:04 +0200)
src/form.c

index 4220adff1c833e9ced34ea712ad9de2e82534dba..1862883be00a820d697b8c1de10f540035e3be5c 100644 (file)
@@ -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)
                {